Question

I have a class that extends from ListActivity and implements OnItemClickListener

It's a very simple test class, the idea is that I select an item on the list, and it shows the selected item on a Toast.

I can see the list normally on the emulator, and I can also see the effects of clicking in the item, but then nothing happens.

I don't think the event is being fired, because I see nothing on LogCat, here's the code:

public class CarsListActivity extends ListActivity implements
        OnItemClickListener {

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

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, listCars()));
    }

    private List<String> listCars() {   
        return Arrays.asList("Ferrari", "Lamborghini", "Porsche");
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView textView = (TextView) view;
        String message = "Selected car: " + textView.getText();
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

}

The Activitiy is defined like this on the AndroidManifest.xml file:

<activity android:name=".CarsListActivity" />

Is there anything I'm missing?

I researched this error and I found many solutions saying that this concerns clickability and focusability attributes on the layout. But I'm using Android's own android.R.layout.simple_list_item_1 so I don't really know how I could fix it.

Am I missing some configuration?

Was it helpful?

Solution

You need to register the OnItemClickListener (the activity) like this :

getListView().setOnItemClickListener(this)

Simply implementing the OnItemClickListener interface is not sufficient

OTHER TIPS

add this in onCreate() below setListAdapter()

    getListView().setOnItemClickListener(this);

use this on oncreate()

getListView().setOnItemClickListener(this);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top