Android带有单选按钮的列表视图

4
> I want to change the selected item's checkbox color of listview so that when user selects the item its color gets changed this is my code :

final ListView listView = (ListView)findViewById(R.id.lvcancelorder);                
//this is my listview
 ArrayAdapter<String> adapter =     
new ArrayAdapter<String(this,android.R.layout.simple_list_item_single_choice, countries); //this is the adapter

listView.setAdapter(adapter);
 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) //this is the item click event
 {    
 selectedFromList = (listView.getItemAtPosition(position)).toString();   
    }});  //this is the selected item from the listview

我该怎么做?请提供一些建议。 我知道可以使用列表视图自定义适配器来实现这个功能,但是不知道如何操作。

2个回答

2

建议创建自己的自定义适配器,继承自BaseAdapter或ArrayAdapter,在选择项目时只需更改根布局视图的背景颜色。

使用以下链接了解如何创建带复选框的列表视图。 点击这里


1
你可以尝试这个:

//Your button to get selected list
getChoice = (Button)findViewById(R.id.getchoice);

        ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, android.R.layout.simple_list_item_multiple_choice, countries);
        myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        myList.setAdapter(adapter);
        getChoice.setOnClickListener(new Button.OnClickListener(){


            @Override
            public void onClick(View v) {

                String selected = "";
                int cntChoice = myList.getCount();

                SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
                for(int i = 0; i < cntChoice; i++){ 
                    if(sparseBooleanArray.get(i)) { 
                        selected += myList.getItemAtPosition(i).toString() + "\n";

                    }

                }

                Toast.makeText(MainActivity.this, selected, Toast.LENGTH_LONG).show();

            }});

我不明白,我问了如何更改单选按钮中所选项目的颜色...? - neha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接