Custom Single Choice Listview

Custom Single choice ListView

I've been searching for an answer to this problem all morning, and the most useful thing I've found so far is this article.

While I haven't implemented the solution it suggests yet, it sounds like it's on the right track.

Basically, the single-choice ListView expects the widgets you provide it with to implement the Checkable interface. LinearLayout and others don't. So you need to create a custom layout that inherits LinearLayout (or whatever layout you want to use for your items) and implements the necessary interface.

Custom Listview with CheckBox single selection

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..

Sample Image

Source: here

let me know if you have any trouble regarding this.

Edit: check this useful link: Custom Single choice ListView

How to add a Single choice list to a custom dialog in android?

Try this

String x_id;

final String[] arrayOfStrings = first.toArray(new String[first.size()]);

Dialog dialog = new Dialog(Conf_game.this);
dialog.setContentView(R.layout.dialogs);
dialog.setTitle("SELECT Item ");

final ListView lst = (ListView) dialog.findViewById(R.id.dialog_list);

lst.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, android.R.id.text1,
arrayOfStrings));

lst.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int item, long arg3) {

coursetext.setText(arrayOfStrings[item]);

x_id = arrayOfStrings [item];

System.out.println(" Value of ID OF ABC OF"
+ arrayOfStrings[item] + " IS " + C_id);
dialog.dismiss();
}
});

dialog.show();

use Custome ListView with TextView And CheckBox With Single Selection of CheckBox

use Custom List-View with Text-View And Check-Box With Single Selection of Check Box
So many try then finally i got the Solution I hope it is Useful Code to you All...
This code Help you to create custom List-view with Text-view and Check-box then you select one check-box and if you select another one the first should automatically be Deselected.....Thank You...

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.listviewdemo2.MainActivity" >

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>

activity_custom_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.listviewdemo2.CustomListActivity" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray" >

<TextView
android:id="@+id/textView1"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="4dp"
android:layout_weight="0.39"
android:textAppearance="?android:attr/textAppearanceMedium" />

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="4dp"/>
</LinearLayout> </LinearLayout>

String.xml

<string-array name="name">
<item>Laptop</item>
<item>Mobile</item>
<item>Desktop</item>
<item>TV</item>
<item>Pendrive</item>
<item>Router</item>
<item>Notebook</item>
<item>Tablet</item>
<item>I-pad</item>
<item>Bluetooth</item>
<item>HomeTheator</item>
</string-array>

MainActivity.java

String[] ItemName;
List<Items> rowItem;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rowItem = new ArrayList<Items>();
ItemName = getResources().getStringArray(R.array.name);

for(int i = 0 ; i < ItemName.length ; i++)
{
Items itm = new Items(ItemName[i]);
rowItem.add(itm);
}

list = (ListView) findViewById(R.id.listView1);
CustomListActivity adapter = new CustomListActivity(this, rowItem);
list.setAdapter(adapter);
}

Items.java

public class Items {

private String items;
private boolean selected;

public Items(String items) {

this.items = items;

}

public String getItems() {

return items;
}

public void setItemName(String name) {

this.items = name;
}
public boolean getSelected() {
return selected;
}

public boolean setSelected(Boolean selected) {
return this.selected = selected;
}}

CustomListActivity.java

public class CustomListActivity extends BaseAdapter {

Context context;
List<Items> rowItem;
View listView;
boolean checkState[];

ViewHolder holder;

public CustomListActivity(Context context, List<Items> rowItem) {

this.context = context;
this.rowItem = rowItem;
checkState = new boolean[rowItem.size()];

}

@Override
public int getCount() {

return rowItem.size();
}

@Override
public Object getItem(int position) {

return rowItem.get(position);

}

@Override
public long getItemId(int position) {

return rowItem.indexOf(getItem(position));

}

public class ViewHolder {
TextView tvItemName;
CheckBox check;
}

@Override
public View getView(final int position, View view, ViewGroup parent) {

holder = new ViewHolder();
final Items itm = rowItem.get(position);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

if (view == null) {

listView = new View(context);
listView = layoutInflater.inflate(R.layout.activity_custom_list,
parent, false);

holder.tvItemName = (TextView) listView
.findViewById(R.id.textView1);
holder.check = (CheckBox) listView.findViewById(R.id.checkBox1);
listView.setTag(holder);

} else {
listView = (View) view;
holder = (ViewHolder) listView.getTag();
}

holder.tvItemName.setText(itm.getItems());

holder.check.setChecked(checkState[position]);

holder.check.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

for(int i=0;i<checkState.length;i++)
{
if(i==position)
{
checkState[i]=true;
}
else
{
checkState[i]=false;
}
}
notifyDataSetChanged();

}
});
return listView;
}}

