【问题标题】:How can I associate a value with each checkbox in a ListView?如何将值与 ListView 中的每个复选框关联?
【发布时间】:2013-08-21 19:25:36
【问题描述】:

我使用自定义适配器创建了一个自定义 ListView。我有一个定义每一行的 xml 文件,每一行都有一个在这个 xml 文件中定义的复选框。我的应用程序是一个评判应用程序,其中 ListView 上的每个项目都是一个“任务”,计入一定数量的点。这个想法是,如果任务完成,那么法官点击复选框,该任务的分数被添加到总分中。

不幸的是,我没有办法让这个值与复选框相关联。有没有办法做到这一点?我将发布一些代码,我希望它足以让我了解我的问题。

行的 XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/score_list_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <CheckBox
        android:id="@+id/score_box"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:paddingTop="30dp"
        android:scaleX="2"
        android:scaleY="2"
        android:onClick="chkBoxClicked" />
    <TextView 
        android:id="@+id/subtask"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/score_box"
        android:paddingRight="30dp"
        android:textSize="20sp"/>
    <TextView
        android:id="@+id/max_points"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/subtask" />


</RelativeLayout>

活动中创建列表的方法:

    ...
public void createScoringList() {
    ListView scoreList = (ListView) findViewById(R.id.score_list);
    ListView scoreListPartial = (ListView) findViewById(R.id.score_list_partial);
    ArrayList<ScoringInfo> objList = new ArrayList<ScoringInfo>();  
    ArrayList<ScoringInfo> objListPartial = new ArrayList<ScoringInfo>();
    ScoringInfo scrInfo;

    for (int i = 0; i < subTaskList.size(); i++) {
        subtask_num = subTaskList.get(i).subtask_num;
        max_points = subTaskList.get(i).max_points;
        partial_points_allowed = subTaskList.get(i).partial_points_allowed;
        task_name = subTaskList.get(i).task_name;

        scrInfo = new ScoringInfo();
        scrInfo.setMaxPoints("Max Points: " + max_points);
        scrInfo.setSubtask(task_name);

        if (partial_points_allowed == 1)
            objListPartial.add(scrInfo);
        else
            objList.add(scrInfo);
    }
    scoreList.setAdapter(new ScoreListAdapter(objList , this));
    scoreListPartial.setAdapter(new ScoreListAdapter2(objListPartial, this));

}

如果为了清楚起见需要更多代码,请询问,我会提供。我只是不想用大量我认为可能不必要的代码来溢出问题。

【问题讨论】:

    标签: java android listview checkbox


    【解决方案1】:

    您可以将此值存储在您的模型中(我认为它称为 ScoringInfo),或者您可以使用setTag("score", value) 方法将此值分配给每个复选框,并通过调用getTag("score") 来读取它。

    您可以像这样在适配器类中设置和读取标签。您的适配器应实现OnClickListener 并管理ScoringInfo 项目列表。

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(this)
                .inflate(R.layout.<your_layout>, parent, false);
        }
    
        ScoringInfo item = this.getItem(position);
    
        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox_id);
        checkBox.setTag("score", item.max_points);
        checkBox.setOnClickListener(this);
    }
    
    public void onClick(View view) {
        if (view instanceof CheckBox) {
            boolean checked = ((CheckBox) view).isChecked();
            int score = view.getTag("score");
            // do the rest
        }
    }
    

    【讨论】:

    • 谢谢。我考虑将它添加到 ScoringInfo 对象,并且您的 set/getTag 很适合了解信息,但如果我没有动态添加复选框,我该如何设置标签?该值是已经添加到我的模型中的 max_points,但我不知道如何为每个复选框指定它。我会使用该行的复选框 id 创建一个新的 CheckBox 项并在那里执行吗?
    • 谢谢!这帮助我指出了正确的方向。我不得不做的与您的建议不同的几件事是在内部类中实现一个新的 OnClickListener 而不是使用“this”。此外,Android/Java 强制您在设置标签时使用预编译键,因此我必须在 strings.xml 中创建一个字符串以将键标记为“分数”。
    猜你喜欢
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    • 2021-05-21
    相关资源
    最近更新 更多