繁体   English   中英

将OnClickListener添加到多选ListView

[英]Adding OnClickListener to Multiple Choice 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;
                }
            }
        }

    }


}

我知道这里有很多类似的问题,但是我仍然不确定该如何处理。 我该怎么办?

请像这样删除可怕的循环:-)

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数组内。 每次您单击列表内的项目时,都会切换状态。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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