0

因此,我计划制作一个列表视图,如果单击其中一个项目,它将转到其他活动。很明显,我想使用带有附加功能的意图。

但在我的代码中,额外内容并没有引用。我试图用精确的值来引用额外的东西,它起作用了,但是如果我把额外的东西用来引用我的对象/变量,它就不起作用了。

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

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);
4

1 回答 1

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");
于 2012-06-25T10:31:26.147 回答