1

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

    }
}

3 Answers 3

2

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);
                }
            }
        });
Sign up to request clarification or add additional context in comments.

Comments

0

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

5 Comments

thank you but there's still error on this line listview.setOnItemClickListener((adapterView, view, pos, id) -> {
@ImahAmmran I've updated it. Should works now. I've added some code as you need to manually change the checked state.
android studio cannot resolve the adapterView, view, pos, id. may i know how to declare those variable?
Remove AdapterView and see what Android Studio suggests. You just need an OnItemClickListener
@ImahAmmran This is this interface android.widget.AdapterView.OnItemClickListener
-1

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

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.