OnClick动态地向TableLayout添加新行,并从数据库中获取值,在添加的行中显示它并给出总计

时间:2022-02-12 08:25:01

This is the link to the screen

这是屏幕的链接

  private void sendBarcode(final String barcodeNum)throws JSONException
{
    final Context context = getApplicationContext();

    StringRequest stringReq = new StringRequest(Request.Method.POST, ITEM_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    showItem(response);

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(context, error.toString(), Toast.LENGTH_SHORT).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError
        {
            Map<String,String> params = new HashMap<>();
            params.put(KEY_BARCODE,barcodeNum);
            return params;

        }
    };
    RequestQueue reqQueue = Volley.newRequestQueue(context);
    reqQueue.add(stringReq);

}

This is my Volley code i use to send data to the database i.e i am sending barcode to the database and fetching the respective item.

这是我使用的Volley代码将数据发送到数据库,即我将条形码发送到数据库并获取相应的项目。

Currently I am simply displaying the fetched data in list format.I would like to display it in table format and and also display the sum total of the items fetched. To display in list format I am using the following code

目前我只是以列表格式显示获取的数据。我想以表格格式显示它,并且还显示所提取项目的总和。要以列表格式显示,我使用以下代码

private void showItem(String json)
{
    ParseBarcode pb = new ParseBarcode(json);
    pb.parseBarcode();
    BarcodeList bl = new BarcodeList(this,ParseBarcode.itembarcode,ParseBarcode.itemdesc,ParseBarcode.weight,ParseBarcode.rate,ParseBarcode.making,ParseBarcode.netrate,ParseBarcode.total);
    invest_listview.setAdapter(bl);
}

json parser class   
public class ParseBarcode {
    public static String[] itembarcode;
    public static String[] itemdesc;
    public static String[] weight;
    public static String[] rate;
    public static String[] making;
    public static String[] netrate;
    public static String[] total;

    public static final String JSON_ARRAY ="result";
    public static final String KEY_BARCODE ="itembarcode";
    public static final String KEY_DESC="itemdesc";
    public static final String KEY_WEIGHT="weight";
    public static final String KEY_RATE="rate";
    public static final String KEY_MAKING="making";
    public static final String KEY_NETRATE="netrate";
    public static final String KEY_TOTAL ="total";


    private JSONArray items =null;

    private String json;

    public ParseBarcode(String json){
        this.json = json;
    }

