我是数据库和tableLayout的新手。我想在活动中向用户显示我存在的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();
}
}
}
我完成的工作的截图是: