Bishan
Bishan

Reputation: 15740

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_ids bind with them.

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

Upvotes: 0

Views: 844

Answers (3)

Aviral
Aviral

Reputation: 1141

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.

Upvotes: 0

GK_
GK_

Reputation: 1222

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

Upvotes: 0

Deepika
Deepika

Reputation: 591

use

android.R.layout.simple_list_item_multiple_choice

Upvotes: 1

Related Questions