我有列表视图[文本视图和复选框控制]与更多的数据.我将从列表视图中选择该项目,并在下一个活动中显示所选项目.
我的问题是,例如,如果我在列表视图中选择第20和第25项,它将在下一个活动中显示一些其他项目.这是列表视图位置在滚动时变化.
我的问题是,例如,如果我在列表视图中选择第20和第25项,它将在下一个活动中显示一些其他项目.这是列表视图位置在滚动时变化.
我通过点击项目上的复选框来选择一个项目.在checkBox.setOnChanged监听器中,我编写了选择或不选择位置的代码.如果我选择第25项并滚动列表视图,则调用getview方法,并且checkBox.setonChanged方法更改所选位置.我最后打印logcat.
我的编码格式:
public class ListContact extends ListActivity { public void onCreate(Bundle icicle){ ..... ArrayAdapter<Model> adapter = new MyCustomArrayAdapter(this,getModel()); setListAdapter(adapter); } .... private List<Model> getModel() { List<Model> list = new ArrayList<Model>(); Iterator<String> itr = constant.selectname.iterator(); while (itr.hasNext()) { list.add(get(itr.next().toString())); } return list; } private Model get(String s) { return new Model(s); } }
MyCustomArrayAdapter.java:
public class MyCustomArrayAdapter extends ArrayAdapter<Model> { private final List<Model> list; private final Activity context; constant con ; public MyCustomArrayAdapter(Activity context,List<Model> list) { super(context,R.layout.list_layout,list); this.context = context; this.list = list; } static class ViewHolder { protected TextView text; protected CheckBox checkBox; } @Override public View getView(final int position,View convertView,ViewGroup parent) { // Log.e("getview","getview"); View view = null; if (convertView == null) { LayoutInflater inflator = context.getLayoutInflater(); view = inflator.inflate(R.layout.list_layout,null); final ViewHolder viewHolder = new ViewHolder(); viewHolder.text = (TextView) view.findViewById(R.id.label); viewHolder.checkBox = (CheckBox) view.findViewById(R.id.check); viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { // con = new constant(); Model element = (Model) viewHolder.checkBox.getTag(); element.setSelected(buttonView.isChecked()); // Log.e("MyCustomArrayAdapter.java","selectpos array list length"+constant.selectpos.size()); if(isChecked==true){ Log.e("check Box value and position ",element.getName()); Log.e("position",""+position); con.selectpos.set(position,1); } else{ Log.e("position unselect",""+position +"---------"+ element.getName()); con.selectpos.set(position,0); } } }); view.setTag(viewHolder); viewHolder.checkBox.setTag(list.get(position)); } else { view = convertView; ((ViewHolder) view.getTag()).checkBox.setTag(list.get(position)); } ViewHolder holder = (ViewHolder) view.getTag(); holder.text.setText(list.get(position).getName()); holder.checkBox.setChecked(list.get(position).isSelected()); return view; } }
logcat结果:
02-08 10:44:28.142: E/check Box value and position(293): AAAA Qqq 02-08 10:44:28.142: E/check Box value and position(293): Mobile-123 02-08 10:44:28.152: E/position(293): 0 **after scrolling the list view some other item print 0th position unselected and wrong data:** 02-08 10:44:31.962: E/position unselect(293): 0---------F212 02-08 10:44:31.962: E/position unselect(293): Home-232
解决方法
我解决了这个问题.我加了
View view = null; convertView = null; //in the get view and comments the else part of if (convertView == null) { } /*else{ } */