从sqlite数据库中的exist表在tablelayout中动态显示数据表

jslywgbw  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(216)

我是新的数据库和表格布局。我想向活动中的用户显示我的exist datatable。但我不知道该怎么做。我试了一点。但还不能这么做。但我的行是列,列是行。我的代码应该是什么!表格布局应该可以水平滚动。谢谢你的回答。
我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.shubho.speedypaper17.SavedDataActivity">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text"
    android:textSize="30dp"
    android:gravity="center"
    android:textColor="@color/black"/>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none">
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/table"/>
</ScrollView>
</RelativeLayout>

以及我的java代码:

public class SavedDataActivity extends AppCompatActivity implements 
View.OnClickListener{
String tableName;
String[] columnNames;
String[] array = null;
SQLiteDatabase db;
TextView textView;
ArrayList<String> ar =null;
Cursor c;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_saved_data);
    Intent intent = getIntent();
    tableName = intent.getExtras().getString("abc");

    db = openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
    c = db.rawQuery("SELECT * FROM " + tableName, null);

    textView = (TextView) findViewById(R.id.text);
    textView.setText("" + tableName + "");

    columnNames = c.getColumnNames();

    addHeaders();
    addData();
}

private TextView getTextView(int id, String title, int color, int typeface, 
int bgColor) {
    TextView tv = new TextView(this);
    tv.setId(id);
    tv.setText(title.toUpperCase());
    tv.setTextColor(color);
    tv.setPadding(40, 40, 40, 40);
    tv.setTypeface(Typeface.DEFAULT, typeface);
    tv.setBackgroundColor(bgColor);
    tv.setLayoutParams(getLayoutParams());
    tv.setOnClickListener(this);
    return tv;
}

@NonNull
private LayoutParams getLayoutParams() {
    LayoutParams params = new LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT);
    params.setMargins(2, 0, 0, 2);
    return params;
}

@NonNull
private TableLayout.LayoutParams getTblLayoutParams() {
    return new TableLayout.LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT);
}
//This is my header of the table column.
public void addHeaders() {
    TableLayout tl = findViewById(R.id.table);
    TableRow tr = new TableRow(this);
    tr.setLayoutParams(getLayoutParams());
    for(int i =0; i<columnNames.length; i++){
        tr.addView(getTextView(0, ""+columnNames[i]+"", Color.WHITE, 
Typeface.BOLD, Color.BLUE));
    }
    tl.addView(tr, getTblLayoutParams());
}

/**
 * This function add the data to the table

**/

public void addData() {

    for (int i = 0; i < columnNames.length; i++) {
        array = null;
        ar = null;
        ar = new ArrayList();
        c.moveToFirst();
        do{
            ar.add(c.getString(i));

        }while (c.moveToNext());
        array = new String[ar.size()];
        array = ar.toArray(array);
        TableLayout tl = findViewById(R.id.table);
        TableRow tr = new TableRow(this);
        tr.setLayoutParams(getLayoutParams());
        for (int j = 0; j < array.length; j++) {

            tr.addView(getTextView(j + 1, array[j], Color.WHITE, 
Typeface.NORMAL, ContextCompat.getColor(this, R.color.colorAccent)));

        }
        tl.addView(tr, getTblLayoutParams());
    }
}

@Override
public void onClick(View v) {
    int id = v.getId();
    TextView tv = findViewById(id);
    if (null != tv) {
        Log.i("onClick", "Clicked on row :: " + id);
        Toast.makeText(this, "Clicked on row :: " + id + ", Text :: " + 
tv.getText(), Toast.LENGTH_SHORT).show();
    }
}
}

我完成工作的截图是:

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题