出现该问题原因为在设置每个条目的值的时候,将设定值的代码放在了判断语句里面,导致有些条目会重复。
对list不清楚导致的错误,特标记之。
错误代码如下,设置应用名称下面的几行值,实践证明,他们的赋值需要在判断条件以外:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public View getView( int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stubLog.i(TAG, "getView position = " + position);
ViewHolder viewHolder = new ViewHolder(); if (convertView == null ) {
convertView = mInflater.inflate(R.layout.apps_list_item, null );
viewHolder.cb_installed = (CheckBox) convertView
.findViewById(R.id.cb_checkedApp);
viewHolder.iv_appIcon = (ImageView) convertView
.findViewById(R.id.iv_app_icon);
viewHolder.tv_appName = (TextView) convertView
.findViewById(R.id.tv_app_name);
// 设置应用名称
String mAppName = mData.get(position);
Log.i( "new" , "position " + position + " mAppName = " + mAppName);
viewHolder.iv_appIcon.setImageResource(R.drawable.ic_launcher);
viewHolder.tv_appName.setText(mAppName);
viewHolder.cb_installed.setChecked(isSelected.get(position));
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)convertView.getTag();
} return convertView;
}
|
将这些赋值放在外面,修改后如下,正常了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public View getView( int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stubLog.i(TAG, "getView position = " + position);
ViewHolder viewHolder = new ViewHolder(); if (convertView == null ) {
convertView = mInflater.inflate(R.layout.apps_list_item, null );
viewHolder.cb_installed = (CheckBox) convertView
.findViewById(R.id.cb_checkedApp);
viewHolder.iv_appIcon = (ImageView) convertView
.findViewById(R.id.iv_app_icon);
viewHolder.tv_appName = (TextView) convertView
.findViewById(R.id.tv_app_name);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)convertView.getTag();
}
// 设置应用名称
String mAppName = mData.get(position);
Log.i( "new" , "position " + position + " mAppName = " + mAppName);
viewHolder.iv_appIcon.setImageResource(R.drawable.ic_launcher);
viewHolder.tv_appName.setText(mAppName);
viewHolder.cb_installed.setChecked(isSelected.get(position));
return convertView;
}
|
本文转自 我不会抽烟 51CTO博客,原文链接:http://blog.51cto.com/zhouhongyu1989/1417823,如需转载请自行联系原作者