简体   繁体   English

如何在Listview的OnClickListener中获取点击项目的位置?

[英]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.现在我已经实现了onItemClickListener但是当我点击一个项目文本时它会被提升。 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?获取列表 onClick 侦听器的文本值?

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

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:如果您的 ListView 项目是使用 ArrayAdapter(希望如此)从数组words中填充的,并使用具有此类构造函数的类Word实现:

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.然后您可以设置setOnItemClickListener以访问单击的项目,例如播放此列表项的数组中定义的音频。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM