Ramamoorthy
Ramamoorthy

Reputation: 287

how to get listitem text in onItemClick listener?

I have created a list view and i have added some item, can anybody help me to get the item text in on Item Click listener?

lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView <? > parent, View view,
            int position, long id) {
            Object o = parent.getItemAtPosition(position);
            String keyword = o.toString();
            Log.v("value ", "result is " + keyword);

        }
    });

I have tried the above code but it does not work...

Upvotes: 5

Views: 14515

Answers (2)

Nikola Despotoski
Nikola Despotoski

Reputation: 50538

I assume you have TextView on the item.

lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView <? > parent, View view,
            int position, long id) {
            TextView txt = (TextView) parent.getChildAt(position - lv.firstVisiblePosition()).findViewById(R.id.mylistviewtextview);
            String keyword = txt.getText().toString();
            Log.v("value ", "result is " + keyword);

        }
    });       

or you can retrieve the text from the ArrayList you feed the ListView

Upvotes: 8

JWL
JWL

Reputation: 14201

First of all, the correct event to intercept for selection is onItemSelected because a selection might not have been made in the onClick handler. And then the question has been well treated here on stackoverflow.

Upvotes: 2

Related Questions