onItemSelected监听器 [英] onItemSelected listener
问题描述
需要一些帮助,让听者用我的列表视图工作。我的活动延伸活动。这可能是完全错误的。我一直在混合和匹配。任何帮助将是真棒。
lv.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener(){
公共无效onItemSelected(适配器视图<> parentView,查看childView,INT位置,长的id){
Toast.makeText(getBaseContext(),U点击+ al.get(位置),Toast.LENGTH_LONG).show();
}
});
您需要从视图中的位置。
这依赖于你已经设置列表(的MyType)一个类型,它也需要你使用任何类型已经覆盖了toString()方法。
这工作,因为ListView的是能够在任何给定的位置返回一个列表项,您正在使用的人code,我猜这是指ArrayList中,你需要的我觉得是在ListView中的位置,获得该项目。 (LV)
lv.setOnItemClickListener(新OnItemClickListener(){ 公共无效onItemClick(适配器视图<>母公司,观景,
INT位置,长的id){
/ *投应该是安全的ListView是的MyType的* /
MyType的MT =(的MyType)lv.getItemAtPosition(位置);
StringBuffer的提示=新的StringBuffer(你点击:);
prompt.append(mt.toString());
Toast.makeText(getApplicationContext(),提示,Toast.LENGTH_SHORT).show();
}
Need some help with getting the listener to work with my listview. My activity is extending activity. This could be completely wrong. I've been mixing and matching. Any help would be awesome.
lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View childView, int position, long id) {
Toast.makeText(getBaseContext(), "u clicked " + al.get(position),Toast.LENGTH_LONG).show();
}
});
You need to get the position from the view.
This relies on you having set a type for the list (MyType), it also requires whatever type you are using has overridden the toString().
This works because the ListView is able to return a listitem at any given position, in your code you are using al, I'm guessing this is to refer to the arraylist, you need I think to get the item at that position in the listview. (lv)
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/* cast should be safe as listview is of MyType */
MyType mt = (MyType) lv.getItemAtPosition(position);
StringBuffer prompt = new StringBuffer("You Clicked : ");
prompt.append(mt.toString());
Toast.makeText(getApplicationContext(), prompt, Toast.LENGTH_SHORT).show();
}
这篇关于onItemSelected监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!