    protected void parseBarcode(){
        JSONObject jsonObject =null;
        try {
            jsonObject = new JSONObject(json);
            items = jsonObject.getJSONArray(JSON_ARRAY);

            itembarcode = new String[items.length()];
            itemdesc = new String[items.length()];
            weight = new String[items.length()];
            rate = new String[items.length()];
            making = new String[items.length()];
            netrate = new String[items.length()];
            total = new String[items.length()];

            for (int i=0; i<items.length();i++)
            {
                JSONObject jo = items.getJSONObject(i);
                itembarcode[i] = jo.getString(KEY_BARCODE);
                itemdesc[i] = jo.getString(KEY_DESC);
                weight[i] = jo.getString(KEY_WEIGHT);
                rate[i] = jo.getString(KEY_RATE);
                making[i] = jo.getString(KEY_MAKING);
                netrate[i] = jo.getString(KEY_NETRATE);
                total[i] = jo.getString(KEY_TOTAL);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


}

This is my xml layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/inv_est_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@color/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />


    <RelativeLayout
        android:id="@+id/rl_inv_est"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/inv_est_toolbar"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_invoice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/switch_bt"
            android:layout_alignParentTop="true"
            android:text="Invoice"
            android:textSize="40dp" />

        <Switch
            android:id="@+id/switch_bt"
            android:layout_centerHorizontal="true"
            android:layout_alignTop="@+id/tv_invoice"
            android:layout_alignBottom="@+id/tv_invoice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            />

        <TextView
            android:id="@+id/tv_estimate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/switch_bt"
            android:text="Estimate"
            android:textSize="40dp" />

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/ll_inv_est"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rl_inv_est"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/iv_logo"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/users" />

        <LinearLayout
            android:id="@+id/ll_inv_est1"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Company Name"
                android:textColor="@android:color/black"
                android:textSize="40dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Company Address"
                android:textSize="24dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Phone:83842382383"
                android:textSize="24dp" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_inv_est2"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/ll_inv_est_inner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Invoice No:"
                    android:textSize="24dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1842"
                    android:textColor="@android:color/black"
                    android:textSize="32dp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/ll_inv_est_inner1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Date :"
                    android:textSize="24dp" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="25/10/2015"
                    android:textColor="@android:color/black"
                    android:textSize="24dp" />


            </LinearLayout>

        </LinearLayout>
        <LinearLayout
            android:id="@+id/ll_inv_est3"
            android:layout_weight="1"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/ll_inv_est_inner2"
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Customer :"
                    android:textSize="24dp"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Test Cust1"
                    android:textSize="24dp"/>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/ll_inv_est_inner3"
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Contact:"
                    android:textSize="24dp"/>


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="+91-8600931386"
                    android:textSize="24dp"/>
            </LinearLayout>


        </LinearLayout>

    </LinearLayout>

<TableLayout
    android:id="@+id/tl_invest"
    android:layout_marginTop="16dp"
    android:layout_below="@+id/ll_inv_est"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TableRow
        android:id="@+id/tr_invest"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        >
        <TextView
            android:layout_weight="1"
            android:gravity="center"
            android:text="Barcode"
            android:textAllCaps="true"
            android:textSize="16sp" />

        <TextView
            android:layout_weight="1"
            android:gravity="center"
            android:text="item desc"
            android:textAllCaps="true"
            android:textSize="16sp" />
        <TextView
            android:layout_weight=".75"
            android:gravity="center"
            android:text="Weight"
            android:textAllCaps="true"
            android:textSize="16sp" />
        <TextView
            android:layout_weight=".5"
            android:gravity="center"
            android:text="Rate\n(per 10gm)"
            android:textAllCaps="true"
            android:textSize="16sp" />
        <TextView
            android:layout_weight=".5"
            android:gravity="center"
            android:text="Making\n(per 10gm)"
            android:textAllCaps="true"
            android:textSize="16sp" />
        <TextView
            android:layout_weight=".5"
            android:gravity="center"
            android:text="net rate\n(per 10gm)"
            android:textAllCaps="true"
            android:textSize="16sp" />
        <TextView
            android:layout_weight="1"
            android:gravity="center"
            android:text="total"
            android:textAllCaps="true"
            android:textSize="16sp" />
    </TableRow>


</TableLayout>

    <ListView
        android:id="@+id/lv_invest"
        android:layout_below="@+id/tl_invest"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <ImageButton
        android:id="@+id/ib_plus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/plus"
        android:layout_alignTop="@+id/ib_save"
        android:layout_toLeftOf="@+id/ib_save"
        android:layout_toStartOf="@+id/ib_save" />
    <ImageButton
        android:id="@+id/ib_save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/save"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/ib_printer"
        android:layout_toStartOf="@+id/ib_printer" />
    <ImageButton
        android:id="@+id/ib_printer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/printer"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/imageButton2"
        android:layout_toStartOf="@+id/imageButton2" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/window_close"
        android:layout_marginRight="34dp"
        android:layout_marginEnd="34dp"
        android:id="@+id/imageButton2"
        android:layout_alignTop="@+id/ib_save"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />


    <LinearLayout
        android:id="@+id/ll_inv_est4"
        android:orientation="vertical"
        android:layout_toLeftOf="@+id/ib_plus"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="16dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/ll_inv_est_inner4"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/tv_vattitle"
                android:textSize="16sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="VAT TIN NO ."/>
            <TextView
                android:id="@+id/tv_vatno"
                android:textSize="16sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="13284715/V/1.4.06 w.e.f.1.4.06"
                android:layout_alignParentBottom="true"
                />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/ll_inv_est_inner5"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/tv_csttitle"
                android:textSize="16sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="CST TIN NO."/>
            <TextView
                android:id="@+id/tv_cstno"
                android:textSize="16sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="13284715/V/1.4.06 w.e.f.1.4.06"
                android:layout_alignParentBottom="true"
                />

        </LinearLayout>

    </LinearLayout>


</RelativeLayout>

This is the on options item selected code for the plus icon in the toolbar and its xml as follows:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId())
    {
        case android.R.id.home:
            super.onBackPressed();
            return true;
        case R.id.action_add:
            final Dialog dialog = new Dialog(this);
            dialog.setContentView(R.layout.barcodenum);

            TextView title = (TextView) dialog.findViewById(R.id.tv_bctitle);
            title.setText("Enter Barcode Number");
            final String bcNumber;

            EditText barcode = (EditText) dialog.findViewById(R.id.et_bcNum);
            bcNumber = barcode.getText().toString();

            Button bt_save = (Button) dialog.findViewById(R.id.bt_bcenter);
            bt_save.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    try {
                        sendBarcode(bcNumber);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });


            dialog.show();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

xml for dialog box

对话框的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    >

    <TextView
        android:id="@+id/tv_bctitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        />

        <TextView
            android:id="@+id/tv_bcName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Barcode No."
            android:layout_alignBottom="@+id/ti_barcode"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
        <android.support.design.widget.TextInputLayout
            android:id="@+id/ti_barcode"
            android:layout_below="@+id/tv_bctitle"
            android:layout_toRightOf="@id/tv_bcName"
            android:layout_width="500dp"
            android:layout_height="wrap_content">
        <EditText
            android:id="@+id/et_bcNum"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter barcode number"
            />
        </android.support.design.widget.TextInputLayout>



    <Button
        android:id="@+id/bt_bcenter"
        android:layout_below="@+id/ti_barcode"
        android:layout_marginTop="16dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="SAVE"
        />
    </RelativeLayout>

1 个解决方案

#1


0  

Alright I will not guide you however I will provide you with a very basic example on creating a table and adding data to it. Now in this example as you will see I am actually using a button to add the data however the principle would remain the same through in terms of adding your parsed data that you are receiving from your server. How to parse that data I will leave up to you. Essentially what we have here is a "blank" TableLayout that I have added the headers to. As you can see I didn't add any TableRows past the headers because that will be added by your parsing of the data (or in my example pressing a button).

好吧,我不会指导你,但是我将为你提供一个关于创建表并向其添加数据的基本示例。现在在这个示例中,您将看到我实际上正在使用按钮来添加数据,但是在添加从服务器接收的已分析数据方面,原则将保持不变。如何解析我将留给你的数据。基本上我们这里有一个“空白”TableLayout,我已经添加了标题。正如您所看到的,我没有在标题之外添加任何TableRows,因为这将通过解析数据(或在我的示例中按下按钮)添加。

This is the TABLE SCREEN Layout

这是TABLE SCREEN布局

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="72dp">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Row"
            android:id="@+id/button"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>

    <TableRow android:layout_height="wrap_content">
        <TextView
            android:id="@+id/barcode"
            android:text="Barcode"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/itemDesc"
            android:text="Item Desc."
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/weight"
            android:text="Weight"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/rate"
            android:text="      Rate \n (Per 10GM)"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/making"
            android:text="      Making \n (Per 10GM)"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/net"
            android:text="Net Rate"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/total"
            android:text="Total"
            android:layout_height="wrap_content"
            />
    </TableRow>
  </TableLayout>
</LinearLayout>

OnClick动态地向TableLayout添加新行,并从数据库中获取值,在添加的行中显示它并给出总计

Now as I said I will not go over parsing your JSON data as it seems as if you have that down and I will leave up to you. However for an example of adding rows I have added in a button that will just add a row with dummy data on a button press. The action will be extremely similar when you are adding your data just you will need to loop or something to get in all of your data.

现在正如我所说的那样,我不会过去解析你的JSON数据,因为好像你已经把它解决了,我会留给你。然而,对于添加行的示例,我已经在按钮中添加了按钮,该按钮将仅在按下按钮时添加具有虚拟数据的行。当您添加数据时,操作将非常相似,只需要循环或获取所有数据。

public class MainActivity extends AppCompatActivity {
Button btn;
TableLayout table;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.button);
    table = (TableLayout) findViewById(R.id.ourTableLayout);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TableRow newRow = new TableRow(MainActivity.this);
            TextView barCode = new TextView(MainActivity.this);
            TextView itemDesc = new TextView(MainActivity.this);
            TextView weightLine = new TextView(MainActivity.this);
            TextView rateAmount = new TextView(MainActivity.this);
            TextView makingAmount = new TextView(MainActivity.this);
            TextView netRate = new TextView(MainActivity.this);
            TextView total = new TextView(MainActivity.this);

            barCode.setText("Barcodes");
            itemDesc.setText("Descriptions");
            weightLine.setText("Weights");
            rateAmount.setText("Rates");
            makingAmount.setText("Makings");
            netRate.setText("Nets");
            total.setText("Totals");

            newRow.addView(barCode);
            newRow.addView(itemDesc);
            newRow.addView(weightLine);
            newRow.addView(rateAmount);
            newRow.addView(makingAmount);
            newRow.addView(netRate);
            newRow.addView(total);
            table.addView(newRow);

        }
    });
  }
}

