一个listactivity中的两个自定义列表视图

时间:2012-07-02 06:27:33

标签: android json android-listview

所以,到目前为止,我构建了一个像这样的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);
            }

        });
    }
}

将在一个listactivity中显示一个listview,但我想知道我是否可以在1个列表活动中制作2个自定义列表视图,但我无法弄清楚如何

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

但我想知道这是真的吗?

提前致谢。

3 个答案:

答案 0 :(得分:4)

为什么在一个Activity中需要两个listview?

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

现在,

ListView listView1=(ListView)findViewById(R.id.listview1);
ListView listView2=(ListView)findViewById(R.id.listview2);

答案 1 :(得分: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其他可以是你选择的任何东西,你可以在代码中设置你想要的适配器。

答案 2 :(得分:0)

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