Reputation: 53600
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);
}
}
Upvotes: 1
Views: 1383
Reputation: 53600
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:
<Check
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"
android:clickable="false" />
Now, regardless of clicking on row or checkbox, onListItemClick() method will call.
Upvotes: 1
Reputation: 53657
You need to write code to check or uncheck the checkbox by using (checkbox.setChecked())
in onListItemClick
method
your code for onListItemClick will be some thing like following
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
// super.onListItemClick(l, v, position, id);
RelativeLayout rl = (RelativeLayout) v;
for (int i = 0; i < rl.getChildCount(); i++) {
if ((rl.getChildAt(i)) instanceof CheckBox) {
CheckBox cb = (CheckBox) rl.getChildAt(i);
boolean check = cb.isChecked();
cb.setChecked(!check);
break;
}
}
// System.out.println(rl.getChildCount() + "...v..." + rl + "...." + id);
// boolean check = cb.isChecked();
// cb.setChecked(!check);
}
MyListVIew.java
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MyListView extends ListActivity {
AutoCompleteTextView myAutoComplete;
String item[] = { "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// getIntent().getParcelableExtra("obj");
// b.putSerializable("mybean", bean);
// i.putExtra("obj", b);
Bundle b = getIntent().getBundleExtra("obj");
MyBean dData = (MyBean) b.getSerializable("mybean");
String[][] str = dData.get2DArray();
setListAdapter(new MyAdapter(this));
}
class MyAdapter extends BaseAdapter {
Context _context;
TextView tv;
public MyAdapter(Context context) {
_context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return item.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return item[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View row, ViewGroup parent) {
// TODO Auto-generated method stub
if (row == null) {
LayoutInflater inflater = (LayoutInflater) _context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.mylist, parent, false);
}
tv = (TextView) row.findViewById(R.id.textview);
// Set the Day GridCell
tv.setText(item[position]);
return row;
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
// super.onListItemClick(l, v, position, id);
RelativeLayout rl = (RelativeLayout) v;
for (int i = 0; i < rl.getChildCount(); i++) {
if ((rl.getChildAt(i)) instanceof CheckBox) {
CheckBox cb = (CheckBox) rl.getChildAt(i);
boolean check = cb.isChecked();
cb.setChecked(!check);
break;
}
}
// System.out.println(rl.getChildCount() + "...v..." + rl + "...." + id);
// boolean check = cb.isChecked();
// cb.setChecked(!check);
}
}
mylist.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false" />
</RelativeLayout>
Upvotes: 0