Alex Rivera | Logout

Android custom listview, setOnItemSelectedListener not working

Asked 2010-09-02T19:24:10.743
10

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) {
          
Edit
Report

1 Answer

4

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.

answered 2010-09-02T19:44:09.490

Your Answer