2

I am trying to learn how to receive a notification on a click on a radio button in a ListView but nothing comes back when I click on a radio button next to a ListView item. Below is the code where I set up the listener. I am doing this in an asynchronous task under the onPostExecute() method where I populate my ListView from the server and my main activity extends MapActivity. Does anyone know what I am doing wrong?

protected void onPostExecute(ArrayList<String> result) {
    // ... some code
    mapView.postInvalidate();

    final ArrayAdapter<String> arrayAdpt = new ArrayAdapter<String>(
            getApplicationContext(), android.R.layout.simple_list_item_single_choice,
            viewline);

    ListView restaurant_list = (ListView) findViewById(R.id.list);
    restaurant_list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    restaurant_list.setAdapter(arrayAdpt);
    restaurant_list.setScrollContainer(true);
    restaurant_list.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            Log.e("listargs", (String.valueOf(arg1)) + " " + String.valueOf(arg3));
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });
Dave
  • 873
  • 2
  • 15
  • 27
  • Try onItemClickListener instead. – Hoan Nguyen Mar 12 '13 at 16:57
  • At a quick glance, I'm not sure you want to replace the onItemSelectedListener for the ListView by an onItemClickListener for the ListView as the others (so far) suggest. You can also set a Listener for the RadioButton. But of course, this makes quite a difference regarding which area will be touch sensitive. – class stacker Mar 12 '13 at 17:05

3 Answers3

2

Does anyone know what I am doing wrong?

I recommend to you use OnItemClickListener instead of OnItemSelectedListener

list.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> adapter, View parent,
                           int position, long id) {
      // do your stuff

   }                
});

OnItemClickListener is usually used when you want to catch click events. OnItemSelectedListener is on the other side usually used with Spinner.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • Thank you. Your suggestion worked. I have been following samples from a book and they never mention the OnItemClickListener. I am trying to make the conversion from C# to Java and it is quite a challenge. I really appreciate your help. – Dave Mar 12 '13 at 17:08
  • @sajmon_d neither are you. You know there's a 15 minute cooldown. – A--C Mar 12 '13 at 17:09
  • @ρяσѕρєяK Cooldown/timeout,etc. The first 15 minutes in which the OP can't accept any answers :-) – A--C Mar 12 '13 at 17:27
  • I will accept your answer as working as it does return what I want, which is the index of the clicked listview item. – Dave Mar 12 '13 at 17:30
  • Since I am fairly new at this, how do you indicate you have accepted an answer? – Dave Mar 12 '13 at 17:32
  • 1
    @ρяσѕρєяK and A--C thanks for cooperation >D i hope that Dave will be back and perform accept. – Simon Dorociak Mar 12 '13 at 17:46
1

It sounds like you might have focus problem. Click listener only works if no other view can take focus.

Alter your checkbox xml with the following:

focusable="false"

Or set it to false if you are generating your layouts in code.

A--C
  • 36,351
  • 10
  • 106
  • 92
Jim Baca
  • 6,112
  • 2
  • 24
  • 30
  • I don't have a checkbox xml as it is part of the ListView parameter using simple_list_item_single_choice as my layout option. – Dave Mar 12 '13 at 17:19
0

OnItemSelectedListener is for trackpad/trackball events

OnItemClickListener is for click events

Personaly I always implement both for safety.

StephaneT
  • 479
  • 5
  • 11
  • Interesting. I was thinking that with a ListView, you have to set it to the mode where you can select one ore more items before it issues any _select_ events. But anyway, how do you test that on an AVD? And do you know Android devices which have these input methods? – class stacker Mar 13 '13 at 08:29