Tôi có chế độ xem danh sách [chế độ xem văn bản và hộp kiểm tra hộp kiểm] với nhiều dữ liệu hơn. Tôi sẽ chọn mục từ chế độ xem danh sách và hiển thị các mục đã chọn trong hoạt động tiếp theo. Vấn đề của tôi là, ví dụ nếu tôi chọn mục 20 và 25 trong chế độ xem danh sách, nó sẽ hiển thị một số mục khác trong hoạt động tiếp theo. Đó là vị trí xem danh sách đang được thay đổi khi cuộn.vị trí xem danh sách đang được thay đổi khi cuộn trên android
tôi đang chọn một mục bằng cách nhấp vào hộp kiểm trên mục. trong trình nghe checkbox.setOnChanged tôi đã viết mã cho vị trí nào được chọn hay không. nếu tôi chọn mục thứ 25 và cuộn chế độ xem danh sách, phương thức getview được gọi và phương thức checkbox.setonChanged sẽ thay đổi vị trí đã chọn. Tôi in bản ghi cuối cùng.
định dạng mã hóa của tôi:
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 kết quả:
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
làm thế nào bạn chọn các mặt hàng. nếu bạn muốn nhận được các mục thứ 25, thông thường bạn làm một cái gì đó như getListView(). getAdapter.getItem (25); – blessenm
tôi đang chọn một mục bằng cách nhấp vào hộp kiểm trên mục danh sách. này gọi phương thức viewHolder.checkbox.setOnCheckedChangeListener (..) trong phương thức getView –