java - 在 ListView 上获取复选框项目时出现问题

标签 java android

我之前使用 setListAdapter 和 simple_multiple_choice 来制作带有复选框的 ListView 。

然后我获取了尺寸并选择了位置:

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

if (checked.get(i)) 
{
...

但现在我需要对布局进行更多控制,所以我在 xml 上执行 ListView ,如下所示:

<listview>
<checkboxes id=.../>
</listview>

对这些 id 使用 setAdapter。

ListView 已正确填充(和以前一样),问题是,现在 SparseBooleanArray 不起作用。变量“cont”给我 0,“checked”为 null。

要确保“列表”没问题:

int len = list.getCount();

它给了我一个正确的值。

有什么问题吗?

最佳答案

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

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;
}

}

your_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>

关于java - 在 ListView 上获取复选框项目时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7387539/

相关文章:

android - 需要数据连接才能连接到 Google Play 服务

android - 主要 Activity 从堆栈中删除 - 将 uset 状态设置为离线

android - 在 AndroidManifest 中有条件地声明服务

java - 使用 Spring "forward:"前缀导致 StackOverflowError

java - 当方法返回 null 时处理 Map 返回类型

Java EE api邮件发送邮件时出错

javax.命名.CommunicationException : simple bind failed

Java LibGDX Texture 给出空指针异常

android - android.content.ActivityNotFoundException :

java - 需要一个很好的 eclipse java ee 教程来开始 Web 应用程序开发