Show The Output :-

Sample Image

Custom ListView with RadioButton single choice

Try like this :

public class CheckedLinearLayout extends RelativeLayout implements
Checkable {

private boolean isChecked;
private List<Checkable> checkableViews;

public CheckedLinearLayout (Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
initialise(attrs);
}

public CheckedLinearLayout (Context context, AttributeSet attrs) {
super(context, attrs);
initialise(attrs);
}

public CheckedLinearLayout (Context context, int checkableId) {
super(context);
initialise(null);
}

public boolean isChecked() {
return isChecked;
}

public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
for (Checkable c : checkableViews) {
c.setChecked(isChecked);
}
}
public void toggle() {
this.isChecked = !this.isChecked;
for (Checkable c : checkableViews) {
c.toggle();
}
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();

final int childCount = this.getChildCount();
for (int i = 0; i < childCount; ++i) {
findCheckableChildren(this.getChildAt(i));
}
}

/**
* Read the custom XML attributes
*/
private void initialise(AttributeSet attrs) {
this.isChecked = false;
this.checkableViews = new ArrayList<Checkable>(5);
}

/**
* Add to our checkable list all the children of the view that implement the
* interface Checkable
*/
private void findCheckableChildren(View v) {
if (v instanceof Checkable) {
this.checkableViews.add((Checkable) v);
}

if (v instanceof ViewGroup) {
final ViewGroup vg = (ViewGroup) v;
final int childCount = vg.getChildCount();
for (int i = 0; i < childCount; ++i) {
findCheckableChildren(vg.getChildAt(i));
}
}
}
}

Custom ListView with single choice selection

I also came across the same problem, I used RelativeLayout

Try like this

public class CheckedLinearLayout extends RelativeLayout implements
Checkable {

private boolean isChecked;
private List<Checkable> checkableViews;

public CheckedLinearLayout (Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
initialise(attrs);
}

public CheckedLinearLayout (Context context, AttributeSet attrs) {
super(context, attrs);
initialise(attrs);
}

public CheckedLinearLayout (Context context, int checkableId) {
super(context);
initialise(null);
}

public boolean isChecked() {
return isChecked;
}

public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
for (Checkable c : checkableViews) {
c.setChecked(isChecked);
}
}
public void toggle() {
this.isChecked = !this.isChecked;
for (Checkable c : checkableViews) {
c.toggle();
}
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();

final int childCount = this.getChildCount();
for (int i = 0; i < childCount; ++i) {
findCheckableChildren(this.getChildAt(i));
}
}

/**
* Read the custom XML attributes
*/
private void initialise(AttributeSet attrs) {
this.isChecked = false;
this.checkableViews = new ArrayList<Checkable>(5);
}

/**
* Add to our checkable list all the children of the view that implement the
* interface Checkable
*/
private void findCheckableChildren(View v) {
if (v instanceof Checkable) {
this.checkableViews.add((Checkable) v);
}

if (v instanceof ViewGroup) {
final ViewGroup vg = (ViewGroup) v;
final int childCount = vg.getChildCount();
for (int i = 0; i < childCount; ++i) {
findCheckableChildren(vg.getChildAt(i));
}
}
}
}

You can go through the below link

http://amitandroid.blogspot.in/2013/03/android-custon-single-choice-lsitview.html

Its having a example on Custom Single Choice ListView



Related Topics



Leave a reply



Submit