将OnClickListener添加到多个选择ListView

时间:2014-07-17 20:37:58

标签: java android listview onclicklistener onitemclicklistener

我的应用有多选ListView,可以选择多个元素。我创建了一个SparseBooleanArray来存储是否单击了不同的元素,但我不确定如何为此实现OnClickListener。

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

    }


}

我知道这里有很多问题有类似的问题,但我仍然不确定该怎么做我的情况。我该怎么办?

1 个答案:

答案 0 :(得分:0)

请删除那个可怕的for循环: - )

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

基本上所有专业都设置为特定索引(从0开始),其检查状态保存在mItemState数组中。每次单击列表中的项目时,都会切换状态。