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

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

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.example.shubho.speedypaper17.SavedDataActivity">
  8. <TextView
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:id="@+id/text"
  12. android:textSize="30dp"
  13. android:gravity="center"
  14. android:textColor="@color/black"/>
  15. <ScrollView
  16. android:layout_width="match_parent"
  17. android:layout_height="match_parent"
  18. android:scrollbars="none">
  19. <TableLayout
  20. android:layout_width="match_parent"
  21. android:layout_height="match_parent"
  22. android:id="@+id/table"/>
  23. </ScrollView>
  24. </RelativeLayout>

以及我的java代码:

  1. public class SavedDataActivity extends AppCompatActivity implements
  2. View.OnClickListener{
  3. String tableName;
  4. String[] columnNames;
  5. String[] array = null;
  6. SQLiteDatabase db;
  7. TextView textView;
  8. ArrayList<String> ar =null;
  9. Cursor c;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_saved_data);
  14. Intent intent = getIntent();
  15. tableName = intent.getExtras().getString("abc");
  16. db = openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
  17. c = db.rawQuery("SELECT * FROM " + tableName, null);
  18. textView = (TextView) findViewById(R.id.text);
  19. textView.setText("" + tableName + "");
  20. columnNames = c.getColumnNames();
  21. addHeaders();
  22. addData();
  23. }
  24. private TextView getTextView(int id, String title, int color, int typeface,
  25. int bgColor) {
  26. TextView tv = new TextView(this);
  27. tv.setId(id);
  28. tv.setText(title.toUpperCase());
  29. tv.setTextColor(color);
  30. tv.setPadding(40, 40, 40, 40);
  31. tv.setTypeface(Typeface.DEFAULT, typeface);
  32. tv.setBackgroundColor(bgColor);
  33. tv.setLayoutParams(getLayoutParams());
  34. tv.setOnClickListener(this);
  35. return tv;
  36. }
  37. @NonNull
  38. private LayoutParams getLayoutParams() {
  39. LayoutParams params = new LayoutParams(
  40. LayoutParams.MATCH_PARENT,
  41. LayoutParams.WRAP_CONTENT);
  42. params.setMargins(2, 0, 0, 2);
  43. return params;
  44. }
  45. @NonNull
  46. private TableLayout.LayoutParams getTblLayoutParams() {
  47. return new TableLayout.LayoutParams(
  48. LayoutParams.MATCH_PARENT,
  49. LayoutParams.WRAP_CONTENT);
  50. }
  51. //This is my header of the table column.
  52. public void addHeaders() {
  53. TableLayout tl = findViewById(R.id.table);
  54. TableRow tr = new TableRow(this);
  55. tr.setLayoutParams(getLayoutParams());
  56. for(int i =0; i<columnNames.length; i++){
  57. tr.addView(getTextView(0, ""+columnNames[i]+"", Color.WHITE,
  58. Typeface.BOLD, Color.BLUE));
  59. }
  60. tl.addView(tr, getTblLayoutParams());
  61. }
  62. /**
  63. * This function add the data to the table
  64. **/
  65. public void addData() {
  66. for (int i = 0; i < columnNames.length; i++) {
  67. array = null;
  68. ar = null;
  69. ar = new ArrayList();
  70. c.moveToFirst();
  71. do{
  72. ar.add(c.getString(i));
  73. }while (c.moveToNext());
  74. array = new String[ar.size()];
  75. array = ar.toArray(array);
  76. TableLayout tl = findViewById(R.id.table);
  77. TableRow tr = new TableRow(this);
  78. tr.setLayoutParams(getLayoutParams());
  79. for (int j = 0; j < array.length; j++) {
  80. tr.addView(getTextView(j + 1, array[j], Color.WHITE,
  81. Typeface.NORMAL, ContextCompat.getColor(this, R.color.colorAccent)));
  82. }
  83. tl.addView(tr, getTblLayoutParams());
  84. }
  85. }
  86. @Override
  87. public void onClick(View v) {
  88. int id = v.getId();
  89. TextView tv = findViewById(id);
  90. if (null != tv) {
  91. Log.i("onClick", "Clicked on row :: " + id);
  92. Toast.makeText(this, "Clicked on row :: " + id + ", Text :: " +
  93. tv.getText(), Toast.LENGTH_SHORT).show();
  94. }
  95. }
  96. }

我完成工作的截图是:

暂无答案!

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

相关问题