简体   繁体   English

如何限制ListView中选中的复选框?

[英]How to restrict the checked checkbox in a ListView?

I'm trying to limit the selected check box on my list view to just 3 selected items and I want to disable any click on the other list if the selected box is already 3. How to do that?我正在尝试将列表视图上的选定复选框限制为 3 个选定项目,如果选定的框已经是 3 个,我想禁用对另一个列表的任何单击。如何做到这一点?

I've been trying to solve it based on the answers on quite similar questions.我一直试图根据非常相似问题的答案来解决它。 But it still didn't work.但它仍然没有奏效。 All of the code will become error of所有的代码都会变成错误
cannot resolve method "setOnItemSelectedListener" .无法解析方法“setOnItemSelectedListener”

Here is my code for second.java class that I've done so far.这是我迄今为止完成的second.java类的代码。

package com.example.imah.uid;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;

public class second extends AppCompatActivity {

    ListView listview ;
    String[] pStyle = new String[] {
            "Fashion",
            "HDR",
            "Hi Speed",
            "Landscape",
            "Portrait",
            "Street",
            "Wedding"

    };

    SparseBooleanArray sparseBooleanArray ;
    private int numberOfCheckboxesChecked;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        listview = (ListView)findViewById(R.id.listView2);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (second.this,
                        android.R.layout.simple_list_item_multiple_choice,
                        android.R.id.text1, pStyle );

        listview.setAdapter(adapter);

        listview.setOnItemSelectedListener(new onItemSelectedListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked && numberOfCheckboxesChecked >= 4) {
                    checkbox1.setChecked(false);
                } else {

                    if (isChecked) {
                        numberOfCheckboxesChecked++;
                    } else {
                        numberOfCheckboxesChecked--;
                    }
                }
            }
        });

    }
}

I was working on a project and came to this issue but what I encountered with first answer is that if you reach the maximum number of checkbox and than select any other checkbox 2 or 3 time it lets you select it.我正在处理一个项目并遇到了这个问题,但我遇到的第一个答案是,如果您达到最大数量的复选框,然后选择任何其他复选框 2 或 3 次,它会让您选择它。 I don't know why it was happening but then I came to know that listView it self has checked method and is working properly without any bugs.我不知道为什么会这样,但后来我知道 listView 它自己已经检查了方法并且正常工作没有任何错误。

set multiple choice layout or you can also set your custom checkedListView Layout.设置多选布局,或者您也可以设置自定义的checkedListView 布局。

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_multiple_choice, SODA);
ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selected = (String) ListView.getItemAtPosition(position);
                if (arrayList.contains(selected)) {
                    arrayList.remove(selected);
            
                    ListView.setItemChecked(position, false);
                } else if (arrayList.size() < MAX_SELECTABLE) {
                    arrayList.add(selected);
                    ListView.setItemChecked(position, true);
                } else {
                    Toast.makeText(UserSelectionActivity.this, "You can\'t select more than " + MAX_SELECTABLE + " items", Toast.LENGTH_SHORT).show();
                    ListView.setItemChecked(position, false);
                }
            }
        });

Try this:试试这个:

public class Second extends AppCompatActivity {

    ListView listview;
    String[] pStyle = new String[]{
            "Fashion",
            "HDR",
            "Hi Speed",
            "Landscape",
            "Portrait",
            "Street",
            "Wedding"

    };
    private static final int MAX_SELECTABLE = 3;
    List<String> mSelected = new ArrayList<>();

    SparseBooleanArray sparseBooleanArray;
    private int numberOfCheckboxesChecked;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        listview = (ListView) findViewById(R.id.listView2);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (this,
                        android.R.layout.simple_list_item_multiple_choice,
                        android.R.id.text1, pStyle);

        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                CheckedTextView ctv = (CheckedTextView) view;
                if (ctv.isChecked()) {
                    mSelected.remove(pStyle[pos]);
                    ctv.setChecked(false);
                } else if (mSelected.size() < MAX_SELECTABLE) {
                    mSelected.add(pStyle[pos]);
                    ctv.setChecked(true);
                } else {
                    Toast.makeText(Second.this, "You can\'t select more than " + MAX_SELECTABLE + " items", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

use in this way:以这种方式使用:

        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (second.this,
                        android.R.layout.simple_list_item_multiple_choice,
                        android.R.id.text1, pStyle );

        listview.setAdapter(adapter);

        listview.setOnItemSelectedListener(new onItemSelectedListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if (isChecked) {
if(numberOfCheckboxesChecked=>4){
checkbox1.setChecked(false);
}
else{
numberOfCheckboxesChecked++;
}


                    } else {
                        numberOfCheckboxesChecked--;
                    }
                }
            }
        });

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM