我遇到了很多麻烦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;
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以执行以下操作。
arrayList.clear();
commentList.invalidateViews();
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();
}
};