And after a few button clicks we have: OnClick动态地向TableLayout添加新行,并从数据库中获取值,在添加的行中显示它并给出总计

点击几下按钮后,我们有:

Now as stated you will definitely need to do some legwork on your end to parse the data correctly but this should be more than enough to get you up and running.

现在如上所述,你肯定需要在你的头上做一些工作来正确地解析数据,但这应该足以让你开始运行。

#1


0  

Alright I will not guide you however I will provide you with a very basic example on creating a table and adding data to it. Now in this example as you will see I am actually using a button to add the data however the principle would remain the same through in terms of adding your parsed data that you are receiving from your server. How to parse that data I will leave up to you. Essentially what we have here is a "blank" TableLayout that I have added the headers to. As you can see I didn't add any TableRows past the headers because that will be added by your parsing of the data (or in my example pressing a button).

好吧,我不会指导你,但是我将为你提供一个关于创建表并向其添加数据的基本示例。现在在这个示例中,您将看到我实际上正在使用按钮来添加数据,但是在添加从服务器接收的已分析数据方面,原则将保持不变。如何解析我将留给你的数据。基本上我们这里有一个“空白”TableLayout,我已经添加了标题。正如您所看到的,我没有在标题之外添加任何TableRows,因为这将通过解析数据(或在我的示例中按下按钮)添加。

