如何在Toast中显示所选数据?

时间:2013-05-29 13:13:03

标签: android listview toast

我正在使用以下代码来显示数据,我想要在Toast中显示列表项的数据时单击我的案例中的listItem不显示,请帮助。谢谢

 public class MainActivity extends ListActivity {
  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<Map<String, String>> list = buildData();
        String[] from = { "name", "purpose" };
        int[] to = { android.R.id.text1, android.R.id.text2 };

        SimpleAdapter adapter = new SimpleAdapter(this, list,
            android.R.layout.simple_list_item_2, from, to);
        setListAdapter(adapter);
      }

      private ArrayList<Map<String, String>> buildData() {
        ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
        list.add(putData("Android", "Mobile"));
        list.add(putData("Windows7", "Windows7"));
        list.add(putData("iPhone", "iPhone"));
        return list;
      }

      private HashMap<String, String> putData(String name, String purpose) {
        HashMap<String, String> item = new HashMap<String, String>();
        item.put("name", name);
        item.put("purpose", purpose);
        return item;
      }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);

         Toast.makeText(getApplicationContext(), ???????????????????????????,
                  Toast.LENGTH_SHORT).show();
    }



    }

3 个答案:

答案 0 :(得分:1)

这样的东西
Toast.makeText(getApplicationContext(), l.getItemAt(position).toString(),Toast.LENGTH_SHORT).show();

可以解决您的问题

答案 1 :(得分:1)

您可以在Toast中显示选定的行数据,如ListView Row上所示:

@Override
    protected void onListItemClick(ListView l, View v,
                               int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        Map<String, String> selecteddata = 
             (Map<String, String>)l.getAdapter().getItem(position);

        String str_selected_data="name :: "+selecteddata.get("name") +
              " purpose :: " +selecteddata.get("purpose");
         Toast.makeText(getApplicationContext(), str_selected_data,
                  Toast.LENGTH_SHORT).show();
    }

答案 2 :(得分:0)

mlist.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            String innString = String.valueOf(imagelist[(int) arg2]);// imagelist[(int)
                                                                        // arg2]
            Toast.makeText(getApplicationContext(), innString, 500).show();


        }
    });