0

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

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



    }
4

3 回答 3

1

就像是

Toast.makeText(getApplicationContext(), l.getItemAt(position).toString(),Toast.LENGTH_SHORT).show();

会解决你的问题

于 2013-05-29T13:17:23.837 回答
1

您可以在 Toast 中显示选定的行数据,就像在 ListView Row Click 上一样:

@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();
    }
于 2013-05-29T13:20:21.600 回答
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();


        }
    });
于 2013-05-29T13:23:00.830 回答