我正在使用从 JSON 数组获取的数据填充 ListView 。当我在 ListView 上滚动时,我获取的元数据会被复制。
public View getView(int pos, View convertView, ViewGroup parent) {
View tv;
TextView t;
if (convertView == null)
tv = m_inflater.inflate (R.layout.item, parent, false);
else
tv = convertView;
try {
t = (TextView) tv.findViewById(R.id.text);
JSONObject obj = _results.getJSONObject(pos);
t.setText (obj.getString("title").replaceAll("\\<.*?\\>", ""));
t = (TextView) tv.findViewById(R.id.created_at);
JSONObject meta = obj.getJSONObject("meta");
t.setText (t.getText() + "\n"+ "When:" + "\t"+meta.getString("startDate")+"\n"+"Location:" +"\t" +meta.getString("location")+"\n" +"More Info:"+"\t" +meta.getString("eventURL")+"\n");
} catch (JSONException e) {
Log.e("alatta", e.getMessage());
}
return tv;
}
最佳答案
而不是
t.setText (t.getText() + "\n"+ "When:" + "\t"+meta.getString( ...
试试这个:
t.setText("When:" + "\t"+meta.getString( ...)
这是因为 ListView 重用列表项对象 (View ConvertView
),因此在滚动时您会得到一个已使用的 tv
并且其 View 已填充了一些值。
关于java - 在 ListView 上滚动会创建重复的元数据项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11235263/