Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get listitem text in onItemClick listener?

Tags:

android

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...

like image 289
Ramamoorthy Avatar asked Aug 13 '11 12:08

Ramamoorthy


People also ask

How to get data from ListView by clicking item on ListView?

Try this: my_listview. setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } });

How to edit ListView item in android?

OnItemClickListener MshowforItem = new AdapterView. OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ((TextView)view). setText("Hello"); } };

What is OnItemClickListener in android?

android.widget.AdapterView.OnItemClickListener. Known indirect subclasses. CharacterPickerDialog, PreferenceScreen. CharacterPickerDialog. Dialog for choosing accented characters related to a base character.


2 Answers

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

like image 140
Nikola Despotoski Avatar answered Sep 26 '22 15:09

Nikola Despotoski


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.

like image 43
JWL Avatar answered Sep 22 '22 15:09

JWL