如何显示带有图标和名称的复选框列表(android)

如何显示带有图标和名称的复选框列表(android),android,Android,我想显示包含一个ImageView和TextView的复选框列表 +-----------+----------+----------+ | CHECK_BOX | ImageVew | TextView | +-----------+----------+----------+ 此列表应该是多个可选的。 如何准备此列表? 如何获取所有选中的复选框和相应的TextView值 能不能请一些人提供这个示例代码 谢谢, PP.通过扩展BaseAdapter类更好地使用Listview自定义适配器。

我想显示包含一个
ImageView
TextView
的复选框列表

+-----------+----------+----------+
| CHECK_BOX | ImageVew | TextView |
+-----------+----------+----------+
此列表应该是多个可选的。
如何准备此列表?
如何获取所有选中的复选框和相应的TextView值

能不能请一些人提供这个示例代码

谢谢,

PP.

通过扩展BaseAdapter类更好地使用Listview自定义适配器。
请参阅此

有关多个可选择的选项,请参阅创建适配器并将其绑定到listview。布局似乎足够简单(具有水平方向的线性布局)。您将需要扩展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;
    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;
}
}

您的getView方法如下所示:

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;
    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;
}
public View getView(最终int位置、视图转换视图、视图组父视图){
视图v=转换视图;
最终基本类e=(基本类)获取项目(位置);
如果(v==null){
LayoutFlater vi=(LayoutFlater)mContext
.getSystemService(上下文布局\充气机\服务);
v=vi.充气(R.layout.checkbox\u list\u项,空);
}
TextView txtTitle=(TextView)v.findViewById(R.id.checkbox\u list\u item\u txtTitle);
ImageView img=(ImageView)v.findViewById(R.id.checkbox\u list\u item\u img);
最终复选框chSelected=(复选框)v.findViewById(R.id.CheckBox\u list\u item\u cbSelected);
txtTitle.setText(e.Text);
img.setBackgroundDrawable(mContext.getResources().getDrawable(例如ImageID))
chSelected.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
//这里的逻辑用于向ArrayList添加和删除选定项
}
});
返回v;
}