【问题标题】:two custom listview in one listactivity一个列表活动中的两个自定义列表视图
【发布时间】:2012-07-02 06:27:33
【问题描述】:

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

但我想确定这是真的吗?

提前致谢。

【问题讨论】:

    标签: android json android-listview


    【解决方案1】:

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

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

    现在,

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

    【讨论】:

    • 因为我制作了一个自定义列表,里面填充了 JSONObject,所以这个就是像这样制作一个自定义列表
    【解决方案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 其他可以是您选择的任何内容,您可以在代码中根据需要设置适配器。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-27
        • 2014-11-02
        • 2015-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-26
        相关资源
        最近更新 更多