android - why disabled item is still checkable in listview -
i want listview items , of them checked. if first item not checked other item not enabled, of course, should not checkable @ time. extend listview mylistview this:
public class mylistview extends listview { public mylistview(context context) { super(context); init(); } public mylistview(context context, attributeset attrs) { super(context, attrs); init(); } public mylistview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(); } private void init() { setadapter(new arrayadapter<string>(getcontext(), android.r.layout.simple_list_item_multiple_choice,array )); setchoicemode(choice_mode_multiple); setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { sparsebooleanarray posarray = getcheckeditempositions(); boolean bchecked = false; if( null != posarray ) { for( int = 0; < posarray.size(); i++ ) { int nidx = posarray.keyat(i); if( nidx == 0 ) { bchecked = posarray.get(nidx); break; } } } if( position == 0 ) { bchecked = !bchecked; for( int = 1; < array.length; i++ ) { getchildat(i).setenabled(!bchecked); } } } }); } private string[] array = { "test1", "test2", "test3", "test4" };
}
and got problem: disabled items still checkable, there wrong? how should it? thanks.
actually,the problem done pawan yadav's suggestion. bellow code, wish meet same problem.
activity_main.xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/parentlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > </relativelayout>
in activity's oncreate method:
listview listview = new listview(this); string [] datas = new string[36]; // generate data here final boolean [] status = new boolean[36]; // generate check status here final myadapter adapter = new myadapter(datas, status); listview.setadapter(adapter ); listview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { if( position== 0 || status[0] ) { status[position] = !status[positioin]; adapter.notifydatasetchanged(); } } }); relativelayout parentlayout = (relativelayout)findviewbyid(r.id.parentlayout); parentlayout.addview(listview);
listview_checkedtextview.xml:
<?xml version="1.0" encoding="utf-8"?> <checkedtextview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="?android:attr/listpreferreditemheightsmall" android:textappearance="?android:attr/textappearancelistitemsmall" android:gravity="center_vertical" android:checkmark="?android:attr/listchoiceindicatormultiple" android:paddingstart="?android:attr/listpreferreditempaddingstart" android:paddingend="?android:attr/listpreferreditempaddingend" />
activity's inner class:
private class myadapter extends baseadapter{ public myadapter( final string [] datas, final boolean [] status ) { this.datas = datas; this.status = status; inflater = layoutinflater.from(mainactivity.this); } @override public int getcount() { return datas.length; } @override public object getitem(int position) { return position; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { if( null == convertview ) { convertview = inflater.inflate(r.layout.listview_checkedtextview, null); convertview = convertview.findviewbyid(r.id.text1); } checkedtextview checkedtextview = (checkedtextview) convertview; checkedtextview.settext(datas[position]); checkedtextview.setchecked(status[position]); checkedtextview.setenabled( position == 0 ? true : status[0]); return convertview; } private string [] datas; private boolean [] status; private layoutinflater inflater; }
you need call setclickable(boolean)
make not clickable instead of setenabled(boolean)
Comments
Post a Comment