onItemSelected listener

房东的猫 提交于 2019-12-11 20:13:22

问题


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();
            }
         }); 

回答1:


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();
       }


来源:https://stackoverflow.com/questions/8362702/onitemselected-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!