-1

I am trying display some data from Firebase in a Fragment, for this i need to use custom adapter.When i open fragment, my app is stopping. When I delete this.inbox_interface = (inbox_data) mContext; in Constructor app working, but fragment is empty. In logcat I get error

InboxActivity cannot be cast to InboxAdapter$inbox_data at InboxAdapter.(InboxAdapter.java:51) at ChatList.onCreateView(ChatList.java:68)

Adapter piece of code

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;
}

Fragment piece of code

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);
    }
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
I.Kuznec
  • 7
  • 5

1 Answers1

1

You need to move out the interface that you have declared inside your adapter and have to implement the interface in your activity.

So create a separate interface class named inbox_data.java

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

And then modify your adapter like this.

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 
}

Now implement the interface in your fragment.

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
    }
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98