将 SQLite 数据显示到 TableRows

Displaying SQLite data into TableRows

我正在创建一个应用程序来显示我的 SQLite 数据库中的数据,尽管我已经成功地在自定义 ListView 中显示数据。但我想知道如何才能以 Table 之类的形式显示数据,我可以显示第一行数据,但我不知道从这里去哪里。

这是我的ActivityMain.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.gin.customlistview.MainActivity">

 <TableLayout
    android:id="@+id/table"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="*"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SAVED MESSAGES"
        android:textSize="20dp"
        android:textColor="#000"
        android:textStyle="bold"
        android:gravity="center_horizontal"/>


    <TableRow
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_margin="1dp"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="15dp"
            android:textColor="#000"
            android:text="Phone Number"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:textStyle="bold"
            android:gravity="center"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textSize="15dp"
            android:textColor="#000"
            android:text="Username"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:textStyle="bold"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textSize="15dp"
            android:textColor="#000"
            android:text="Account Number"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:textStyle="bold"
            android:layout_column="2"
            />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow"
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="1dp">

        <TextView
            android:id="@+id/txtPhoneNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:textColor="#000"
            android:background="#FFFFFF"
            android:gravity="center"
            android:layout_margin="1dp" />

        <TextView
            android:id="@+id/txtUsername"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:textColor="#000"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            />

        <TextView
            android:id="@+id/txtAccountNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:textColor="#000"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center" />
    </TableRow>


</TableLayout>


</LinearLayout>

这是我的 MainActivity:

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    db = new textDatabase(this);

    txtPhoneNumber = (TextView) findViewById(R.id.txtPhoneNumber);
    txtUsername = (TextView) findViewById(R.id.txtUsername);
    txtAccountNumber = (TextView) findViewById(R.id.txtAccountNumber);

    checkPermission();
    showData();
}

public static void showData() {

 //  databaseNumber.clear();
 //   databaseUsername.clear();
 //   databaseAccountNumber.clear();

Cursor data = db.getSMS();
if (data.getCount() == 0) {
} else {
    data.moveToFirst();
    do {

        txtPhoneNumber.setText(data.getString(1));
        txtUsername.setText(data.getString(2));
        txtAccountNumber.setText(data.getString(3));

 //       databaseNumber.add(data.getString(1));
 //       databaseUsername.add(data.getString(2));
 //       databaseAccountNumber.add(data.getString(3));

    } while (data.moveToNext());
}
data.close();

//  adapters.notifyDataSetChanged();
}

在此先感谢您的帮助和建议! :D

您可以使用自定义列表视图或 Recyclerview,它或多或少类似于列表视图,准确地说,它只是一个高级列表视图。您可以使用此文档来了解 recyclerviews https://developer.android.com/training/material/lists-cards.html

尝试这样的事情

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:orientation="vertical">
</TableLayout>

创建 table 布局后创建 table 行布局 table_item.xml

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:layout_weight="0.25"
        android:gravity="center"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/title"
        android:layout_weight="0.25"
        android:gravity="center"/>
</TableRow>

在您的 activity 中遍历从 sql 检索到的值并将其添加到 table

private TableLayout tableLayout;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table);
        tableLayout=(TableLayout)findViewById(R.id.tableLayout);

       data.moveToFirst();
       do {
            View tableRow = LayoutInflater.from(this).inflate(R.layout.table_item,null,false);
            TextView name  = (TextView) tableRow.findViewById(R.id.name);
            TextView title  = (TextView) tableRow.findViewById(R.id.title);


            name.setText(data.getString(1));
            title.setText(data.getString(2));                
            tableLayout.addView(tableRow);

        } while (data.moveToNext());
        data.close();
    }

使用下面的代码,将根据数据动态创建,结果将类似于此

LinearLayout table = (LinearLayout) findViewById(R.id.table);

ArrayList<Tabledata> datalist = new ArrayList<>();

datalist = db.getyourSQLITEdata();


    // this method for table title
    createtitle(table);

    for(int i=0;i<datalist.size();i++){

        // this method is creating table rows and filling data
        Createtable(table,i); 

    }

}

table 标题

