问题
My code work without error, but i can't get data from Firebase, i use custom adapter for fragment and when i open fragment there are empty. I don't know what i need do. P.S when i use code in Activity all work. In Manifest i have all permissions. Other Activity where need get data from Firebase, working very well. My code Fragment
public class ChatList extends Fragment implements ContactsAdapter.iData, InboxAdapter.inbox_data{
LinearLayoutManager messageLayoutManager,contactsLayoutManager;
ContactsAdapter adapter_contacts;
InboxAdapter adapter_inbox;
ArrayList<User> all_users = new ArrayList<>();
User current;
RecyclerView inbox_rv;
ArrayList<InboxObject> allInboxObjects = new ArrayList<>();
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);
}
@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
ArrayList<InboxObject> allInboxObjects = new ArrayList<>();
adapter_inbox = new InboxAdapter(allInboxObjects, getContext(), this);
inbox_rv.setLayoutManager(messageLayoutManager);
inbox_rv.setAdapter(adapter_inbox);
return layout;
}
@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);
}
}
Log.d("demo","Number of inbox is"+ allInboxObjects.size());
Collections.sort(allInboxObjects, new Comparator<InboxObject>() {
@Override
public int compare(InboxObject inboxObject, InboxObject t1) {
Date d1 = null;
Date d2 = null;
try {
d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(inboxObject.getLastMessage().getCreatedAt());
d2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(t1.getLastMessage().getCreatedAt());
} catch (ParseException e) {
e.printStackTrace();
}
if(d1.before(d2)){
return 1;
}
else if(d1.after(d2)){
return -1;
}
else{
return 0;
}
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
adapter_inbox = new InboxAdapter(allInboxObjects, getContext(), this);
inbox_rv.setLayoutManager(messageLayoutManager);
inbox_rv.setAdapter(adapter_inbox);
}
@Override
public void onItemClick(User u) {
}
@Override
public void onInboxClick(String reciver, String photo, String fn) {
}
}
My code custom adapter
public class InboxAdapter extends RecyclerView.Adapter<InboxAdapter.InboxViewHolder> {
public ArrayList<InboxObject> inbox_list;
public 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);
}
DatabaseReference f_database = FirebaseDatabase.getInstance().getReference();
User contact_user;
// private iData inbox_interface;
public InboxAdapter(ArrayList<InboxObject> inbox_list, Context mContext,inbox_data chatList) {
this.inbox_list = inbox_list;
this.f_manager = f_manager;
this.mContext = mContext;
this.inbox_interface = (inbox_data) chatList;
}
@Override
public InboxViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflatedView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_inbox, parent, false);
return new InboxViewHolder(inflatedView);
}
@Override
public void onBindViewHolder(final InboxViewHolder holder, int position) {
final InboxObject io = inbox_list.get(position);
holder.lastMsg.setText(io.getLastMessage().getMessage());
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(io.getLastMessage().getCreatedAt());
} catch (ParseException e) {
e.printStackTrace();
}
holder.time.setText(new SimpleDateFormat("HH:mm-EEE").format(date));
if(io.getLastMessageRead() == true){
holder.star.setVisibility(View.INVISIBLE);
}else{
holder.star.setVisibility(View.VISIBLE);
}
contact_user = io.getReceiver_user();
holder.contact_username.setText(contact_user.getFirstname()+" "+contact_user.getLastname());
if(contact_user.getPhotoUrl() == null || contact_user.getPhotoUrl().equals("")){
holder.contact_avatar
.setImageDrawable(ContextCompat.getDrawable(mContext,
R.drawable.ic_account_circle_black_36dp));
}
else{
Picasso.with(mContext).load(contact_user.getPhotoUrl())
.into(holder.contact_avatar);
}
holder.container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
InboxObject current_object = io;
contact_user=io.getReceiver_user();
current_object.setLastMessageRead(true);
if(inbox_interface != null) {
String reciver = contact_user.getUid();
String fn = contact_user.getFirstname();
String photo = contact_user.getPhotoUrl();
inbox_interface.onInboxClick(reciver, photo, fn);
}
else{
Log.d("demo","click failed");
}
}
});
holder.container.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
f_database.child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("inboxobjects").child(contact_user.getUid()).removeValue();
f_database.child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("inbox").child(contact_user.getUid()).removeValue();
return true;
}
});
}
@Override
public int getItemCount() {
return inbox_list.size();
}
public class InboxViewHolder extends RecyclerView.ViewHolder {
private CircleImageView contact_avatar;
private TextView contact_username, time;
private TextView lastMsg;
private View container;
private ImageView star;
public InboxViewHolder(View itemView) {
super(itemView);
contact_username = (TextView) itemView.findViewById(R.id.messengerTextView);
lastMsg = (TextView) itemView.findViewById(R.id.messageTextView);
contact_avatar = (CircleImageView) itemView.findViewById(R.id.messengerImageView);
container = (View) itemView.findViewById(R.id.rowContainer);
time = (TextView) itemView.findViewById(R.id.timeView);
star = (ImageView) itemView.findViewById(R.id.starView);
//container.setOnClickListener(this);
}
}
}
回答1:
Need add in Fragment this code
adapter_inbox.notifyDataSetChanged();
来源:https://stackoverflow.com/questions/49975066/i-cant-get-data-from-firebase-in-fragment-with-custom-adapter