I call
adapter.notifyDataSetChanged();
but nothing happens UNTIL I click the list view to scroll it, then the list view updates. I want it to update automatically.. what should I do?
I have tried this answer but it didn't worked for me
Obs: I'm using a custom array adapter. Its a chat app that needs to update its online users automatically.
Here's how I declare the adapter and the array adapter (onCreate) with a button that calls an AsyncTask, I call the notifyDataSetChanged inside the AsyncTask but inside onPostExecute
:
peopleList = (ListView) findViewById(R.id.peopleList);
adapter = new MyAdapter(this, people);
peopleList.setAdapter(adapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ConnectAndLoad(MainActivity.this).execute();
}
});
This is my Adapter:
public class PeopleAdapter extends ArrayAdapter<People> {
private ArrayList<People> events_list = new ArrayList<>();
Context context;
public PeopleAdapter(Context context, ArrayList<People> users) {
super(context, 0, users);
this.context = context;
this.events_list = users;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
People user = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.people_list, parent, false);
}
TextView tvName = (TextView) convertView.findViewById(R.id.name);
TextView tvStatus = (TextView) convertView.findViewById(R.id.status);
tvName.setText(user.name);
tvStatus.setText(user.status);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "You Clicked " + events_list.get(position).name, Toast.LENGTH_SHORT).show();
Intent i = new Intent(context, ConversationActivity.class);
i.putExtra("user", events_list.get(position).name);
context.startActivity(i);
}
});
return convertView;
}
}
Finally here is the AsyncTask's onPostExecute that calls notifyDataSetChanged, everytime a user has its presence changed it is supposed to add the new user to the people list, and then call the notifyDataSetChanged (It adds the new person to the list and it actually changes the data set, the ONLY problem is that I only see the updated list once I click the listview!):
protected void onPostExecute(Boolean boo) {
roster.addRosterListener(new RosterListener() {
public void entriesDeleted(Collection<String> addresses) {
}
public void entriesUpdated(Collection<String> addresses) {
}
public void entriesAdded(Collection<String> addresses) {
}
@Override
public void presenceChanged(Presence presence) {
people.add(new People(presence.getFrom(), presence.getStatus()));
adapter.notifyDataSetChanged();
}
});
dialog.dismiss();
}
EDIT: I followed what @savepopulation said and took the adapter.notifyDataSetChanged();
out of presenceChanged and it worked. BUT I need this to be inside the presenceChanged so it updates when users go online and offline. If you guys have any advice please let me know :)