Java 获取ListView上的复选框项时出现问题

Java 获取ListView上的复选框项时出现问题,java,android,Java,Android,我在setListAdapter之前使用了简单的多选项来创建带有复选框的listview 然后我做了一些事情来确定尺寸和选择位置: SparseBooleanArray checked = list.getCheckedItemPositions(); cont = list.getCheckedItemPositions().size(); if (checked.get(i)) { ... 但是现在我需要对布局进行更多的控制,所以我在xml上执行listview,类似于: <li

我在setListAdapter之前使用了简单的多选项来创建带有复选框的listview

然后我做了一些事情来确定尺寸和选择位置:

SparseBooleanArray checked = list.getCheckedItemPositions();
cont = list.getCheckedItemPositions().size();

if (checked.get(i)) 
{
...
但是现在我需要对布局进行更多的控制,所以我在xml上执行listview,类似于:

<listview>
<checkboxes id=.../>
</listview>
它给了我一个正确的值


有什么问题吗?

您需要实现自定义列表适配器

public class YourAdapterName extends BaseAdapter{

private Context mContext;
private DataType mValuestoShow;//Use your DataType to pass values to adapter. 

/**
 * Constructor to be called to initialize adapter with values.
 * @param context
 * @param vector
 */
public YourAdapterName(Context context, DataType data){
    mContext = context;
    mValuestoShow = vector;
}

public int getCount() {
    if(null != mValuestoShow){
        return mValuestoShow.size();
    }
    return 0;
}

public Object getItem(int position) {
    if(position < mValuestoShow.size())
        return  mValuestoShow.get(position);
    else
        return null;
}

public long getItemId(int position) {
    return 0;
}



public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder ;
    if(convertView == null){
        LayoutInflater li =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(R.layout.your_layout, null);
        holder = new ViewHolder();
        holder.txt_name = (TextView) convertView.findViewById(R.id.name_txt);
        holder.checkBox = (Checkbox) convertView.findViewById(R.id.checkbox);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txt_name.setText(getItem(position).toString());
    holder.checkBox    // Do your task with checkbox.
    return convertView;
}

class ViewHolder {
    TextView txt_name;
    Checkbox checkBox;
}

}
公共类YourAdapterName扩展BaseAdapter{
私有上下文;
私有数据类型mValuestoShow;//使用您的数据类型将值传递给适配器。
/**
*要调用的构造函数以使用值初始化适配器。
*@param上下文
*@param向量
*/
公共YourAdapterName(上下文、数据类型数据){
mContext=上下文;
mValuestoShow=向量;
}
public int getCount(){
if(null!=mValuestoShow){
返回mValuestoShow.size();
}
返回0;
}
公共对象getItem(int位置){
if(位置
您的_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width = "fill_parent"
    android:layout_height = "wrap_content"
    android:padding = "10dp" >

<TextView
    android:id = "@+id/txt_type1"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content" />

<CheckBox
    android:id = "@+id/checkbox"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:layout_alignParentRight="true" />

</RelativeLayout>


我将尝试了解一些基于自定义适配器的解决方案。您应该根据您的需要对其进行修改……我发布了一个通用自定义列表适配器……您能看到我的问题吗?您能看到@Vinet吗
我希望您不介意回答这些问题,我将非常感激
(1)之后我应该使用list.setAdapter(YourAdapterName)之类的吗
(2)当从未使用矢量时,为什么要放置矢量
(3)该功能已启用目的是什么?谢谢你给我一些时间,我会在测试后根据你的要求发布代码。。。。。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width = "fill_parent"
    android:layout_height = "wrap_content"
    android:padding = "10dp" >

<TextView
    android:id = "@+id/txt_type1"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content" />

<CheckBox
    android:id = "@+id/checkbox"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:layout_alignParentRight="true" />

</RelativeLayout>