简体   繁体   中英

Adding OnClickListener to Multiple Choice ListView

My app has a Multiple Choice ListView where multiple elements may be selected. I have created a SparseBooleanArray to store whether or not the different elements were clicked, but am unsure how to implement an OnClickListener to this.

public class ListOfMajors extends Activity {

    public static boolean aerospace, agricultural, biomed, chem, civil, computer, electrical, physics, environment, industrial, materials, mechanical;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.majorslist);
        ListView mylist = (ListView) findViewById(R.id.majorslist);
        String[] list={"Aerospace Engineering","Agricultural Engineering","Biomedical Engineering","Chemical Engineering","Civil Engineering",
                        "Computer Engineering","Electrical Engineering","Engineering Physics","Environmental Engineering","Industrial Engineering",
                        "Materials Engineering","Mechanical Engineering"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(ListOfMajors.this,android.R.layout.simple_list_item_multiple_choice,list);
        mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        mylist.setAdapter(adapter);

        SparseBooleanArray checkedItems = mylist.getCheckedItemPositions();
        if (checkedItems!= null){
            for(int i=0; i<checkedItems.size();i++){
                if(checkedItems.valueAt(0)){
                    aerospace = true;
                }
                if(checkedItems.valueAt(1)){
                    agricultural = true;
                }
                if(checkedItems.valueAt(2)){
                    biomed = true;
                }
                if(checkedItems.valueAt(3)){
                    chem = true;
                }
                if(checkedItems.valueAt(4)){
                    civil = true;
                }
                if(checkedItems.valueAt(5)){
                    computer = true;
                }
                if(checkedItems.valueAt(6)){
                    electrical = true;
                }
                if(checkedItems.valueAt(7)){
                    physics = true;
                }
                if(checkedItems.valueAt(8)){
                    environment = true;
                }
                if(checkedItems.valueAt(9)){
                    industrial = true;
                }
                if(checkedItems.valueAt(10)){
                    materials = true;
                }
                if(checkedItems.valueAt(11)){
                    mechanical = true;
                }
            }
        }

    }


}

I know there are quite a few questions on here with a similar question, but I am still uncertain what to do for my case. What should I do?

Please remove that horrible for loop like this :-)

boolean[] mItemState;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.majorslist);
    ListView mylist = (ListView) findViewById(R.id.majorslist);
    final String[] list={"Aerospace Engineering","Agricultural Engineering",
            "Biomedical Engineering","Chemical Engineering","Civil Engineering",
            "Computer Engineering","Electrical Engineering","Engineering Physics", 
            "Environmental Engineering","Industrial Engineering",
            "Materials Engineering","Mechanical Engineering"};
    mItemState = new boolean[list.length]
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(ListOfMajors.this,android.R.layout.simple_list_item_multiple_choice,list);
    mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Toggle the state
            mItemState[position] = !mItemState[position];
            if (mItemState[position])
                Log.d("onItemClick","Major " + list[position] + " has been selected");
            else
                Log.d("onItemClick","Major " + list[position] + " has been deselected");
        }
    });

    mylist.setAdapter(adapter); 
}

Basically all your majors are set to a specific index (starting at 0) and their checked state is saved inside of the mItemState array. The states are toggled everytime you click on an item inside the list.

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