请帮助我...我对Android开发很新,现在尝试将SQLite
数据检索到TableLayout
。程序将检索属于该名称的信息,最后显示在TableLayout
中。但是当我点击button
中的WorkDetailsTable.java
时,它就崩溃了。
DisplayData.java
public class DisplayData extends AppCompatActivity {
TableLayout table_layout;
InfoAPI sqlcon;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displaydata);
MyDatabaseHelper db=new MyDatabaseHelper(this);
InfoAPI I1=new InfoAPI(this);
sqlcon=new InfoAPI(this);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
table_layout = (TableLayout) findViewById(R.id.tableLayout1);
final String name1 = getIntent().getExtras().getString("name"); // pass from WorkDetailsTable.java
Toast.makeText(getApplicationContext(), name1, Toast.LENGTH_SHORT).show();
if (name1.equals("X X X")) ;
{
BuildTable(name1);
}
}
private void BuildTable(String name)
{
sqlcon.open();
Cursor c = sqlcon.readEntry(name); //refer to InfoAPI
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 ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
// inner for loop
for (int j = 0; j < cols; j++) {
TextView tv = new TextView(this);
tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.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();
}
}
InfoAPI.java
public Cursor readEntry(String name) {
String selectQuery = ("SELECT Weather,Date,Status FROM "+MyDatabaseHelper.TABLE_INFO+"WHERE Name= ?");
database=dbHelper.getWritableDatabase();
Cursor c = database.query(MyDatabaseHelper.TABLE_INFO,new String[]{MyDatabaseHelper.ID,MyDatabaseHelper.Name,MyDatabaseHelper.Weather,MyDatabaseHelper.Date,MyDatabaseHelper.Status},MyDatabaseHelper.Name+"=?", new String[] { String.valueOf(name)}, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
displaydata.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" >
<TextView
android:id="@+id/weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weather" />
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Date" />
<TextView
android:id="@+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Status" />
</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>
LogCat错误
10-10 08:52:04.691 6844-6844/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.project, PID: 6844
java.lang.ArithmeticException: divide by zero
at android.widget.TableLayout.mutateColumnsWidth(TableLayout.java:584)
at android.widget.TableLayout.shrinkAndStretchColumns(TableLayout.java:573)
at android.widget.TableLayout.measureVertical(TableLayout.java:471)
at android.widget.TableLayout.onMeasure(TableLayout.java:436)
at android.view.View.measure(View.java:18788)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1283)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.widget.ScrollView.onMeasure(ScrollView.java:340)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
错误来自InfoAPI吗?如何解决这个问题?我们非常感谢任何建议。
为了您的信息,我的信息表(身份证,姓名,天气,日期,状态)中有5个数据,但我只想检索天气,日期和状态。
答案 0 :(得分:0)
在table layout
android:shrinkColumns="*"
android:stretchColumns="*"