Reputation: 2108
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 .
Upvotes: 1
Views: 4495
Reputation: 6108
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
Upvotes: 0
Reputation: 5234
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,
Upvotes: 1