这是我的主要活动
TestAdapter mdh = new TestAdapter(this);
mdh.open();
ArrayList<String> songs = mdh.getAllSongs();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, songs);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.autocomplete_songs);
textView.setAdapter(adapter);
textView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
如您所见..适配器包含所有歌曲并传递给autocompletetextview 我想要的是当我点击autocompletetextview上的一个项目时,我将得到它的索引号并将其存储在一个整数...我希望它的索引来自arraylist而不是它的索引来自下拉列表..请帮助我...
答案 0 :(得分:0)
TestAdapter mdh = new TestAdapter(this);
mdh.open();
ArrayList<String> songs = mdh.getAllSongs();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, songs);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.autocomplete_songs);
textView.setAdapter(adapter);
textView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.i("GiveTag", arg2); // it will give you what ever item clicked position /index
}
});
答案 1 :(得分:0)
试试这种方式
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
String str = songs.get(pos);
}
});
答案 2 :(得分:0)
检查是否有效
textView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String text = textView.getText().toString();
int position = songs.indexOf(text);
}
});
答案 3 :(得分:0)
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Object data = arg0.getItemAtPosition(arg2);
int pos = java.util.Arrays.asList(songs).indexOf(data);
}
});