listview onitemclick无法参考

时间:2012-06-25 10:10:55

标签: android listview android-intent onitemclicklistener

所以,我打算制作一个列表视图,如果点击其中一个项目,它将转到其他活动。很明显,我想用额外的意图。

但是在我的代码中,附加内容并不是在提供。 我尝试使用精确值来引用附加内容,但是如果我将附加内容引用到我的对象/变量,它将无效。

到目前为止,我的代码就像这样

public class bulan extends ListActivity {
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.tagihan_list);

        Intent i = getIntent();
        final String no_pel = i.getStringExtra("no_pel");

        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("nopel", no_pel));

        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

        JSON json = new JSON();
        JSONObject jobj = json.getJSON("http://10.0.2.2/KP/tagihan.php", pairs);

        try {
            int length = jobj.getInt("panjang");

            for(int n = 1; n <= length; n++){

                String m = Integer.toString(n);
                JSONObject row = jobj.getJSONObject(m);

                String id_tagihan = "No. tagihan : " + row.getString("id");
                String bulan = row.getString("bulan");
                String tahun = row.getString("tahun");
                String tagihan = "Rp. " + row.getString("tagihan");
                String status = row.getString("status");

                HashMap<String, String> map = new HashMap<String, String>();

                map.put("id_tagihan", id_tagihan);
                map.put("bulan", bulan);
                map.put("tahun", tahun);
                map.put("tagihan", tagihan);
                map.put("status", status);

                list.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ListAdapter adapter = new SimpleAdapter(this, list, R.layout.list, 
                new String[] {"id_tagihan","bulan","tahun","tagihan","status"}, new int[] { 
                R.id.id_tagihan, R.id.bulan, R.id.tahun, R.id.tagihan, R.id.status});

        setListAdapter(adapter);

        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {

                // TODO Auto-generated method stub
                String sid = ((TextView) view.findViewById(R.id.id_tagihan)).getText().toString();
                String snopel = no_pel;

                Intent i = new Intent(bulan.this, rincian.class);
                i.putExtra("id", sid);
                i.putExtra("no_pel", snopel);
                startActivity(i);

            }

        });
    }
}

就像我之前解释的那样,这就是这部分

Intent i = new Intent(bulan.this, rincian.class);
                    i.putExtra("id", sid);
                    i.putExtra("no_pel", snopel);
                    startActivity(i);

如果我像这样写

,我的代码会改变
Intent i = new Intent(bulan.this, rincian.class);
                    i.putExtra("id", "00001");
                    i.putExtra("no_pel", "0620120001");
                    startActivity(i);

1 个答案:

答案 0 :(得分:0)

尝试

Intent i = new Intent(bulan.this, rincian.class);
Bundle b=new Bundle();
b.putString("id",sid);
b.putString("no_pel",snopel);
i.putExtras(b);
startActivity(i);

在您的rincian课程中获取如下值。

b=getIntent().getExtras();
String fri_id=b.getString("id");
String nopel=b.getString("no_pel");