In my application, I have a list. This list includes some texts and a checkbox. In Xml definition of list, If I put android:focusable="true" then I can click it and change it’s status from checked to not checked and vice versa. But I can’t change its status when I click on its row.
If I put android:focusable="false" the story will exchange. If I choose row, I can change the status of checkbox while if I click on it, nothing will change. I need regardless of clicking on row or checkbox, status of chackbox changes. If i click on checkbox it’s status will change but my list doesn’t aware of this changes therefore, onListItemClick() not execute.
I tried to add another element android:enabled="false". But UI is not user friendly. Also, when I click on checkbox its status is not changed.
What is your suggestion?
This is xml code of list:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="6dip"
android:layout_marginLeft="4px"
android:focusable="false" />
.
.
.
and in code: Flag and planId are String Array.
private String[] flag = new String[50];
private String[] planId = new String[50];
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.i("List Clicked:", "******");
//Toggle CheckBox from not selected to selected and vice versa.
if(flag[position] == "true")
flag[position] = "false";
else
flag[position] = "true";
//Keep this CheckBox selected and clear the rest.
for(int i=0; i<position; i++)
flag[i] = "false";
for(int i=position+1; i<flag.length; i++)
flag[i] = "false";
//If checkBox is selected show dialog if cleared don't show
if(flag[position] == "true")
//alertDialog.show();
dialog.show();
//Extracting Plan Id
subscriptionPlanId = planId[position];
}
screen capture:

=======>
Update:
//*********************
// RowModel Class
//*********************
private class RowModel{
TextView title;
TextView description;
CheckBox checkbox;
}
//*********************
// ListAdapter Class
//*********************
private class ListAdapter extends ArrayAdapter<String>{
public ListAdapter(Context c) {
super(c, R.layout.infindo_listformat, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RowModel holder;
View row = convertView;
if(row == null){
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.infindo_listformat, parent, false);
holder = new RowModel();
holder.title = (TextView) row.findViewById(R.id.title);
holder.description = (TextView) row.findViewById(R.id.description);
holder.checkbox = (CheckBox) row.findViewById(R.id.checkbox);
row.setTag(holder);
} else {
holder = (RowModel) row.getTag();
}
holder.title.setText(titels[position]);
holder.description.setText(descriptions[position]);
String s = flag[position];
if(s.equalsIgnoreCase("false"))
holder.checkbox.setChecked(false);
else
holder.checkbox.setChecked(true);
return(row);
}
}
Sorry I answer my question here because above is so messy and confusable. For future reference I put my answer here.
Question: When I click on checkbox in a list, its status will change but onListItemClick() method, doesn’t aware of this change and therefore this method will not run.
Answer: In XML elemnts of checkbox, just set clickable of this item to false. 🙂 the problem is solved. In my case the XML is:
Now, regardless of clicking on row or checkbox, onListItemClick() method will call.