java - OnClick 动态添加一个新行到 TableLayout 并从数据库中获取值将其显示在添加的行中并给出总计

标签 java android mysql database android-volley

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

}

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

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

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

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

最佳答案

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

这是桌面屏幕布局

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

This is our table description

现在,正如我所说,我不会过度解析您的 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);

        }
    });
  }
}

点击几次按钮后,我们得到: enter image description here

现在,如上所述,您肯定需要做一些跑腿工作才能正确解析数据,但这应该足以让您启动并运行。

关于java - OnClick 动态添加一个新行到 TableLayout 并从数据库中获取值将其显示在添加的行中并给出总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33565585/

相关文章:

java - 特定的 RepaintManager 可以用于特定的 JPanel 吗?

java - json.org 零被保存为字符串而不是数字

java - 如何使用gson序列化Pair的ArrayList

android - Boot BroadcastReceiver 在小米设备上不起作用

mysql - 如何在一个查询中使用 SUM 和 COUNT mysql

mysql 确保一次只能访问一次

java - 如何用真实数据库表填充 h2 内存表?

java.lang.String 无法转换为 JSONObject,其中值已转换为 JSONObject

java - 使用 Android 应用程序使用模拟退火的最短路径

php - (MySQL)需要从多个表中获取匹配分数,并按它们的分数之和排序