Toast not generating text from selected item from list

只谈情不闲聊 提交于 2019-12-02 11:08:30

问题


I've made a simple app in android with list View,In that i want to make a toast when select an item,i have tried as below but its not working..

my code is as below:

main.java

    package com.example.listview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class ListViewActivity extends Activity {


    String items[]={"Car","Bird","Bike","Flower"};
    String category[]={"Sports","Birds","Sports","Nature"};
    int icons[]={R.drawable.car,R.drawable.bird,R.drawable.bike,R.drawable.flower};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        List <HashMap<String,String>> aList=new ArrayList<HashMap<String,String>>();
        for(int i=0;i<4;i++)
        {
            HashMap<String,String> hm=new HashMap<String, String>();
            hm.put("txt","Item : "+items[i]);
            hm.put("category","Category : "+category[i]);
            hm.put("icon",Integer.toString(icons[i]));
            aList.add(hm);
        }
        String []from={"icon","txt","category"};
        int []to={R.id.image,R.id.text,R.id.category};
        SimpleAdapter adapter=new SimpleAdapter(getBaseContext(),aList,R.layout.list,from,to);
        final ListView lv=(ListView)findViewById(R.id.listView);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                // TODO Auto-generated method stub
                String selectedValue =(String) (lv.getItemAtPosition(position));
                Toast.makeText(getApplicationContext(),selectedValue , Toast.LENGTH_LONG).show();
            }
    });
    }


}

please help me..thanx in advance


回答1:


HashMap<String, String> selectedValue = (HashMap<String, String>) (lv.getItemAtPosition(position));         
ArrayList<String> list = new ArrayList<String>(selectedValue.keySet());             
Toast.makeText(getApplicationContext(), selectedValue.get("txt"), Toast.LENGTH_LONG).show();

That hashmap has got keys which are present in that list. That list is actually the from array which you have given. Just give the corresponding key to display the corresponding text.

Its working. :)




回答2:


I hope this way get selected item

      lv.setOnItemClickListener(new OnItemClickListener() {

                    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                            long arg3) {
                        // TODO Auto-generated method stub
                     //  Toast.makeText(getApplicationContext(),"Position is: "+ position, Toast.LENGTH_LONG).show();
                       String selectedFromList = lv.getItemAtPosition(position).toString();
                        Toast.makeText(getApplicationContext(),selectedFromList , Toast.LENGTH_LONG).show();
                    }
            });



回答3:


Try

String selectedValue = from[position];



回答4:


You can take the adapter and take value from it.

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
            String selectedValue =(String) (lv.getAdapter().getItem(position));
            Toast.makeText(getApplicationContext(),Toast.LENGTH_LONG).show();
        }



回答5:


I am so confused on what you are doing, why don't you just do it like this:

String selectedValue = items[position];

instead of:

String selectedValue =(String) (lv.getItemAtPosition(position));


来源:https://stackoverflow.com/questions/17038157/toast-not-generating-text-from-selected-item-from-list

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