创建一个适配器,并将其绑定到ListView。布局看起来很简单(带水平方向的LinearLayout)。您将需要扩展BaseAdapter类。
假设你的类,用于保存数据看起来是这样的:
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;
}
你getView方法会是这个样子:
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;
}