I'm pretty beginner,so take me easy. I have two listview parallel , here only listViewSelectFile he can makes check's. The problem is when i checked a item from this listview !!! Example : If i select first item from listview , next item's until i move the scroll from listview are unchecked. If on one page they can be maxim 9 item's to see (first item is check next 8 is unchecked) and the 10 item is checked then next 8 item's are unchecked then the item 19 is checked and so far one. I tested and with the phone horizontal and it's the same problem.I think the problem appear is when i move the scroll next item's(CheckTextView ID ) he give the id 0 , and he multiply my CheckTextView ID . How can i solve this ? Any example ? Thank you !!!!! If you don't get this problem maybe you can give me a example how to make 2 listview paralels with textviewcheck.
PopulateListView();
The whole code from bot it's in PopulateListView():
adapterImageFileName = new ItemsAdapter(
screen3_select_from_lists.this, mImageFilenames);
listViewSelectFile.setAdapter(adapterImageFileName);
listViewSelectFile
.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
// When clicked, show a toast with the TextView text
try {
LinearLayout item=(LinearLayout) view;
CheckedTextView tv = (CheckedTextView)item.findViewById(R.id.textView1);
toggle(tv);
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
e.getMessage(), Toast.LENGTH_LONG)
.show();
}
}
});
Toggle function :
public void toggle(CheckedTextView v) {
if (v.isChecked()) {
v.setChecked(false);
} else {
v.setChecked(true);
}
}
MyAdapter :
private class ItemsAdapter extends BaseAdapter {
String[] items;
public ItemsAdapter(Context context, String[] mImageFilenames) {
// TODO Auto-generated constructor stub
this.items = mImageFilenames;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_view_rows, null);
}
CheckedTextView post = (CheckedTextView)v
.findViewById(R.id.textView1);
post.setText(items[position]);
return v;
}
}
and my xml, 2 parallel listview's :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" >
<ListView
android:id="@+id/listviewSelectStudents"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:choiceMode="multipleChoice"
>
</ListView>
<ListView
android:id="@+id/listviewSelectFiles"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:choiceMode="multipleChoice">
</ListView>
</LinearLayout>
list_view_rows.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckedTextView
android:id="@+id/textView1"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:onClick="toggle" />
</LinearLayout>
Your problem is that your view is being reused by Android in order to optimize resources. I managed to resolve this problem creating my own adapter, and saving inside my adapter the IDs I have checked. Every time my getView
method gets called I clear the check objet and then I see if in my checked-row structure this row / ID has been checked by the user.
Then if I rotate the screen I ask to the adapter of an array of checked items in my onSaveInstanceState
which I reuse in my onRestoreInstanceState
method.
Hope it helps.