gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:29:20 31 4
gpt4 key购买 nike

我想显示其中包含一个 ImageViewTextView 的复选框列表。

+-----------+----------+----------+
| CHECK_BOX | ImageVew | TextView |
+-----------+----------+----------+

这个列表应该是多选的。
如何准备这份 list ?
如何获取所有选中的复选框和对应的TextView值?

有人可以为此提供示例代码吗?

谢谢,
PP.

最佳答案

创建适配器并将其绑定(bind)到 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;
}

关于android - 如何显示带有图标和名称的复选框列表(android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4789949/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com