PLEASE help me...I'm very new to android development and now trying to retrieve SQLite Data Into TableLayout. The program will retrieve the information which belongs to the name and finally display in TableLayout. But when I click the button in WorkDetailsTable.java, it crashed.
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://ift.tt/nIICcg"
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 Error
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)
Are the error came from InfoAPI? How to fix this? Any suggestions woukd be greatly appreciated.
For your information, I have 5 data in my Info Table (id,name,weather,date,status), but I want retrieve weather,date and status only.
Aucun commentaire:
Enregistrer un commentaire