【问题标题】:Error when i start fragment with recyclerview and custom adapter当我使用 recyclerview 和自定义适配器启动片段时出错
【发布时间】:2018-04-23 13:17:38
【问题描述】:

我正在尝试在 Fragment 中显示来自 Firebase 的一些数据,为此我需要使用自定义适配器。当我打开片段时,我的应用程序正在停止。当我在构造函数应用程序中删除this.inbox_interface = (inbox_data) mContext; 时,片段为空。在 logcat 我得到错误

InboxActivity 无法在以下位置转换为 InboxAdapter$inbox_data InboxAdapter.(InboxAdapter.java:51) 在 ChatList.onCreateView(ChatList.java:68)

适配器代码段

DatabaseReference f_database = FirebaseDatabase.getInstance().getReference();
User contact_user;
public ArrayList<InboxObject> inbox_list;
private Fragment chatlist;
public Context mContext;
public inbox_data inbox_interface;
public FragmentManager f_manager;
public interface inbox_data{
    void onInboxClick(String reciver, String photo, String fn);
}

public InboxAdapter(ArrayList<InboxObject> inbox_list, Context mContext) {
    this.inbox_list = inbox_list;
    this.f_manager = f_manager;
    this.mContext = mContext;
    this.inbox_interface = (inbox_data) mContext;
}

代码片段

public class ChatList extends Fragment implements ContactsAdapter.iData,InboxAdapter.inbox_data{
    LinearLayoutManager messageLayoutManager,contactsLayoutManager;

    InboxAdapter adapter_inbox;
    ArrayList<User> all_users = new ArrayList<>();
    User current;
    RecyclerView inbox_rv;
    ArrayList<InboxObject> allInboxObjects;
    public InboxAdapter.inbox_data inbox_interface;
    public static final String CONTACT_TAG = "contact_user";
    public static final String CURRENT_USER = "current_user";
    GoogleApiClient mGoogleApiClient;
    DatabaseReference f_database = FirebaseDatabase.getInstance().getReference();
    StorageReference f_storage = FirebaseStorage.getInstance().getReference();

    public ChatList() {
        // Required empty public constructor
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        allInboxObjects = new ArrayList<>();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        Context context = container.getContext();
        View layout = LayoutInflater.from(context).inflate(R.layout.fragment_chat_list, container, false);
        inbox_rv = (RecyclerView) layout.findViewById(R.id.inboxsRecyclerView);
        // Inflate the layout for this fragment

        messageLayoutManager= new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL, false);
        ArrayList<InboxObject> allInboxObjects = new ArrayList<>();
        InboxAdapter adapter_inbox = new InboxAdapter(allInboxObjects, getActivity());
        inbox_rv.setLayoutManager(messageLayoutManager);
        inbox_rv.setAdapter(adapter_inbox);
        return inflater.inflate(R.layout.fragment_chat_list, container, false);
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
    }
    @Override
    public void onStart() {
        super.onStart();
        Log.d("demo","onStart");
        f_database.child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("inboxobjects").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.d("demo","onStart:inside inbox data change");
                allInboxObjects.clear();
                for (com.google.firebase.database.DataSnapshot snapshot: dataSnapshot.getChildren()) {


                    if(snapshot.getKey().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())){

                    }
                    else{
                        InboxObject io = snapshot.getValue(InboxObject.class);
                        allInboxObjects.add(io);
                    }
                }
                });
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });

        InboxAdapter adapter_inbox = new InboxAdapter(allInboxObjects, getActivity());
        inbox_rv.setLayoutManager(messageLayoutManager);
        inbox_rv.setAdapter(adapter_inbox);
    }
}

【问题讨论】:

    标签: android android-studio android-fragments android-arrayadapter android-adapter


    【解决方案1】:

    您需要移出您在适配器中声明的interface,并且必须在您的活动中实现接口。

    所以创建一个单独的接口类,命名为inbox_data.java

    public interface inbox_data{
        void onInboxClick(String reciver, String photo, String fn);
    }
    

    然后像这样修改你的适配器。

    DatabaseReference f_database = FirebaseDatabase.getInstance().getReference();
    User contact_user;
    public ArrayList<InboxObject> inbox_list;
    private Fragment chatlist;
    public Context mContext;
    public inbox_data inbox_interface;
    public FragmentManager f_manager;
    
    public InboxAdapter(ArrayList<InboxObject> inbox_list, Context mContext, inbox_data inbox_interface) {
        this.inbox_list = inbox_list;
        this.f_manager = f_manager;
        this.mContext = mContext;
        this.inbox_interface = inbox_interface; // Assign the interface instead 
    }
    

    现在在您的片段中实现接口。

    public class ChatList extends Fragment implements ContactsAdapter.iData, inbox_data{
    
        LinearLayoutManager messageLayoutManager,contactsLayoutManager;
    
        InboxAdapter adapter_inbox;
        ArrayList<User> all_users = new ArrayList<>();
        User current;
        RecyclerView inbox_rv;
        ArrayList<InboxObject> allInboxObjects;
        public InboxAdapter.inbox_data inbox_interface;
        public static final String CONTACT_TAG = "contact_user";
        public static final String CURRENT_USER = "current_user";
        GoogleApiClient mGoogleApiClient;
        DatabaseReference f_database = FirebaseDatabase.getInstance().getReference();
        StorageReference f_storage = FirebaseStorage.getInstance().getReference();
    
        public ChatList() {
            // Required empty public constructor
        }
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            allInboxObjects = new ArrayList<>();
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            Context context = container.getContext();
            View layout = LayoutInflater.from(context).inflate(R.layout.fragment_chat_list, container, false);
            inbox_rv = (RecyclerView) layout.findViewById(R.id.inboxsRecyclerView);
            // Inflate the layout for this fragment
    
            messageLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL, false);
            ArrayList<InboxObject> allInboxObjects = new ArrayList<>();
            InboxAdapter adapter_inbox = new InboxAdapter(allInboxObjects, getActivity(), this); // Pass the interface using this
            inbox_rv.setLayoutManager(messageLayoutManager);
            inbox_rv.setAdapter(adapter_inbox);
            return inflater.inflate(R.layout.fragment_chat_list, container, false);
        }
    
        @Override
        public void onStart() {
            super.onStart();
            Log.d("demo","onStart");
            f_database.child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("inboxobjects").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Log.d("demo","onStart:inside inbox data change");
                    allInboxObjects.clear();
                    for (com.google.firebase.database.DataSnapshot snapshot: dataSnapshot.getChildren()) {
    
    
                        if(snapshot.getKey().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())){
    
                        }
                        else{
                            InboxObject io = snapshot.getValue(InboxObject.class);
                            allInboxObjects.add(io);
                        }
                    }
                    });
                }
    
                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
    
            InboxAdapter adapter_inbox = new InboxAdapter(allInboxObjects, getActivity(), this); // Pass the interface to the adapter using this
            inbox_rv.setLayoutManager(messageLayoutManager);
            inbox_rv.setAdapter(adapter_inbox);
        }
    
        // Override the function of the implemented interface.
        @Override
        public void void onInboxClick(String reciver, String photo, String fn) {
            // Do something based on the item click in the RecyclerView
        }
    }
    

    【讨论】:

    • 朋友,现在代码可以正常工作,但我无法从服务器(Firebase)获取数据
    • 您在AndroidManifest.xml 中有INTERNET 权限吗?
    • 是的,我有 INTERNET 权限
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多