2011-01-25 92 views
1

我想显示其中包含一个ImageViewTextView的复选框列表。如何显示带有图标和名称的复选框列表(安卓)

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

此列表应该是可多选的。
如何准备此清单?
如何获取所有选中的复选框和相应的TextView值?

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

谢谢,
PP。

回答

1

创建一个适配器,并将其绑定到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; 
} 
1

通过扩展BaseAdapter类更好地使用Listview自定义适配器。
请参阅本links
对于多个可选择的参考this