1

所以,到目前为止,我建立了一个这样的 json 对象列表

public class list extends ListActivity{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);

        Intent i = getIntent();
        String snopel = i.getStringExtra("nopel");
        String snama = i.getStringExtra("nama");
        String salamat = i.getStringExtra("alamat");
        String sgolongan = i.getStringExtra("golongan");

        TextView tx_nopel = (TextView)findViewById(R.id.l_nopel);
        TextView tx_nama= (TextView)findViewById(R.id.l_nama);
        TextView tx_alamat = (TextView)findViewById(R.id.l_alamat);
        TextView tx_golongan = (TextView)findViewById(R.id.l_golongan);

        tx_nopel.setText(snopel);
        tx_nama.setText(snama);
        tx_alamat.setText(salamat);
        tx_golongan.setText(sgolongan);

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

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

        JSON json_lr = new JSON();
        JSONObject jobj_lr = json_lr.getJSON("http://10.0.2.2/KP/pdam/listtagihan.php", pairs);

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

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

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

                String snomor = row.getString("nomor");
                String sbulan = row.getString("bulan");
                String stahun = row.getString("tahun");
                String stagihan = "Rp. " + row.getString("tagihan");

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

                rek.put("nomor", snomor);
                rek.put("bulan", sbulan);
                rek.put("tahun", stahun);
                rek.put("tagihan", stagihan);

                lr.add(rek);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ListAdapter adapter_lr = new SimpleAdapter(this, lr, R.layout.list_data,
                new String[]{"nomor","bulan","tahun","tagihan"},
                new int[]{R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4});

        setListAdapter(adapter_lr);

        ListView lv_lr = getListView();

        lv_lr.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                // TODO Auto-generated method stub

                Intent i = new Intent(list.this, rincian.class);
                i.putExtra("nomor", ((TextView)view.findViewById(R.id.textView1)).getText().toString());
                startActivity(i);
            }

        });
    }
}

这将在一个列表活动中显示一个列表视图,但我想知道我是否可以在 1 个列表活动中创建 2 个自定义列表视图,但我不知道如何

我认为这是不可能的,因为在 listactivity 中,我们必须设置只能像这样选择 1 个列表适配器的适配器setListAdapter(adapter_lr);

但我想确定这是真的吗?

提前致谢。

4

3 回答 3

4

为什么在一个活动中需要两个列表视图?

如果你想要两个列表视图,那么你extends Activity可以add two listview in layout file.

现在,

ListView listView1=(ListView)findViewById(R.id.listview1);
ListView listView2=(ListView)findViewById(R.id.listview2);
于 2012-07-02T06:31:30.133 回答
2

您可以通过在 xml 文件中声明它们来在一个列表活动中创建两个自定义列表视图

<ListView 
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="Value in dps"
    ></ListView>

<ListView 
 android:id="@+id/list1"
    android:layout_width="fill_parent"
    android:layout_height="Value in dps"
    ></ListView>

一个列表 id 必须是 @android:id/list 其他可以是您选择的任何内容,您可以在代码中根据需要设置适配器。

于 2012-07-02T10:05:53.683 回答
0

您需要在 YourClass 中扩展 Activity 类,并且您可以从布局中拥有尽可能多的 List 视图。

于 2012-07-02T06:44:24.890 回答