该项目被点击的点击列表视图 [英] which item was clicked in a clickable list view
本文介绍了该项目被点击的点击列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个充满字符串的ArrayList一个ListView
我想使它点击..但我不能承认被点击了哪个项目
我做了这一点,但它不工作!
有没有办法知道点击的字符串?
如果不
是有办法知道点击项目的位置?
公共类的SearchResult延伸活动{ 公众的ArrayList<串GT; findMovieByName(字符串名称){
ArrayList的<电影和GT;比赛=新的ArrayList<电影和GT;();
//通过成员名单,并与给定的名称比较名称
对于(动画电影:MovieReg_activity.movies){
如果(movie.getName()。包含(名称)){
matches.add(电影); //添加匹配部件在返回表 }
}
ArrayList的<串GT; matchesNames =新的ArrayList<串GT;();
INT X = 0;
对于(动画电影:匹配){
串name65 = movie.getName();
matchesNames.add(X,name65);
X ++;
} 返回matchesNames; //返回比赛中,当与给定名称的成员,发现里面是空的
}
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.test);
LV的ListView =(ListView控件)findViewById(R.id.listView1);
TextView的电视=(的TextView)findViewById(R.id.textView1);
束束= getIntent()getExtras()。
如果(捆绑!= NULL){
字符串搜索栏= bundle.getString(搜索值);
ArrayList的<串GT;清单= findMovieByName(搜索栏);
INT match_size =则为list.size();
tv.setText(不匹配=的+ match_size); ArrayAdapter<串GT;广告=新ArrayAdapter<串GT;(SearchResults.this,android.R.layout.simple_list_item_1,清单);
lv.setAdapter(广告); lv.setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>为arg0,景观位置,INT ARG2,
长ARG3){ 如果(position.equals(1)){
Toast.makeText(SearchResults.this,1选择Toast.LENGTH_LONG).show();
}其他{
Toast.makeText(SearchResults.this,一无所有,Toast.LENGTH_LONG).show(); } }
}); }
}
}
解决方案
最终的ListView LV =(ListView控件)findViewById(R.id.yourListView);lv.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<> myAdapter,查看MyView的,INT myItemInt,长mylng){
字符串selectedFromList =(字符串)(lv.getItemAtPosition(myItemInt)); }
});
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屋!
查看全文