Tell the library for tabular display of data, the maximum in appearance and functionality approximate to a DBGrid from Delphi or Microsoft DataGrid .
|
1 answer
use TableLayout
here is an example
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/fistname_et_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="firstName" /> <EditText android:id="@+id/lastname_et_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="lastname" /> <Button android:id="@+id/addmem_btn_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Add" /> </LinearLayout> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:shrinkColumns="*" android:stretchColumns="*" > </TableLayout> </ScrollView> </LinearLayout> manage table (MainActivity.java)
package com.pavan.sqlitetabledemo; import android.app.Activity; import android.app.ProgressDialog; import android.database.Cursor; import android.os.AsyncTask; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TableRow.LayoutParams; import android.widget.TextView; public class MainActivity extends Activity { TableLayout table_layout; EditText firstname_et, lastname_et; Button addmem_btn; SQLController sqlcon; ProgressDialog PD; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sqlcon = new SQLController(this); firstname_et = (EditText) findViewById(R.id.fistname_et_id); lastname_et = (EditText) findViewById(R.id.lastname_et_id); addmem_btn = (Button) findViewById(R.id.addmem_btn_id); table_layout = (TableLayout) findViewById(R.id.tableLayout1); BuildTable(); addmem_btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new MyAsync().execute(); } }); } private void BuildTable() { sqlcon.open(); Cursor c = sqlcon.readEntry(); int rows = c.getCount(); int cols = c.getColumnCount(); c.moveToFirst(); // outer for loop for (int i = 0; i < rows; i++) { TableRow row = new TableRow(this); row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); // inner for loop for (int j = 0; j < cols; j++) { TextView tv = new TextView(this); tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); tv.setBackgroundResource(R.drawable.cell_shape); tv.setGravity(Gravity.CENTER); tv.setTextSize(18); tv.setPadding(0, 5, 0, 5); tv.setText(c.getString(j)); row.addView(tv); } c.moveToNext(); table_layout.addView(row); } sqlcon.close(); } private class MyAsync extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); table_layout.removeAllViews(); PD = new ProgressDialog(MainActivity.this); PD.setTitle("Please Wait.."); PD.setMessage("Loading..."); PD.setCancelable(false); PD.show(); } @Override protected Void doInBackground(Void... params) { String firstname = firstname_et.getText().toString(); String lastname = lastname_et.getText().toString(); // inserting data sqlcon.open(); sqlcon.insertData(firstname, lastname); // BuildTable(); return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); BuildTable(); PD.dismiss(); } } } |