Jay Lefler
Jay Lefler

Reputation: 315

How do I enable clicking in ListView

I want to get the text element that makes up my ListView items.

myList = (ListView) findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, partialNames);
myList.setAdapter(adapter);

I am not extending ListActivity so I can't call a simple onListItemClick function.

I have tried using the following:

myList.setOnItemClickListener(adapter.?)

, but I don't know what parameters I need for the constructor

How can I capture the string value in a particular ListView item when the user clicks on the row?

Upvotes: 0

Views: 50

Answers (1)

Carnal
Carnal

Reputation: 22064

Make your Activity implement OnItemClickListener, and then:

myList.setOnItemClickListener(this);

or if you dont want to implements OnItemClickListener, you can do:

myList.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View view,
                        int position, long arg3) {
    // TODO Auto-generated method stub

    }
});

Upvotes: 2

Related Questions