I'm having a lot of trouble refreshing a list view with a custom adapter. I can't seem to find any solution that will make my list view refresh. I've tried notifyDataSetChanged, and also listView.invalidate, but nothing seems to be working. Any help will greatly be appreaciated. Thanks
Below is the code.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
Toast.makeText(getApplicationContext(), "Update Chat Ui",
Toast.LENGTH_LONG).show();
commentList.invalidateViews();
commentList.setAdapter(adapter);
adapter.notifyDataSetChanged();
//adapter.notifyDataSetInvalidated();
// adapter.notifyDataSetChanged();
}
};
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(
mMessageReceiver);
super.onPause();
};
Async task onPost ():
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
adapter = new ListViewAdapter(PrivateMessages.this, arraylist);
commentList.setAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (Exception e) {
} finally {
}
}
Custom Adapter :
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
UserFunctions mUserFunctions;
Context context;
ArrayList<HashMap<String, String>> data;
Preferences mPreferences;
public static int actualPosition;
private static HashMap<String, String> resultp = new HashMap<String, String>();
boolean likeState;
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
mPreferences = new Preferences(context);
mUserFunctions = new UserFunctions();
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
ViewHolder holder = new ViewHolder();
likeState = false;
resultp = data.get(position);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (resultp.get("message_admin").equals(mPreferences.getProfileId())) {
convertView = inflater.inflate(R.layout.comment_listview_item_user,
null);
} else {
convertView = inflater
.inflate(R.layout.comment_listview_item, null);
}
holder.userComment = (TextView) convertView
.findViewById(R.id.comment_item);
holder.commentTime = (TextView) convertView
.findViewById(R.id.time_of_comment);
holder.userComment.setText(resultp.get("message"));
holder.commentTime.setText(resultp.get("message_time"));
return convertView;
}
static class ViewHolder {
TextView userComment;
TextView commentTime;
}
}