简体   繁体   中英

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?

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" .

Here is my code for second.java class that I've done so far.

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

set multiple choice layout or you can also set your custom checkedListView Layout.

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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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