// your table title here (if you want title for your table you can use this)
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
void createtitle(LinearLayout main){
    LinearLayout row = new LinearLayout(this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 7);
    row.setLayoutParams(params);
    row.setOrientation(LinearLayout.HORIZONTAL);
    row.setWeightSum(100);

    // your table title here

    String[] title={"title1","title2","title3","title4","title5","title6","title7"};


    for (int i = 0; i < 7; i++) {
        LinearLayout.LayoutParams textparam;
        if(i == 2){
            textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 20.285);
        }else{
            textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 13.285);
        }
        TextView col1 = new TextView(this);
        col1.setText(title[i]);
        col1.setTextSize(13);
        col1.setPadding(0,0,0,0);
        col1.setLayoutParams(textparam);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            col1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            col1.setBackground(getResources().getDrawable(R.drawable.border,null));
        }else{
            col1.setBackground(getResources().getDrawable(R.drawable.border));
        }
        row.addView(col1);
    }
    main.addView(row);
}

table

 // your data table is created here 
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
void Createtable(LinearLayout main,int pos) {
    Tabledata data = datalist.get(pos);

    // this is where you get data from list and show it in table 
    String[] yourdata = {data.getdata1(),data.getdata2(),data.getdata3(),data.getdata4(),data.getdata5(),data.getdata6(),data.getdata7()};


    LinearLayout row = new LinearLayout(this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 7);
    row.setLayoutParams(params);
    row.setOrientation(LinearLayout.HORIZONTAL);
    row.setWeightSum(100);
    for (int i = 0; i < 7; i++) {
        LinearLayout.LayoutParams textparam;
        if(i == 2){
            textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 20.285);
        }else{
            textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 13.285);
        }
        TextView col1 = new TextView(this);
        col1.setText(yourdata[i]);
        col1.setTextSize(10);
        col1.setLines(1);
        col1.setPadding(0,0,0,0);
        col1.setLayoutParams(textparam);
        col1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            col1.setBackground(getResources().getDrawable(R.drawable.border,null));
        }else{
            col1.setBackground(getResources().getDrawable(R.drawable.border));
        }
        row.addView(col1);
    }
    main.addView(row);
}

Xml代码

<LinearLayout
                android:id="@+id/table"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="5dp">

            </LinearLayout>
TableLayout tableLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db = new textDatabase(this);

        txtPhoneNumber = (TextView) findViewById(R.id.txtPhoneNumber);
        txtUsername = (TextView) findViewById(R.id.txtUsername);
        txtAccountNumber = (TextView) findViewById(R.id.txtAccountNumber);

        //table layout
        tableLayout = (TableLayout) findViewById(R.id.table);

        checkPermission();
        showData();
    }

    public void showData() {

        TextView phn_no,user_name,account_no;
        //  databaseNumber.clear();
        //   databaseUsername.clear();
        //   databaseAccountNumber.clear();

        Cursor data = db.getSMS();
        if (data.getCount() == 0) {
        } else {
            data.moveToFirst();
            do {
                phn_no=new TextView(this);
                user_name=new TextView(this);
                account_no=new TextView(this);

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT);
                params.setMargins(1,1,1,1);
                params.gravity= Gravity.CENTER;

                phn_no.setBackgroundColor(Color.parseColor("#FFFFFF"));
                phn_no.setTextColor(Color.parseColor("#000"));
                phn_no.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
                phn_no.setLayoutParams(params);
                phn_no.setText(data.getString(1));

                user_name.setBackgroundColor(Color.parseColor("#FFFFFF"));
                user_name.setTextColor(Color.parseColor("#000"));
                user_name.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
                user_name.setLayoutParams(params);
                user_name.setText(data.getString(2));

                account_no.setBackgroundColor(Color.parseColor("#FFFFFF"));
                account_no.setTextColor(Color.parseColor("#000"));
                account_no.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
                account_no.setLayoutParams(params);
                account_no.setText(data.getString(3));

                TableRow row = new TableRow(this);
                row.addView(phn_no);
                row.addView(user_name);
                row.addView(account_no);

                tableLayout.addView(row);

                txtPhoneNumber.setText(data.getString(1));
                txtUsername.setText(data.getString(2));
                txtAccountNumber.setText(data.getString(3));

                //       databaseNumber.add(data.getString(1));
                //       databaseUsername.add(data.getString(2));
                //       databaseAccountNumber.add(data.getString(3));

            } while (data.moveToNext());
        }
        data.close();

//  adapters.notifyDataSetChanged();
    }
}

你可以用这个