[英]Android ListView with LinkedHashMap - scrambled list when scrolling
I've had trouble finding much documentation on using LinkedHashMap with a ListView for Android, but I was able to get the list to populate correctly with the below. 我很难找到很多有关将LinkedHashMap与Android的ListView一起使用的文档,但是我能够使用以下内容正确填充列表。
Unfortunately, when I scroll, the list is scrambled the same as it would be if I was using a regular HashMap. 不幸的是,当我滚动时,列表的混乱程度与使用常规HashMap时的混乱情况相同。 I'm not sure what I'm doing wrong. 我不确定自己在做什么错。
MainActivity.java (only a portion): MainActivity.java (仅一部分):
public class MainActivity extends ListActivity {
private ListView dataList;
ArrayList<String> listItems=new ArrayList<String>();
ListViewAdapter adapter;
private ArrayList<LinkedHashMap<String, String>> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataList = (ListView)findViewById(android.R.id.list);
list=new ArrayList<LinkedHashMap<String,String>>();
adapter=new ListViewAdapter(this, list);
dataList.setAdapter(adapter);
addItems();
}
public void addItems() {
LinkedHashMap<String,String> row = new LinkedHashMap<String, String>();
row.put(KEY_COLUMN, key);
row.put(FIRST_COLUMN, dateTime);
row.put(SECOND_COLUMN, data1);
row.put(THIRD_COLUMN, data2);
row.put(FOURTH_COLUMN, data3);
list.add(row);
adapter.notifyDataSetChanged();
}
}
ListViewAdapter Class ListViewAdapter类
public class ListViewAdapter extends BaseAdapter{
public ArrayList<LinkedHashMap<String, String>> list;
Activity activity;
TextView txtKey;
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFourth;
public ListViewAdapter(Activity activity,ArrayList<LinkedHashMap<String, String>> list){
super();
this.activity = activity;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
LayoutInflater inflater = activity.getLayoutInflater();
convertView = inflater.inflate(R.layout.list_row_columns, null);
txtKey = (TextView) convertView.findViewById(R.id.row_key);
txtFirst = (TextView) convertView.findViewById(R.id.date_time);
txtSecond = (TextView) convertView.findViewById(R.id.data1);
txtThird = (TextView) convertView.findViewById(R.id.data2);
txtFourth = (TextView) convertView.findViewById(R.id.data3);
}
LinkedHashMap<String, String> map = list.get(position);
txtKey.setText(map.get(KEY_COLUMN));
txtFirst.setText(map.get(FIRST_COLUMN));
txtSecond.setText(map.get(SECOND_COLUMN));
txtThird.setText(map.get(THIRD_COLUMN));
txtFourth.setText(map.get(FOURTH_COLUMN));
return convertView;
}
}
The simplest solution would be to use ArrayAdapter
rather than rolling your own BaseAdapter
subclass. 最简单的解决方案是使用ArrayAdapter
而不是滚动自己的BaseAdapter
子类。 But, assuming that you really want to extend BaseAdapter
... 但是,假设您确实要扩展BaseAdapter
...
First, get rid of all of the widgets from the class declaration (eg, TextView txtKey
), moving them to be local variables inside of getView()
. 首先,从类声明中删除所有小部件(例如TextView txtKey
),将它们移到getView()
内的局部变量中。
Then, populate those local variables whether or not you are inflating a new layout. 然后,填充这些局部变量,而不管是否要填充新的布局。
Other improvements: 其他改进:
Never use LayoutInflater.from()
when you have an Activity
. 拥有Activity
时,切勿使用LayoutInflater.from()
。 Call getLayoutInflater()
on the Activity
, so you get a LayoutInflater
that knows about your themes and styles. 在Activity
上调用getLayoutInflater()
,这样您将获得一个知道主题和样式的LayoutInflater
。
Use getItem()
, instead of list.get(position)
, in getView()
, so getView()
and getItem()
stay in sync. 在getView()
使用getItem()
而不是list.get(position)
,以便getView()
和getItem()
保持同步。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.