朋友,
我希望获得listview项目onclicked listner of listview
现在我已经实现了onItemClickListener但是当我点击项目文本时它会被引发 我想在listview行点击
任何想法如何实现这一目标?获取list onClick listener的文本值?
lv1.setOnItemClickListener(new OnItemClickListener() { ![enter image description here][2]
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String value = lv1.getAdapter().getItem(position).toString();
//display value here
}
});
答案 0 :(得分:12)
尝试使用此代码。
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
}
});
答案 1 :(得分:4)
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int pos = parent.getPositionForView(view);
Toast.makeText(this,pos + "",Toast.LENGTH_SHORT).show();
}
答案 2 :(得分:0)
如果您的ListView项目是使用ArrayAdapter(有希望)从数组单词填充并使用类 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;
}
}
然后您可以设置 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();
}
});
答案 3 :(得分:0)
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();