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