Reputation: 4086
i want to show a listview with check box like
checkbox listitem1
checkbox listitem2
checkbox listitem3
.
.
.
.
If click on any listitem in listview then the corresponding check box checked will be true . I tried below code
<?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="fill_parent">
<CheckBox android:text=""
android:id="@+id/list_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
></CheckBox>
<TextView
android:id="@+id/songname"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="10px"
android:layout_marginLeft="60px"/>
<TextView
android:id="@+id/artist"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="30px"
android:layout_marginLeft="60px"/>
</RelativeLayout>
The class file is
list.setOnItemClickListener(this);
list.setAdapter(new EfficientAdapter(this));
private static class EfficientAdapter extends BaseAdapter
{
private LayoutInflater mInflater;
public EfficientAdapter(Context context)
{
mInflater = LayoutInflater.from(context);
}
public int getCount()
{
return title.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.selectsongs, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.songname);
holder.artist = (TextView) convertView.findViewById(R.id.artist);
holder.check = (CheckBox) convertView.findViewById(R.id.list_checkbox);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(title[position]);
holder.artist.setText(artist[position]);
return convertView;
}
static class ViewHolder
{
TextView title,artist;
CheckBox check;
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
Log.e("name",title[position]);
}
but in this the OnClickItemClickListener on the listview is not working. The checkbox checkable is true when i click on the check box not on listitem in listview. So please tell me how to show the listview with checkbox and also listitem checkbox checkable is true when i click on the listitem.
Best Regards.
Thanks in advance
Upvotes: 1
Views: 4018
Reputation: 5750
After two hours effort ,i got solution for it.
/**
*This is Adapter class
*/
public class CustomListAdapter extends BaseAdapter {
private String[] stringArray;
private Context mContext;
private LayoutInflater inflator;
int checkbox;
/**
*
* @param context
* @param stringArray
*/
public CustomListAdapter(Context context, String[] stringArray)
{
this.mContext=context;
this.stringArray=stringArray;
this.inflator= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return stringArray.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)
{
final MainListHolder mHolder;
View v = convertView;
if (convertView == null)
{
mHolder = new MainListHolder();
v = inflator.inflate(android.R.layout.simple_list_item_multiple_choice, null);
mHolder.txt=(CheckedTextView) v.findViewById(android.R.id.text1);
v.setTag(mHolder);
}
else
{
mHolder = (MainListHolder) v.getTag();
}
mHolder.txt.setText(stringArray[position]);
mHolder.txt.setTextSize(12);
mHolder.txt.setTextColor(Color.YELLOW);
/**
* When checkbox image is set By setImageFromResourceCheckBox(int id) method...Otherwise it takes default
*/
if(checkbox!=0)
mHolder.txt.setCheckMarkDrawable(R.drawable.android_button);
mHolder.txt.setPadding(5, 5, 5, 5);
return v;
}
class MainListHolder
{
private CheckedTextView txt;
}
/***
* Setting Image for Checkbox
* @param id
*
*/
public void setImageFromResourceCheckBox(int id)
{
this.checkbox=id;
}
}
And Activity class should be like this.
public class MyActivity extends ListActivity implements OnItemClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CustomListAdapter c=new CustomListAdapter(this,GENRES);
// c.setImageForCheckBox(R.drawable.android_button);//Image for checkbox
setListAdapter(c);
final ListView listView = getListView();
listView.setItemsCanFocus(false);
// listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);// For single mOde
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setItemChecked(2, true);
listView.setOnItemClickListener(this);
}
private static String[] GENRES = new String[] {
"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
"Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
};
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int arg2, long arg3)
{
SparseBooleanArray sp=getListView().getCheckedItemPositions();
String str="";
for(int i=0;i<sp.size();i++)
{
str+=GENRES[sp.keyAt(i)]+",";
}
Toast.makeText(this, ""+str, Toast.LENGTH_SHORT).show();
}
}
Upvotes: 0
Reputation: 1648
I have the same problem. i have just fixed it 2 days before:
In the : public View getView
cb =(CheckBox)row.findViewById(R.id.CheckBox01);
cb.setChecked(etat[position]);
final int xt=position;
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
if(arg0.isChecked())
{
etat[xt]=true;
//Update the state of Checkbox to the: "tabEtat"
Etat.getInstance().setAddIdEtat(String.valueOf(xt));
}
}
else
{
//Update the state of Checkbox to the : "tabEtat"
Etat.getInstance().setDeleteIdEtat(String.valueOf(xt));
}
}
}
});
Upvotes: 0
Reputation: 6195
Its better to accomplish the list using a ChechBoxPreference.
The main advantage of using preference is that you don't need to write code to save the value and you can easily get the value in any activity. The value is stored in the android preference as a key-pair value. You can refer the value using the 'KeyName'.
The following link will help you to get an idea about this:
Upvotes: 3