Detecting Which item was Selected From a ListView

点点圈 提交于 2019-12-11 13:10:06

问题


I am trying to get the title of the song that was selected from my listview but I'm getting a forced close. Any ideas?

    ArrayList<String>songtitle = new ArrayList<String>();

    //This is how i popluated sontitle//
   ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.song,songtitle);
    setListAdapter(adapter);

 protected void onListIemClick(ListView  , View v, int position, long id){
       super.onListItemClick(c, v, position, id);
        Object o = this.getListAdapter().getItem(position);
    String pen = o.toString();
        Toast.makeText(this, "You have chosen the color: " + " " + songtitle, Toast.LENGTH_LONG).show();

回答1:


 protected void onListIemClick(ListView  , View v, int position, long id){
     Toast.makeText(this, "You have chosen the color: " + songtitle.get(position), Toast.LENGTH_LONG).show();
}

try this.




回答2:


selection is not being updated here, it should be computed based on the position. You probably want to something like this:

    ((TextView)v).setText(text);

Also, it looks like songTitle is a list. You should probably rename that to songTitles to begin with. Check that the position is within the bounds of your list. What kind of error are you getting?



来源:https://stackoverflow.com/questions/8196552/detecting-which-item-was-selected-from-a-listview

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