I have a Custom ListView
that contains checkbox
and `TextView' per row. I want to set it up so that only one checkbox can be selected at a time.... Thanks in Advance .
Asked
Active
Viewed 4,488 times
1

Amresh
- 2,098
- 16
- 20
-
2You would need to uncheck the other boxes manually. – Merlevede Mar 03 '14 at 08:07
-
Use Single Choice Mode for ListView. **setChoiceMode(ListView.CHOICE_MODE_SINGLE);** – Piyush Mar 03 '14 at 08:08
-
Have a Java model for your rows which holds the checked status. Check or unCheck it if relevant row is checked. Also uncheck all other rows. Then notifyDataSetChanged. – Devrim Mar 03 '14 at 08:09
-
Code snippet would be really helpful .... thnxs – Amresh Mar 03 '14 at 08:12
2 Answers
1
if you are using this default class
mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, mData);
then you can use
setChoiceMode(ListView.CHOICE_MODE_SINGLE);
or if you are using any custom class then set this as follow.
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
holder.mCheckBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
holder.mCheckBox.setTag(position);
holder.mCheckBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// do your stuff here.
}
});
this will allow you to click only single check-box,

InnocentKiller
- 5,234
- 7
- 36
- 84
-
Which code you are using `simple_list_item_multiple_choice` or custom adapter. – InnocentKiller Mar 04 '14 at 06:45
-
Okay, or you have created your own layout or you are using android default XML files. – InnocentKiller Mar 04 '14 at 06:51
-
Then Use second method which i have defined in above code, what's the problem you are facing? – InnocentKiller Mar 04 '14 at 06:55
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48927/discussion-between-ryderz-and-innocentkiller) – Amresh Mar 04 '14 at 06:55
0
Here is what i would do if i need to select only single item at a time.
Home.java (Activity)
package com.lvcheck.activities;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class Home extends Activity
{
private ListView lvCheckBox;
private Button btnCheckAll, btnClearALl;
private String[] arr = {"One", "Two", "Three", "Four", "Five", "Six"};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnCheckAll = (Button)findViewById(R.id.btnCheckAll);
btnClearALl = (Button)findViewById(R.id.btnClearAll);
lvCheckBox = (ListView)findViewById(R.id.lvCheckBox);
lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvCheckBox.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, arr));
btnCheckAll.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
for(int i=0 ; i < lvCheckBox.getAdapter().getCount(); i++)
{
lvCheckBox.setItemChecked(i, true);
}
}
});
btnClearALl.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
for(int i=0 ; i < lvCheckBox.getAdapter().getCount(); i++)
{
lvCheckBox.setItemChecked(i, false);
}
}
});
}
}
and my (main.xml) XML file should like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:text="Check All"
android:layout_marginRight="7dip"
android:id="@+id/btnCheckAll"
android:layout_height="wrap_content">
</Button>
<Button
android:layout_width="wrap_content"
android:text="Clear All"
android:id="@+id/btnClearAll"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<ListView
android:layout_width="fill_parent"
android:id="@+id/lvCheckBox"
android:fadingEdge="none"
android:cacheColorHint="@null"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
so the output will be like this way..
Source: here
let me know if you have any trouble regarding this.
Edit: check this useful link: Custom Single choice ListView

Community
- 1
- 1

Kanaiya Katarmal
- 5,974
- 4
- 30
- 56