我的 json 响应的输出显示数据库中的最后一项

output of my json response is displaying last item in the database

我试图在表格布局中显示我的数据库的内容,我已经从数据库中获取数据,并将其设置为在我的 android 应用程序中显示,但是当数据显示时,它覆盖之前的数据获取,只显示最后一项。

这是我的代码的一个例外形式,我认为我的代码有误

 @Override
    protected void onPostExecute(String result) {
       /*     TextView textView= (TextView)findViewById(R.id.textview);
            textView.setText(result);*/

         json_string = result;


        try {
            jsonObject = new JSONObject(json_string);
            jsonArray = jsonObject.getJSONArray("server_response");
            int count = 0;
            String id, inv_id, refnumber, description, details, qty, unit_price, amount;
            Double subTotal = 00.00;
            int sn = 1;
            Double dAmount;
            while (count < jsonArray.length()) {
                JSONObject JO = jsonArray.getJSONObject(count);

                description = JO.getString("description");

                qty = JO.getString("qty");
                unit_price = JO.getString("unit_price");
                amount = JO.getString("amount");

                txtDescription.setText(description);
                txtQty.setText(qty);
                txtUnit_price.setText(unit_price);
                txtAmount.setText(amount);
                txtsn.setText(String.valueOf(sn));
                count++;
                sn++;


                try {
                    subTotal = subTotal + Double.valueOf(amount);
                    //  dAmount = Double.valueOf(amount);
                } catch (NumberFormatException e) {
                    subTotal = 00.00;
                }


            }

            txtSubTotal.setText(String.valueOf(subTotal));
            double VAT = 0.05*(subTotal);
            txtVat.setText(String.valueOf(VAT));

            Double Total = VAT + subTotal;

            txtTotal.setText(String.valueOf(Total));
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

我的XML布局如下

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ng.proartisan.invoiceapp.MainActivity">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

<TableRow>
<TextView
    android:text="S/N"
    android:background="@drawable/cellborder"
    android:layout_weight="1"
    android:gravity="center"
    android:padding="5dp"
    android:textStyle="bold"

    />
    <TextView
        android:text="Description"
        android:background="@drawable/cellborder"
        android:padding="5dp"
        android:layout_weight="4"
        android:gravity="center"
        android:textStyle="bold"
        />
    <TextView
        android:text="QTY"
        android:background="@drawable/cellborder"
        android:layout_weight="1"
        android:padding="5dp"
        android:gravity="center"
        android:textStyle="bold"
        />
    <TextView
        android:text="Unit Price"
        android:background="@drawable/cellborder"
        android:layout_weight="2"
        android:padding="5dp"
        android:gravity="center"
        android:textStyle="bold"
        />
    <TextView
        android:text="Amount"
        android:background="@drawable/cellborder"
        android:layout_weight="2"
        android:padding="5dp"
        android:gravity="center"
        android:textStyle="bold"
        />

</TableRow>
        <TableRow>
            <TextView
                android:id="@+id/sn"
                android:text="S/N"
                android:background="@drawable/cellborder"
                android:layout_weight="1"
                android:gravity="center"
                android:padding="5dp"

                />
            <TextView
                android:id="@+id/description"
                android:text="Description"
                android:background="@drawable/cellborder"
                android:padding="5dp"
                android:layout_weight="4"
                android:gravity="center"
                />
            <TextView
                android:id="@+id/qty"
                android:text="QTY"
                android:background="@drawable/cellborder"
                android:layout_weight="1"
                android:padding="5dp"
                android:gravity="center"
                />
            <TextView
                android:id="@+id/unit_price"
                android:text="Unit Price"
                android:background="@drawable/cellborder"
                android:layout_weight="2"
                android:padding="5dp"
                android:gravity="center"
                />
            <TextView
                android:id="@+id/amount"
                android:text="Amount"
                android:background="@drawable/cellborder"
                android:layout_weight="2"
                android:padding="5dp"
                android:gravity="center"
                />

        </TableRow>
        <TableRow>
            <TextView

                android:background="@drawable/cellborder"
                android:layout_weight="1"
                android:gravity="center"
                android:padding="5dp"

                />
            <TextView

                android:text="SubTotal"
                android:background="@drawable/cellborder"
                android:padding="5dp"
                android:layout_weight="4"
                android:gravity="center"
                />
            <TextView

                android:background="@drawable/cellborder"
                android:layout_weight="1"
                android:padding="5dp"
                android:gravity="center"
                />
            <TextView

                android:background="@drawable/cellborder"
                android:layout_weight="2"
                android:padding="5dp"
                android:gravity="center"
                />
            <TextView
                android:id="@+id/subtotal"
                android:text=""
                android:background="@drawable/cellborder"
                android:layout_weight="2"
                android:padding="5dp"
                android:gravity="center"
                />

        </TableRow>
        <TableRow>
            <TextView

                android:background="@drawable/cellborder"
                android:layout_weight="1"
                android:gravity="center"
                android:padding="5dp"

                />
            <TextView

                android:text="VAT"
                android:background="@drawable/cellborder"
                android:padding="5dp"
                android:layout_weight="4"
                android:gravity="center"
                />
            <TextView

                android:background="@drawable/cellborder"
                android:layout_weight="1"
                android:padding="5dp"
                android:gravity="center"
                />
            <TextView

                android:background="@drawable/cellborder"
                android:layout_weight="2"
                android:padding="5dp"
                android:gravity="center"
                />
            <TextView
                android:id="@+id/vat"
                android:text=""
                android:background="@drawable/cellborder"
                android:layout_weight="2"
                android:padding="5dp"
                android:gravity="center"
                />

        </TableRow>
        <TableRow>
            <TextView

                android:background="@drawable/cellborder"
                android:layout_weight="1"
                android:gravity="center"
                android:padding="5dp"

                />
            <TextView

                android:text="Total"
                android:background="@drawable/cellborder"
                android:padding="5dp"
                android:layout_weight="4"
                android:gravity="center"
                />
            <TextView

                android:background="@drawable/cellborder"
                android:layout_weight="1"
                android:padding="5dp"
                android:gravity="center"
                />
            <TextView

                android:background="@drawable/cellborder"
                android:layout_weight="2"
                android:padding="5dp"
                android:gravity="center"
                />
            <TextView
                android:id="@+id/total"
                android:text=""
                android:background="@drawable/cellborder"
                android:layout_weight="2"
                android:padding="5dp"
                android:gravity="center"
                />

        </TableRow>
    </TableLayout>




</LinearLayout>
</ScrollView>

请帮忙

您正在循环中将数据设置到同一视图中,其他解决问题的方法如创建新布局并添加到您的布局示例中:XML 文件 layout_item_description.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TableRow>
        <TextView
            android:id="@+id/tv_sn"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="5dp"
            android:text="S/N"
            android:textStyle="bold"
            />
        <TextView
            android:id="@+id/tv_description"
            android:layout_weight="4"
            android:gravity="center"
            android:padding="5dp"
            android:text="Description"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/tv_qty"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="5dp"
            android:text="QTY"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/tv_unit_price"
            android:layout_weight="2"
            android:gravity="center"
            android:padding="5dp"
            android:text="Unit Price"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/tv_amount"
            android:layout_weight="2"
            android:gravity="center"
            android:padding="5dp"
            android:text="Amount"
            android:textStyle="bold" />
    </TableRow>
</TableLayout>

将此项目视图添加到您的布局中。您的 XML 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ng.proartisan.invoiceapp.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TableRow>
                <TextView
                    android:text="S/N"
                    android:background="@drawable/cellborder"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:padding="5dp"
                    android:textStyle="bold"
                    />
                <TextView
                    android:text="Description"
                    android:background="@drawable/cellborder"
                    android:padding="5dp"
                    android:layout_weight="4"
                    android:gravity="center"
                    android:textStyle="bold"
                    />
                <TextView
                    android:text="QTY"
                    android:background="@drawable/cellborder"
                    android:layout_weight="1"
                    android:padding="5dp"
                    android:gravity="center"
                    android:textStyle="bold"
                    />
                <TextView
                    android:text="Unit Price"
                    android:background="@drawable/cellborder"
                    android:layout_weight="2"
                    android:padding="5dp"
                    android:gravity="center"
                    android:textStyle="bold"
                    />
                <TextView
                    android:text="Amount"
                    android:background="@drawable/cellborder"
                    android:layout_weight="2"
                    android:padding="5dp"
                    android:gravity="center"
                    android:textStyle="bold"
                    />
            </TableRow>
</TableLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/ll_data_description"/>
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <TableRow>
                <TextView
                    android:background="@drawable/cellborder"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:padding="5dp"
                   />
                <TextView
                   android:text="SubTotal"
                    android:background="@drawable/cellborder"
                    android:padding="5dp"
                    android:layout_weight="4"
                    android:gravity="center"
                    />
                <TextView
                   android:background="@drawable/cellborder"
                    android:layout_weight="1"
                    android:padding="5dp"
                    android:gravity="center"
                    />
                <TextView
                    android:background="@drawable/cellborder"
                    android:layout_weight="2"
                    android:padding="5dp"
                    android:gravity="center"
                    />
                <TextView
                    android:id="@+id/subtotal"
                    android:text=""
                    android:background="@drawable/cellborder"
                    android:layout_weight="2"
                    android:padding="5dp"
                    android:gravity="center"
                    />
            </TableRow>
            <TableRow>
                <TextView
                   android:background="@drawable/cellborder"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:padding="5dp"
                    />
                <TextView
                    android:text="VAT"
                    android:background="@drawable/cellborder"
                    android:padding="5dp"
                    android:layout_weight="4"
                    android:gravity="center"
                    />
                <TextView
                    android:background="@drawable/cellborder"
                    android:layout_weight="1"
                    android:padding="5dp"
                    android:gravity="center"
                    />
                <TextView
                    android:background="@drawable/cellborder"
                    android:layout_weight="2"
                    android:padding="5dp"
                    android:gravity="center"
                    />
                <TextView
                    android:id="@+id/vat"
                    android:text=""
                    android:background="@drawable/cellborder"
                    android:layout_weight="2"
                    android:padding="5dp"
                    android:gravity="center"
                   />
            </TableRow>
            <TableRow>
                <TextView
                    android:background="@drawable/cellborder"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:padding="5dp"
                    />
                <TextView
                   android:text="Total"
                    android:background="@drawable/cellborder"
                    android:padding="5dp"
                    android:layout_weight="4"
                    android:gravity="center"
                    />
                <TextView
                    android:background="@drawable/cellborder"
                    android:layout_weight="1"
                    android:padding="5dp"
                    android:gravity="center"
                    />
                <TextView
                    android:background="@drawable/cellborder"
                    android:layout_weight="2"
                    android:padding="5dp"
                    android:gravity="center"
                    />
                <TextView
                    android:id="@+id/total"
                    android:text=""
                    android:background="@drawable/cellborder"
                    android:layout_weight="2"
                    android:padding="5dp"
                    android:gravity="center"
                    />
            </TableRow>
        </TableLayout>
    </LinearLayout>
</ScrollView>

添加LinearLayout ll_data_description = (LinearLayout)findViewById(R.id.ll_data_description);

@Override
        protected void onPostExecute(String result) {
       /*     TextView textView= (TextView)findViewById(R.id.textview);
            textView.setText(result);*/

            json_string = result;


            try {
                jsonObject = new JSONObject(json_string);
                jsonArray = jsonObject.getJSONArray("server_response");
                int count = 0;
                String id, inv_id, refnumber, description, details, qty, unit_price, amount;
                Double subTotal = 00.00;
                int sn = 1;
                Double dAmount;
                while (count < jsonArray.length()) {
                    JSONObject JO = jsonArray.getJSONObject(count);


                    View layout_item_description = getLayoutInflater().inflate(R.layout.layout_item_description, null);
                    ((TextView) layout_item_description.findViewById(R.id.tv_sn)).setText(sn+"");

                    description = JO.getString("description");

                    qty = JO.getString("qty");
                    unit_price = JO.getString("unit_price");
                    amount = JO.getString("amount");

                    ((TextView) layout_item_description.findViewById(R.id.tv_description)).setText(sn+"");
                    ((TextView) layout_item_description.findViewById(R.id.tv_qty)).setText(sn+"");
                    ((TextView) layout_item_description.findViewById(R.id.tv_unit_price)).setText(sn+"");
                    ((TextView) layout_item_description.findViewById(R.id.tv_amount)).setText(sn+"");

                    count++;
                    sn++;
                    ll_data_description.addView(layout_item_description);

                    try {
                        subTotal = subTotal + Double.valueOf(amount);
                        //  dAmount = Double.valueOf(amount);
                    } catch (NumberFormatException e) {
                        subTotal = 00.00;
                    }


                }

                txtSubTotal.setText(String.valueOf(subTotal));
                double VAT = 0.05*(subTotal);
                txtVat.setText(String.valueOf(VAT));

                Double Total = VAT + subTotal;

                txtTotal.setText(String.valueOf(Total));
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }