代码之家  ›  专栏  ›  技术社区  ›  Gionne Lapuz

将SQLite数据显示到TableRows中

  •  2
  • Gionne Lapuz  · 技术社区  · 7 年前

    这是我的活动主题。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>
    

    这是我的主要活动:

      @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 回复  |  直到 7 年前
        1
  •  4
  •   Sajidh Zahir    7 年前

    <?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 row layout table_项。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();
        }
    
        2
  •  1
  •   Omar Dhanish    7 年前

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

    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
  •   PounKumar Purushothaman    7 年前
    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();
        }
    }
    

        4
  •  -1
  •   Mohale    7 年前

    你可以使用一个自定义的listview或Recyclerview,它或多或少像一个listview,确切地说,它只是一个高级的listview。您可以使用此文档来理解recyclerviews https://developer.android.com/training/material/lists-cards.html