0

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;

}
}
M S Gadag
  • 2,057
  • 1
  • 12
  • 15
addy123
  • 514
  • 2
  • 10
  • 28
  • Why are you always doing convertView = inflater.inflate ? the point of list view is that it uses recycled view, you should check if convertview is null before inflating it. This may not be directly connected to your problem, but you should still fix it. – JanBo Oct 30 '14 at 07:59
  • @JanBo thanks... and I know this, but somehow I need to inflate 2 different layouts at different conditions. that's why I tired it. Doing otherway round was giving me nullpointer. Well, I'll still try to do that again. – addy123 Oct 30 '14 at 08:11
  • You can inflate different layouts and still use the covertView ;) just need to work a little bit more on it. – JanBo Oct 30 '14 at 08:15

3 Answers3

0

try below code:-

myListView.invalidateViews();

Works fine atleast for me.For more information see below link :-

How to refresh Android listview?

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
0

You can do following.

  1. arrayList.clear();
  2. arrayList = your new List.
  3. commentList.invalidateViews();
  4. adapter = new ListViewAdapter(PrivateMessages.this, arraylist); commentList.setAdapter(adapter);

Ara Badalyan
  • 1,616
  • 1
  • 15
  • 20
0

Thanks all, but I finally able to resolve it through the following code:

    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        new InitiateChatTask().execute(wish_user_id,
                mPreferences.getProfileId());
        commentList.invalidateViews();          

    }
};
addy123
  • 514
  • 2
  • 10
  • 28