我有两个ListView
让我们称他们为1和2.我将项目添加到ListView 1
,然后在一段时间后,我将项目移至ListView 2
。
我遇到的问题是,有时(我还没有制定出模式)ListView 1
中显示的文字是错误的 - 它显示了先前创建的项目的文本。但当我将其移至ListView 2
时,它会显示正确的文字。
我理解视图已被回收,因此列表项可以使用旧视图。我不认为它是这样做的,因为它在另一个列表中显示了正确的信息。
由于getView()
方法中存在大量文本,因此设置文本可能需要很长时间,因此它会在后台更新时使用旧值。我不知道这是否可行,但即使我致电notifyDataSetChanged()
,仍然会显示错误信息。
这是我的代码:
ListView 1
。
这是List的适配器:
public class PendingAdapter extends BaseAdapter {
private List<Map<String, Object>> mPendingItemList;
public PendingAdapter() {
mPendingItemList = DataModel.getInstance().getPendingItemList();
}
@Override
public int getCount() {
return mPendingItemList.size();
}
@Override
public Object getItem(int position) {
return mPendingItemList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = LayoutInflater.from(getActivity()).inflate(
R.layout.pending_item, null);
}
TextView tv_title = (TextView) convertView
.findViewById(R.id.pi_tv_title);
TextView tv_content = (TextView) convertView
.findViewById(R.id.pi_tv_content);
TextView tv_counter = (TextView) convertView
.findViewById(R.id.pi_tv_counter);
TextView tv_ongoing = (TextView) convertView
.findViewById(R.id.pi_tv_ongoing);
TextView tv_type = (TextView) convertView
.findViewById(R.id.pi_tv_type);
TextView tv_date = (TextView) convertView
.findViewById(R.id.pi_tv_date);
@SuppressWarnings("unchecked")
HashMap<String, String> itemDataHashMap = (HashMap<String, String>) getItem(position);
tv_title.setText(itemDataHashMap.get("planet"));
tv_content.setText(itemDataHashMap.get("content"));
tv_counter.setText(itemDataHashMap.get("counter"));
tv_type.setText(itemDataHashMap.get("type"));
tv_ongoing.setText(itemDataHashMap.get("ongoing"));
tv_date.setText(itemDataHashMap.get("date"));
return convertView;
}
}
我不确定哪些其他代码会有用(也许是mPendingItemList
) - 但如果有更多帮助请说: - )。
答案 0 :(得分:2)
// try this one
public class PendingAdapter extends BaseAdapter {
private List<Map<String, Object>> mPendingItemList;
public PendingAdapter() {
mPendingItemList = DataModel.getInstance().getPendingItemList();
}
@Override
public int getCount() {
return mPendingItemList.size();
}
@Override
public Object getItem(int position) {
return mPendingItemList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (null == convertView) {
convertView = LayoutInflater.from(getActivity()).inflate(
R.layout.pending_item, null);
holder = new ViewHolder();
holder.tv_title = (TextView) convertView
.findViewById(R.id.pi_tv_title);
holder.tv_content = (TextView) convertView
.findViewById(R.id.pi_tv_content);
holder.tv_counter = (TextView) convertView
.findViewById(R.id.pi_tv_counter);
holder.tv_ongoing = (TextView) convertView
.findViewById(R.id.pi_tv_ongoing);
holder.tv_type = (TextView) convertView
.findViewById(R.id.pi_tv_type);
holder.tv_date = (TextView) convertView
.findViewById(R.id.pi_tv_date);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
@SuppressWarnings("unchecked")
HashMap<String, String> itemDataHashMap = (HashMap<String, String>) getItem(position);
holder.tv_title.setText(itemDataHashMap.get("planet"));
holder.tv_content.setText(itemDataHashMap.get("content"));
holder.tv_counter.setText(itemDataHashMap.get("counter"));
holder. tv_type.setText(itemDataHashMap.get("type"));
holder.tv_ongoing.setText(itemDataHashMap.get("ongoing"));
holder.tv_date.setText(itemDataHashMap.get("date"));
convertView.setTag(holder);
return convertView;
}
static class ViewHolder {
TextView tv_title;
TextView tv_content;
TextView tv_counter;
TextView tv_ongoing;
TextView tv_type;
TextView tv_date;
}
}