UMAR-MOBITSOLUTIONS
UMAR-MOBITSOLUTIONS

Reputation: 78004

how to get position of clicked item in OnClickListener of Listview?

I want to get selected listview item onclicked listner of listview.

Right now I have implemented onItemClickListener but when I click on an item text then it is raised. I want to raise it on a listview row click.

Any idea how to achieve this? To get the text value of the list onClick listener?

lv1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String value = lv1.getAdapter().getItem(position).toString();
                //display value here 
                
                }
              });

Upvotes: 0

Views: 47083

Answers (5)

Stevie
Stevie

Reputation: 626

Here is what you need, by Kotlin:

tvSearchAutoComplete.setOnItemClickListener { parent, view, position, id ->
   val item = cities.find { it.name == (view as TextView).text } ?: return@setOnItemClickListener
    // TODO item ....
}

Upvotes: 0

Rohit Kharb
Rohit Kharb

Reputation: 11

public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
    int pos = (int) adapterView.getPositionForView(view);
    Toast.makeText(MainActivity.this, pos, Toast.LENGTH_SHORT).show();

Upvotes: 0

Denis Pisarev
Denis Pisarev

Reputation: 584

If your ListView items were populated from an array words using an ArrayAdapter (which is hopefully) and implemented using a class Word with kind of this constructor:

public class Word {
    private String mValueA;
    /** Image resource ID for the word */
    private int mAudioResourceId;

    /*
    * Create a new Word object.
    */

    public Word(String valueA, int audioResourceId) {
        mValueA = valueA;
        mAudioResourceId = audioResourceId;
    }

   /*
    * Get the default translation of the word
     */
    public String getValueA() {
        return mValueA;
    }

    /**
     * Return the audio resource ID of the word.
     **/

    public int getAudioResourceId() {
        return mAudioResourceId;
    }    
}

And then you can set setOnItemClickListener to access clicked item for example to play audio defined in array for this list item.

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        //like this:
        Word word = words.get(position);
        //and use this position to play the audio from the array
        //corresponding to its item
        mMediaPlayer = MediaPlayer.create(SomeActivity.this, word.getAudioResourceId());
        mMediaPlayer.start();
    }
});

Upvotes: 0

Saud Khan
Saud Khan

Reputation: 844

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    int pos = parent.getPositionForView(view);
    Toast.makeText(this,pos + "",Toast.LENGTH_SHORT).show();
}

Upvotes: 4

Shashank_Itmaster
Shashank_Itmaster

Reputation: 2475

Try using this code.

lv1.setOnItemClickListener(new OnItemClickListener() {     ![enter image description here][2]
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          String value = lv1.getItemAtPosition(position).toString();
               //display value here 
         }
});

Upvotes: 13

Related Questions