我的问题是,例如,如果我在列表视图中选择第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-12302-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---------F21202-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{ } */总结
以上是内存溢出为你收集整理的列表视图位置在Android上滚动时变化全部内容,希望文章能够帮你解决列表视图位置在Android上滚动时变化所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)