How to display list of check box with icon and name (android)
I want to display list of check boxes with one ImageView
and TextView
in it.
+-----------+----------+----------+
| CHECK_BOX | ImageVew | TextView |
+-----------+----------+----------+
This list should be multiple selectable.
How to prepare This list? How to get all selected check boxes and corresponding TextView value? 开发者_高级运维Can some one please provide sample code for this.
Thanks,
PP.Create an adapter and bind it to a listview. The layout seems simple enough (LinearLayout with horizontal orientation). You will need to extend the BaseAdapter class.
Assume your class that holds the data looks something like this:
public class BasicClass {
// Holds the ID for the row
public int ID;
// Holds the resource ID for the imageview
public int ImageID;
// Holds the text for the textview
public String Text;
}
Your getView method will look something like this:
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
final BasicClass e = (BasicClass) getItem(position);
if (v == null) {
LayoutInflater vi = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.checkbox_list_item, null);
}
TextView txtTitle = (TextView)v.findViewById(R.id.checkbox_list_item_txtTitle);
ImageView img = (ImageView)v.findViewById(R.id.checkbox_list_item_img);
final CheckBox chSelected = (CheckBox)v.findViewById(R.id.checkbox_list_item_cbSelected);
txtTitle.setText(e.Text);
img.setBackgroundDrawable(mContext.getResources().getDrawable(e.ImageID))
chSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Logic comes here to add and remove the selected items to a ArrayList<BasicCLass>
}
});
return v;
}
Better to use Listview Custom adapters by extending BaseAdapter class.
Refer this links
For multiple selectable refer this
精彩评论