Android - 以表格形式显示来自 sqlite 的所有数据

标签 android textview android-sqlite

下面是我用来在单个 TextView 中显示 sqlite 数据的代码。 到目前为止,一切都很好。但现在我需要数据以表格形式存在。我需要“billprint”字符串,因为我正在使用它在打印设备中打印详细信息。

那么,有什么方法可以在单个字符串文件中以表格形式打印/显示详细信息。?

private void displaydb() {
    // TODO Auto-generated method stub
    billprint = "";
    TextView tv = (TextView)findViewById(R.id.textView1);
    SQLiteDatabase myDB = null;

    try {
        myDB = getActivity().openOrCreateDatabase("shopcart1", android.content.Context.MODE_PRIVATE, null);

        String selectQuery = "SELECT  * FROM tbl_cartitems";
        Cursor c = myDB.rawQuery(selectQuery, null);

        int Column1 = c.getColumnIndex("productID");
        int Column2 = c.getColumnIndex("productName");
        int Column3 = c.getColumnIndex("productQuantity");
        int Column4 = c.getColumnIndex("productPrice");

        c.moveToFirst();
        if (c != null) {
            do {
                String pID = c.getString(Column1);
                String pName = c.getString(Column2);
                String pQua = c.getString(Column3);
                String pPri = c.getString(Column4);


                billprint = billprint +""+
                        pID+"   "+pName+"       "+""+pQua+"  "+pPri+"\n";
            } while (c.moveToNext());
        }

        tv.setText(pppid);
        tv.setTextSize(18F);
        tv.setTextColor(Color.RED);
    }

    catch (Exception e) {
        Toast.makeText(getActivity(), "Database Empty", Toast.LENGTH_LONG).show();
    } finally {
        if (myDB != null)
            myDB.close();
    }
}

到目前为止,输出是这种形式:

prod-1 vanilla ice cream 1 15
prod-2 chocolate ice cream 5 50
prod-3 milk ice cream 10 150

但我想要的输出应该是表格形式,如:

prod-1 vanilla ice cream    1   15
prod-2 chocolate ice cream  5   50
prod-3 milk ice cream       10  150

布局(activity_testing.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.naji.productorder.Testing" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />

如有任何帮助,我们将不胜感激。提前致谢。

最佳答案

不要使用这种编码标准。

  • 您应该通过在单独的类中拆分来分离 Ui 组件和 Db 操作。
  • 使用AsyncTask进行Db操作,并创建DTO类保存从sqlite中获取后要存储的数据
  • 在服务类中分离您的业务逻辑。 不要在一个类中进行所有操作。

关于Android - 以表格形式显示来自 sqlite 的所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36491652/

相关文章:

android - Estimote 和 Gimbal 信标的正确 BeaconLayout 是什么

android - textview 不支持多行

android - 如何设置 TextView 的文本?

android标签主机不刷新数据

android - Proguard 在导出应用程序时出现以下错误

android - 在 Intellij Idea 中更改目标 Android 设备

android - 如何在 Android 中设计同步服务

android - 如何设置TextView的textStyle如粗体、斜体

android - Android 9 中的空 SQLite 文件

java - 在 SQLite 数据库 android 中存储数据