Android在运行时将数据加载到表布局中

萨曼莎(Samantha Withanage)

在我的Android应用程序中,我必须以表布局显示从数据库获取的数据我得到了数据,当我向表中添加TableRaws时,它给出了一个空指针。我正确实例化了表中的元素,但无法弄清错误。我的代码段如下所示。

TableLayout tbl=(TableLayout) findViewById(R.id.dpl_tbl_lst);

        if(!retailersList.isEmpty()){

            //TableRow Hedding=(TableRow) findViewById(R.id.dpl_tbl_hdr);
            //tbl.addView(Hedding);

            for(int i=0;i<retailersList.size();i++){

                Retailer RtlIns=retailersList.get(i);


                TableRow tbl_rw=(TableRow) findViewById(R.id.dpl_tbr_cnt);

                TextView sequence, custommerName, city, status;
                sequence=(TextView) findViewById(R.id.dpl_txt_seq);
                custommerName=(TextView) findViewById(R.id.dpl_txt_cus_nm);
                city=(TextView) findViewById(R.id.dpl_txt_cty);
                status=(TextView) findViewById(R.id.dpl_txt_sts);

                sequence.setText(RtlIns.getPosition());
                custommerName.setText(RtlIns.getName());
                city.setText(RtlIns.getCity());
                status.setText(RtlIns.getStatus());

                tbl_rw.addView(sequence);
                tbl_rw.addView(custommerName);
                tbl_rw.addView(city);
                tbl_rw.addView(status);


                tbl.addView(tbl_rw);}

我的表布局如下。

<TableLayout
            android:id="@+id/dpl_tbl_lst"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TableRow
                android:id="@+id/dpl_tbl_hdr"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/textview3"
                    android:layout_width="@dimen/cell_width_0"
                    android:layout_height="@dimen/cell_h_h"
                    android:background="@drawable/cell_shape"
                    android:padding="@dimen/cell_pdn"
                    android:text="@string/list_sequense"
                    android:textColor="@color/white"
                    android:textSize="@dimen/normal_text"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="@dimen/cell_width_2"
                    android:layout_height="@dimen/cell_h_h"
                    android:background="@drawable/cell_shape"
                    android:padding="@dimen/cell_pdn"
                    android:text="@string/list_cust_name"
                    android:textColor="@color/white"
                    android:textSize="@dimen/normal_text"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="@dimen/cell_width_2"
                    android:layout_height="@dimen/cell_h_h"
                    android:background="@drawable/cell_shape"
                    android:padding="@dimen/cell_pdn"
                    android:text="@string/list_city"
                    android:textColor="@color/white"
                    android:textSize="@dimen/normal_text"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="@dimen/cell_width_1"
                    android:layout_height="@dimen/cell_h_h"
                    android:background="@drawable/cell_shape"
                    android:padding="@dimen/cell_pdn"
                    android:text="@string/list_status"
                    android:textColor="@color/white"
                    android:textSize="@dimen/normal_text"
                    android:textStyle="bold" />
            </TableRow>


        </TableLayout>

有人可以帮我这个忙。

哈雷什·切拉纳(Haresh Chhelana)
// try this way i gave simple demo but you have use your data rather my static data..
1. **table.xml**
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dpl_tbl_lst"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</TableLayout>
2.**table_row.xml**
<?xml version="1.0" encoding="utf-8"?>
<TableRow  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dpl_tbl_hdr"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/dpl_txt_seq"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:text="asd"/>

    <TextView
        android:id="@+id/dpl_txt_cus_nm"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:text="asd"/>

    <TextView
        android:id="@+id/dpl_txt_cty"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:text="asd"/>

    <TextView
        android:id="@+id/dpl_txt_sts"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:text="asd"/>
</TableRow>

3. **MyActivity.java**
public class MyActivity extends Activity {
    TableLayout dpl_tbl_lst;
    @Override
    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.table);
        dpl_tbl_lst = (TableLayout) findViewById(R.id.dpl_tbl_lst);

        for (int i=0;i<5;i++){
            View row = LayoutInflater.from(this).inflate(R.layout.table_row,null,false);

            TextView dpl_txt_seq = (TextView) row.findViewById(R.id.dpl_txt_seq);
            TextView dpl_txt_cus_nm = (TextView) row.findViewById(R.id.dpl_txt_cus_nm);
            TextView dpl_txt_cty = (TextView) row.findViewById(R.id.dpl_txt_cty);
            TextView dpl_txt_sts = (TextView) row.findViewById(R.id.dpl_txt_sts);

            dpl_txt_seq.setText("Seq"+String.valueOf(i+1));
            dpl_txt_cus_nm.setText("Nm"+String.valueOf(i+1));
            dpl_txt_cty.setText("City"+String.valueOf(i+1));
            dpl_txt_sts.setText("Sts"+String.valueOf(i+1));

            dpl_txt_seq.setTag("Seq"+String.valueOf(i+1));
            dpl_txt_seq.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String data = view.getTag().toString();
                    Toast.makeText(MyActivity.this,data,Toast.LENGTH_SHORT).show();

                }
            });
            dpl_txt_cus_nm.setTag("Nm"+String.valueOf(i+1));
            dpl_txt_cus_nm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String data = view.getTag().toString();
                    Toast.makeText(MyActivity.this,data,Toast.LENGTH_SHORT).show();

                }
            });
            dpl_txt_cty.setTag("City"+String.valueOf(i+1));
            dpl_txt_cty.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String data = view.getTag().toString();
                    Toast.makeText(MyActivity.this,data,Toast.LENGTH_SHORT).show();

                }
            });
            dpl_txt_sts.setTag("Sts"+String.valueOf(i+1));
            dpl_txt_sts.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String data = view.getTag().toString();
                    Toast.makeText(MyActivity.this,data,Toast.LENGTH_SHORT).show();

                }
            });
            dpl_tbl_lst.addView(row);
        }
    }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章