Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android custom listview, setOnItemSelectedListener not working

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.

like image 915
Tejaswi Yerukalapudi Avatar asked Sep 02 '10 19:09

Tejaswi Yerukalapudi


4 Answers

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.

like image 145
Juri Avatar answered Oct 06 '22 09:10

Juri


use

setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// here you code 
}
})

instead of setOnItemSelectedListener

As setOnItemSelectedListener is called when item is being selected not clicked so to get clicked item you must use setOnItemClickListener this will work

like image 42
vikas aggarwal Avatar answered Oct 06 '22 08:10

vikas aggarwal


I know this is an outdated answer but I'm going to write it just in case some other fellow who has the same "problem" bumps onto this page :

The solution to the above problem which is not a problem but just a misunderstanding is that the ListView.onItemSelected() event is fired up, upon :

1) Navigating through the emulators-cross handles or 2) as far-as my HTC-Hero is concerned, the rolling-action on the white little roller-ball.

You don't have to extend your activity explicitly to a ListActivity.

Here's my tiny little code which retrieves a phone number from a TextView control, inside a listview item. When the user either touches the list item or scrolls through the list with

the little roller-ball the below Events, fire up and MakeACall() method is called :

myList.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> parent, View view, int position, long i) 
            {
                TextView myPhone = (TextView)view.findViewById(R.id.txtphone);
                MakeACall(myPhone.getText().toString());        
            }
        });

        myList.setOnItemSelectedListener(new OnItemSelectedListener() 
        {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long i) 
            {
                TextView myPhone = (TextView)view.findViewById(R.id.txtphone);
                MakeACall(myPhone.getText().toString());
            }

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

            }
        });

I hope that was helpful... :)

like image 38
Skourkos Avatar answered Oct 06 '22 10:10

Skourkos


You should set all focusable items in custom list layout to false:

        android:focusable="false"

also I think you should not use attributes like android:clickable="true" for them.

like image 44
maanijou Avatar answered Oct 06 '22 08:10

maanijou