vendredi 4 août 2017

How to restrict the checked checkbox in list view Android studio

I'm very new to Android Studio and Android apps development. I'm trying to limit the selected check box on my list view to 3 selected items only and I want to disable any click on other list if the selected box is already 3. How to do that?

I've been trying to solved it based on majority answers on the 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--;
                    }
                }
            }
        });

    }
}




Aucun commentaire:

Enregistrer un commentaire