在可点击的列表视图中点击了哪个项目 [英] which item was clicked in a clickable list view
本文介绍了在可点击的列表视图中点击了哪个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 listView,里面装满了一个字符串数组我想让它可点击..但我无法识别点击了哪个项目
i have a listView which is filled with an arraylist of strings and i want to make it clickable .. but i cant recognize which item was clicked
我做了这个,但它不起作用!
i've made this but it doesn't work !
有没有办法知道点击的字符串?如果不有没有办法知道被点击的项目的位置?
is there way to know the string clicked ? if not is there a way to know even the position of clicked item?
public class SearchResults extends Activity{
public ArrayList<String> findMovieByName(String name) {
ArrayList<movie> matches = new ArrayList<movie>();
// go through list of members and compare name with given name
for(movie movie : MovieReg_activity.movies) {
if (movie.getName().contains(name)) {
matches.add(movie); // adds matching member to the return list
}
}
ArrayList<String> matchesNames = new ArrayList<String>();
int x=0;
for( movie movie : matches) {
String name65 = movie.getName();
matchesNames.add(x,name65);
x++ ;
}
return matchesNames; // return the matches, which is empty when no member with the given name was found
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ListView lv=(ListView)findViewById(R.id.listView1);
TextView tv=(TextView)findViewById(R.id.textView1);
Bundle bundle =getIntent().getExtras();
if(bundle!=null){
String searchbar =bundle.getString("search value");
ArrayList<String> list = findMovieByName(searchbar);
int match_size=list.size();
tv.setText("no of matches=" +match_size);
ArrayAdapter<String> ad = new ArrayAdapter<String>(SearchResults.this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(ad);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View position , int arg2,
long arg3) {
if( position.equals(1) ){
Toast.makeText(SearchResults.this,"1 chosen", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(SearchResults.this,"nothing", Toast.LENGTH_LONG).show();
}
}
});
}
}
}
推荐答案
final ListView lv = (ListView) findViewById(R.id.yourListView);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
String selectedFromList =(String) (lv.getItemAtPosition(myItemInt));
}
});
这篇关于在可点击的列表视图中点击了哪个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文