Question

In my Android app I have to display data taken from a db in a table layout. I get the data and when I adding TableRaws to the Table it gives an Null-pointer. I properly instantiate the elements in the table and I cant figure out the error. My code snippet is as below....

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

And my table layout is as below.....

<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>

Can someone help me in this matter.

Was it helpful?

Solution

// 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);
        }
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top