如何更新或刷新列表视图

时间:2014-10-30 07:42:52

标签: android android-listview

我遇到了很多麻烦refreshing list view custom adapter。我似乎无法找到任何可以使我的列表视图刷新的解决方案。我已经尝试notifyDataSetChanged,还有listView.invalidate,但似乎没有任何效果。任何帮助都将大大增加。感谢

以下是代码。

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();
};

异步任务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 {

        }

    }

自定义适配器:

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;

}
}

3 个答案:

答案 0 :(得分:0)

尝试以下代码: -

myListView.invalidateViews();

至少可以为我工作。有关更多信息,请参阅以下链接: -

How to refresh Android listview?

答案 1 :(得分:0)

您可以执行以下操作。

  1. arrayList.clear();
  2. arrayList =您的新列表。
  3. commentList.invalidateViews();
  4. adapter = new ListViewAdapter(PrivateMessages.this,arraylist); commentList.setAdapter(适配器);

答案 2 :(得分:0)

谢谢大家,但我终于可以通过以下代码解决它了:

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

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

    }
};