使用recyclerview和自定义适配器启动片段时出错

时间:2018-04-23 04:52:59

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

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

  

无法将InboxActivity强制转换为InboxAdapter $ inbox_data   InboxAdapter。(InboxAdapter.java:51)at   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);
    }
}

1 个答案:

答案 0 :(得分: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
    }
}