I have a listview that each item in listview contains a checkbox. My problem is i want to set only one checkbox selected at a time. Another problem is when i select a checkbox in order 3, then when i scroll up and back, checkbox is checked in different order 5 or 8 randomly Could you help me solve this issue
Asked
Active
Viewed 616 times
2
-
Thisis because of the way in which listview recycling mechanism works. You need to post relevant adapter code – Raghunandan Jul 27 '15 at 05:39
-
Probably what you want is a radio button rather than checkbox. Also, to get better response, post the relevant code. – Ganesh Kumar Jul 27 '15 at 05:39
-
take a boolean in listview object for check and uncheck and change its value if checked true and false if uncheck and in place of taking android checkbox, use a checkbox image let's say checked.png and unchecked.png use it according to check status. – Sandeep Tiwari Jul 27 '15 at 05:43
-
I know, every item when i scroll up and back, listview is redrawn. Do you know where i can get clear concept about it? – Farruh Habibullaev Jul 27 '15 at 05:44
2 Answers
0
Set your listview to :
lvSelectLocation.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
ArrayAdapter<String> adptr = new ArrayAdapter<String>(Location.this, android.R.layout.simple_list_item_single_choice, locationList);
Handle onItemClick only :
lvSelectLocation.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Selected = arg0.getItemAtPosition(arg2).toString();
}
});
Android automatically recycle the listview view so, selected option is recycle when you scroll. For that you have to set the selected list item to your ArrayList.

SANAT
- 8,489
- 55
- 66
-
-
-
check this links : http://joshskeen.com/building-a-radiogroup-recyclerview/ and https://bignerdranch.github.io/recyclerview-multiselect/ – SANAT Jul 27 '15 at 06:07
0
You can use the API demo to understand the best way for this implementation.
Have a look on this.
There is a CHOCIE_MODE
option with the list view you can set it to CHOCIE_MODE_SINGLE
. Simple code is like this-
public class List10 extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, GENRES));
final ListView listView = getListView();
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
private static final String[] GENRES = new String[] {
"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
"Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
};
}

Sanjeet A
- 5,171
- 3
- 23
- 40