views:

207

answers:

1

Hi,

I'm just beginning Android development, and I'm working to get a Custom listview with a checkbox working. I've created a base class that extends Activity, Created an Adapter and overrode the getView() method to add the checkbox to the listview. I'm assuming I need to do this because I need something equivalent to didSelectRowIndexAtPath from Obj C to update my model. Please let me know if there's an alternate way of doing this too!

Now in my base class, I have the following code -

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout);
    setContentView(R.layout.facilityscreen);

    /* Static Data source */
    facilityModel = new FacilityDataModel[2];

    facilityModel[0] = new FacilityDataModel();
    facilityModel[1] = new FacilityDataModel();


    facilityModel[0].setFacilityName("Test 1");
    facilityModel[0].setFacilityID("Facid0001");
    facilityModel[0].setChecked(false);


    facilityModel[1].setFacilityName("Test 2");
    facilityModel[1].setFacilityID("Facid0002");
    facilityModel[1].setChecked(true);


    facilityListView = (ListView) findViewById(R.id.facilityListView);

    FacilityScreenAdapter adapter = new FacilityScreenAdapter(this, facilityModel);

    facilityListView.setAdapter(adapter);    

    myPatBtn = (Button) findViewById(R.id.myPatBtn);
    myPatBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            int i=0;
            i++;
        }});

    facilityListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            int i=0;
            i++;

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });

}    

My problem now is the setOnItemSelectedListener isn't getting called at all. Been struggling with this for a couple of hours now, and I can't figure out why it wouldn't get called at all.

Any help is much appreciated!

Thanks,
Teja.

+1  A: 

There exists already the possibility to have a ListView with checkboxes.

public class List11 extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, GENRES));

        final ListView listView = getListView();

        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }


    private static final String[] GENRES = new String[] {
        "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
        "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
    };
}

I've taken this from the APIDemos 'cause it was the simplest. You can then get the selected items by using:

long[] selectedIds = getListView().getCheckItemIds();

What you may also be interested in is the CheckedTextView which is used internally in the list.

To the part of the onListItemClick problem
Try to extend from ListActivity rather than Activity. Then override the onListItemClick. That should work.

Juri
Thanks! That really saves me a lot of trouble. But the next ListView I'm implementing does require me to customize it, so I still need help on the onClickListener().
Tejaswi Yerukalapudi