开发者

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 
                
                }
              });

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

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


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 
         }
});


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


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();
    }
});


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