将SQLite数据显示到TableRows中

时间:2017-08-20 17:49:02

标签: android sqlite tablelayout android-tablelayout

我正在创建一个可以显示来自我的SQLite数据库的数据的应用程序,尽管我已成功地在Custom ListViews中显示数据。但我想知道如何在表格中显示数据,我可以显示第一行数据虽然我不知道从哪里开始。

这是我的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

4 个答案:

答案 0 :(得分:1)

尝试这样的事情

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

创建tableLayout后创建表行布局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>

在您的活动内部迭代从sql检索到的值并将其添加到表

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

enter image description here

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

    }

}

表格标题

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

表格

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

答案 3 :(得分:0)

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

您可以使用此