简体   繁体   中英

Android: Dealing with CheckBox in a ListView

I'm using a SimpleAdapter to fill my ListView with below code.

    SimpleAdapter adapter = new SimpleAdapter(this,
            saleDriver.getOutstandings(clientId),
            R.layout.outstanding_list_row, new String[] { "sale_id",
                    "sale_date", "invoice_number", "sale_total", },
            new int[] { R.id.tt_check_box, R.id.tt_invoice_date,
                    R.id.tt_invoice_no, R.id.tt_invoice_tot });

    setListAdapter(adapter);

According to above code, i have bind sale_id with CheckBox (R.id.tt_check_box) in the listview . When i run the program, value of checkboxes displayed right of the CheckBox as text. but i don't want to display them.

My actual need is, when user checked checkboxes , i need to get sale_id s bind with them.

How could i access sale_id s bind with checked checkboxes in my java programe ?

采用

android.R.layout.simple_list_item_multiple_choice

Override the getView() function in the SimpleAdapter and use the value of sale_id to check/uncheck the checkbox. Then use this custom adapter for your list.

EDIT: In looking at another answer, my refined guess is that you need the Multiple Choice solution rather than this (since you need to find out the selections). This solution is more to display the checkbox based on existing data. If you still need this, let me know and I will post the sample code once I have access to my code.

I've attached code to obtain listview that works with multichoice checkboxes..

public class MultiChoiceActivity extends Activity {

String[] choice = { "Choice A", "Choice B", "Choice C", "Choice D", "Choice E"};

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    choiceList = (ListView)findViewById(R.id.list);

    ArrayAdapter<String> adapter
    = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_multiple_choice, 
      choice);
    choiceList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    choiceList.setAdapter(adapter);

}

Refernce Link

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