'notifydatasetchanged not working after app close?
My code is as follows. I have a couple of classes and when someone want to update it doesn't work.what could be the problem? I checked it a lot and the application closes = arrayAdapter.notifyDataSetChanged();
public class CardArrayAdapter extends ArrayAdapter<Card> {
public CardArrayAdapter(Context context, int resource,List<Card> objects) {
super(context, resource, objects);
}
@NonNull
@Override
public View getView(int position,View convertView, ViewGroup parent) {
Card card = getItem(position);
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item,parent,false);
TextView textView = convertView.findViewById(R.id.ItemTextView);
ImageView imageView = convertView.findViewById(R.id.ItemImageView);
textView.setText(card.name);
Glide.with(getContext()).load(card.photoURL).into(imageView);
return convertView;
}
}
Main class
ArrayList<Card> list;
CardArrayAdapter arrayAdapter;
public void loadsCards(){
list=new ArrayList<Card>();
String name = dataSnapshot.child("name").getValue().toString();
String photoURL = dataSnapshot.child("photo").getValue().toString();
String uid = dataSnapshot.getKey();
Card card = new Card(uid,photoURL,name);
Toast.makeText(getActivity(), "sonuc:"+card.name, Toast.LENGTH_LONG).show();
list.add(card);
arrayAdapter.notifyDataSetChanged(); //closes here
}
Solution 1:[1]
I guess you are trying to use RecyclerView and populate it with your arrayAdapter.You are missing the statement to instruct the recycler view to use the adapter.
Try adding this and check if it works
recyclerView.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
If you are using any other views such as ListView you have to set your adapter to that.
listView.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
If you are not using any views, you can't get your data populated. Please check this article for more on ArrayAdapter: ArrayAdapter with ListView, ArrayAdapter with RecyclerView
Solution 2:[2]
//I solved the problem.
list=new ArrayList<Card>();
//adding code
arrayAdapter=new CardArrayAdapter(this.getContext(),list.size(),list);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Asutosh |
| Solution 2 | Alp |
