从动态列表视图中获取值

时间:2013-05-23 14:00:22

标签: android json listview

所以我通过json获取价值

   JSONObject json = new JSONObject(result);        
    if(json.has("message"))
    {
        message = json.getString("message");
   }
   else
   {
        for (int i = 0; i < json.length(); i++) 
        {
            suppliername = json.getString("s_name");
            suppliernumber=json.getString("suppliernumber");
            supplierid=json.getString("supplierid");
            HashMap<String, Object> data;
            data = new HashMap<String, Object>();
            data.put(NAME, suppliername);
            data.put(PHONE,suppliernumber);
            sampleData.add(data);
        }
    adapter = new MyCustomAdapter(getApplicationContext(), sampleData);
    lv.setAdapter(adapter);
  }

这是我的倾听者

lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
        startActivity(new Intent(getApplicationContext(),
                    supp_details.class));
        }
    });

所以我想要的是当我点击第二个列表项时,它必须将第二个值传递给supp_details.class

Customadapter类

class MyCustomAdapter extends BaseAdapter {
    ArrayList<HashMap<String, Object>> data;
    Context context;

    public MyCustomAdapter(Context context,
            ArrayList<HashMap<String, Object>> data) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int arg0) {
        return data.get(arg0);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public class Myholder {
    TextView name, phone;

    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflator.inflate(R.layout.list_item,
                    null);
            convertView = view;
            Myholder holder = new Myholder();
            holder.name = (TextView) convertView
                    .findViewById(R.id.name);

            holder.phone = (TextView) convertView
                    .findViewById(R.id.phone);

            convertView.setTag(holder);
        }
        Myholder holder = (Myholder) convertView.getTag();
        holder.name.setText("name   : " + data.get(position).get(NAME));

        holder.phone.setText("Ph        : "
                + data.get(position).get(PHONE) + "");

        return convertView;
    }
}

这是我的习惯用语类

2 个答案:

答案 0 :(得分:0)

lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
        HashMap<String, Object> data = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);
        Intent intent = new new Intent(getApplicationContext(),
                    supp_details.class);
        intent.putExtra("KEY", data);

        startActivity(intent);
        }
    });

答案 1 :(得分:0)

从中得出一个想法:

listview1.setOnItemClickListener(new OnItemClickListener()
{
     @Override
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
     {
             Addiction addiction = (Addiction) listview1.getItemAtPosition(arg2);
             String textview1 = addiction.getCatName();
             Intent intent = new Intent(StartActivity.this, AddictionDetails.class);
             intent.putExtra("cat_name", textview1);
             startActivity(intent);
     }
});

根据您的要求做一点修改,然后用OnItemClick()方法重写这段代码。