i want to show a listview with check box like
checkbox listitem1
checkbox listitem2
checkbox listitem3
.
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();
}
}
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:
http://geekswithblogs.net/bosuch/archive/2010/12/03/android---creating-a-custom-preferences-activity-screen.aspx
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));
}
}
}
});