【问题标题】:Adding OnClickListener to Multiple Choice ListView将 OnClickListener 添加到多项选择 ListView
【发布时间】:2023-03-26 12:02:01
【问题描述】:

我的应用有一个 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;
                }
            }
        }

    }


}

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

【问题讨论】:

    标签: java android listview onclicklistener onitemclicklistener


    【解决方案1】:

    请像这样删除那个可怕的 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 数组中。每次单击列表中的项目时,状态都会切换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 2017-08-23
      相关资源
      最近更新 更多