在Android上滚动时,列表视图位置正在逐渐改变 [英] list view position is getting changed when scrolling on android
问题描述
我的列表视图[文本视图和复选框controlls]有更多的数据。我会选择从列表视图中的项目,并显示在接下来的活动中选定的项目。 我的问题是,对于例如,如果我选择20日和25日的项目列表中查看它会显示下一个活动的一些其他物品。 这就是滚动时,列表视图位置正在逐渐改变。
我按一下该项目的复选框,选择一个项目。在checkbox.setOnChanged监听我写的哪个位置是否选择code。如果让我选择25号项目,滚动列表视图中,getview方法被调用,checkbox.setonChanged方法是改变选择的位置。我打印logcat的最后。
我的编码格式:
公共类ListContact扩展ListActivity {
公共无效的onCreate(包冰柱){
.....
ArrayAdapter<型号:GT;适配器=新MyCustomArrayAdapter(这一点,getModel());
setListAdapter(适配器);
}
....
私人列表<型号:GT; getModel(){
名单<型号:GT;名单=新的ArrayList<型号>();
迭代器<字符串> ITR = constant.selectname.iterator();
而(itr.hasNext()){
list.add(得到(itr.next()的toString()));
}
返回列表;
}
私模的get(String s)将{
返回新模式(S);
}
}
MyCustomArrayAdapter.java:
公共类MyCustomArrayAdapter扩展ArrayAdapter<型号:GT; {
私人最终名单,其中,型号>清单;
私人最终活动范围内;
恒CON;
公共MyCustomArrayAdapter(活动背景下,列表与LT;型号>列表){
超(背景下,R.layout.list_layout,清单);
this.context =背景;
this.list =清单;
}
静态类ViewHolder {
受保护的TextView文本;
保护复选框复选框;
}
@覆盖
公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
// Log.e(getview,getview);
查看查看= NULL;
如果(convertView == NULL){
LayoutInflater充气= context.getLayoutInflater();
鉴于= inflator.inflate(R.layout.list_layout,NULL);
最后ViewHolder viewHolder =新ViewHolder();
viewHolder.text =(TextView中)view.findViewById(R.id.label);
viewHolder.checkbox =(复选框)view.findViewById(R.id.check);
viewHolder.checkbox.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
// CON =新常数();
模型元素=(模型)viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
// Log.e(MyCustomArrayAdapter.java,selectpos数组列表长度+ constant.selectpos.size());
如果(==器isChecked真){
Log.e(复选框价值与地位,element.getName());
Log.e(位置,+位置);
con.selectpos.set(位置,1);
}
其他{
Log.e(位置取消选择,+位置+---------+ element.getName());
con.selectpos.set(位置,0);
}
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(位置));
} 其他 {
鉴于= convertView;
((ViewHolder)view.getTag())checkbox.setTag(list.get(位置))。
}
ViewHolder支架=(ViewHolder)view.getTag();
holder.text.setText(list.get(位置).getName());
holder.checkbox.setChecked(list.get(位置).isSelected());
返回查看;
}
}
logcat的结果:
10月2号至八号:44:28.142:E /复选框值和位置(293):AAAA QQQ
十月2日至8日:44:28.142:E /复选框值和位置(293):移动-123
十月2日至8日:44:28.152:E /位置(293):0
**滚动列表中查看其他项目打印第0位选择,而错误的数据后:**
十月2日至8日:44:31.962:E /位置取消选择(293):0 --------- F212
十月2日至8日:44:31.962:E /位置取消选择(293):主页-232
我解决了这个问题。我添加了
查看视图= NULL;
convertView = NULL;在get观点和意见的其他部分
如果(convertView == NULL){
}
/*其他{
} * /
I have list view [text view and check box controlls] with more data. I will choose the item from list view and display the selected items in the next activity. My problem is, for the example if I choose 20th and 25th item in the list view it will display some other items in the next activity. That is list view position is getting changed when scrolling.
i am choosing an item by clicking the check box on the item. in the checkbox.setOnChanged listener i wrote code for which position is selected or not. if i choose 25th item and scroll the list view, the getview method is called and the checkbox.setonChanged method is change the selected position. I print the logcat at last.
My coding format:
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 result:
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
I solved the problem. i add the
View view = null;
convertView = null; in the get view and comments the else part of
if (convertView == null) {
}
/*else{
} */
这篇关于在Android上滚动时,列表视图位置正在逐渐改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!