This is the TABLE SCREEN Layout

这是TABLE SCREEN布局

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="72dp">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Row"
            android:id="@+id/button"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>

    <TableRow android:layout_height="wrap_content">
        <TextView
            android:id="@+id/barcode"
            android:text="Barcode"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/itemDesc"
            android:text="Item Desc."
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/weight"
            android:text="Weight"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/rate"
            android:text="      Rate \n (Per 10GM)"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/making"
            android:text="      Making \n (Per 10GM)"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/net"
            android:text="Net Rate"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/total"
            android:text="Total"
            android:layout_height="wrap_content"
            />
    </TableRow>
  </TableLayout>
</LinearLayout>

OnClick动态地向TableLayout添加新行,并从数据库中获取值,在添加的行中显示它并给出总计

Now as I said I will not go over parsing your JSON data as it seems as if you have that down and I will leave up to you. However for an example of adding rows I have added in a button that will just add a row with dummy data on a button press. The action will be extremely similar when you are adding your data just you will need to loop or something to get in all of your data.

现在正如我所说的那样,我不会过去解析你的JSON数据,因为好像你已经把它解决了,我会留给你。然而,对于添加行的示例,我已经在按钮中添加了按钮,该按钮将仅在按下按钮时添加具有虚拟数据的行。当您添加数据时,操作将非常相似,只需要循环或获取所有数据。

public class MainActivity extends AppCompatActivity {
Button btn;
TableLayout table;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.button);
    table = (TableLayout) findViewById(R.id.ourTableLayout);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TableRow newRow = new TableRow(MainActivity.this);
            TextView barCode = new TextView(MainActivity.this);
            TextView itemDesc = new TextView(MainActivity.this);
            TextView weightLine = new TextView(MainActivity.this);
            TextView rateAmount = new TextView(MainActivity.this);
            TextView makingAmount = new TextView(MainActivity.this);
            TextView netRate = new TextView(MainActivity.this);
            TextView total = new TextView(MainActivity.this);

            barCode.setText("Barcodes");
            itemDesc.setText("Descriptions");
            weightLine.setText("Weights");
            rateAmount.setText("Rates");
            makingAmount.setText("Makings");
            netRate.setText("Nets");
            total.setText("Totals");

            newRow.addView(barCode);
            newRow.addView(itemDesc);
            newRow.addView(weightLine);
            newRow.addView(rateAmount);
            newRow.addView(makingAmount);
            newRow.addView(netRate);
            newRow.addView(total);
            table.addView(newRow);

        }
    });
  }
}

And after a few button clicks we have: OnClick动态地向TableLayout添加新行,并从数据库中获取值,在添加的行中显示它并给出总计

点击几下按钮后,我们有:

Now as stated you will definitely need to do some legwork on your end to parse the data correctly but this should be more than enough to get you up and running.

现在如上所述,你肯定需要在你的头上做一些工作来正确地解析数据,但这应该足以让你开始运行。