Reputation: 23
After inizializing the custom adapter for my ListView it draw the first part of the dataset. Now I want to add some other view at the bottom of the listview. But when i use notifyDataSetChanged it doesn't refresh the listview.
setup
ListView messages = findViewById(R.id.latest_messages);
adapter = new AdapterChat(getApplicationContext(),
R.layout.element_chat_other, inputItems);
messages = (ListView) findViewById(R.id.messages);
messages.setAdapter(adapter);
messages.setSmoothScrollbarEnabled(true);
update not working:
i add a new object in the arraylist and then recall the adapter for refreshing data. But view still not showing:
inputItems.add(new DTOChat(fromCC,messageCC, tsCC, idCC));
adapter = new AdapterChat(getApplicationContext(),
R.layout.element_chat_other, inputItems);
adapter.notifyDataSetChanged();
ADAPTER
public class AdapterChat extends ArrayAdapter<DTOChat > {
Context context;
int rand = 0;
public AdapterChat(Context context, int risorsaId,
List<DTOChat> items) {
super(context, risorsaId, items);
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
//controllo se c'è gia un layout
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final DTOChat content = getItem(position);
SharedPreferences preferences = getContext().getSharedPreferences("pref", Context.MODE_PRIVATE);
String username = preferences.getString("username", "");
if(content.getFrom().toLowerCase().equals(username.toLowerCase())) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.element_chat_other, null);
}
} else {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.element_chat_me, null);
}
}
final TextView msg = convertView.findViewById(R.id.msg);
msg.setText(content.getMessage());
//data
TextView ts = convertView.findViewById(R.id.ts);
String[] chunks = content.getTs().split(" ");
String[] chunks2 = chunks[1].split(":");
ts.setText(chunks2[0]+":"+chunks2[1]);
return convertView;
}
}
Upvotes: 0
Views: 295
Reputation: 3930
try like this
inputItems.add(new DTOChat(fromCC,messageCC, tsCC, idCC));
// here no need to create new adapter instance only add below line
adapter.notifyDataSetChanged();
Upvotes: 0
Reputation: 13019
With the following statement, you assign an instance of AdapterChat
to adapter
adapter = new AdapterChat(getApplicationContext(),
R.layout.element_chat_other, inputItems);
Then you set this instance as adapter for the ListView
messages.setAdapter(adapter);
Some time later, you add a new item to the data list inputItems and assign another instance of AdapterChat
to adapter
inputItems.add(new DTOChat(fromCC,messageCC, tsCC, idCC));
adapter = new AdapterChat(getApplicationContext(),
R.layout.element_chat_other, inputItems);
Now, the old instance is still set as the current ListView
adapter but when you call adapter.notifyDataSetChanged();
you notify the recently created instance which you assigned to adapter.
You can keep adapter the way it is (don't create a new instance), simply add the new item to inputItems and directly afterwards call
adapter.notifyDataSetChanged();
Upvotes: 1