2016-07-31 9 views
0

Ich versuche, Daten aus der SQLite-Datenbanktabelle abzurufen und sie als Table-Layout in Android anzuzeigen. Es zeigt jedoch nur den Titel und die Textansicht an. Was stimmt nicht mit den Codes?SQLite-Daten in Android-Tabellenlayout anzeigen

Graf.java

public class Graf extends AppCompatActivity { 

DatabaseHelper myDb; 
TextView dt, water, inc; 
TableLayout tableLayout; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_graf); 
    myDb=new DatabaseHelper(this); 
    dt=(TextView)findViewById(R.id.date); 
    water=(TextView)findViewById(R.id.water); 
    inc=(TextView)findViewById(R.id.inc); 
    tableLayout=(TableLayout)findViewById(R.id.tableLayout1); 



    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    final String date=getDate(); 
    Toast.makeText(getApplicationContext(),date, Toast.LENGTH_SHORT).show(); 

    BuildTable(date); 




} 

private void BuildTable(String date){ 

    Cursor c=myDb.readEntry(date); 

    int rows=c.getCount(); 
    int cols=c.getColumnCount(); 

    c.moveToFirst(); 

    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)); 

     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(); 

     tableLayout.addView(row); 
    } 


} 





/*public void getData(){ 

    buttonA.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 


       Cursor res = myDb.getAllData(); 
       if (res.getCount() == 0) { 
        showMessage("Ralat", "Tiada Rekod."); 
       } 

       StringBuffer buffer = new StringBuffer(); 
       while (res.moveToNext()) { 
        buffer.append("Tarikh  : " + res.getString(1) + "\n"); 
        buffer.append("Air Diminum : " + res.getString(2) + "\n"); 
        buffer.append("Pencapaian : " + res.getString(3) + "\n\n"); 
        showMessage("Laporan", buffer.toString()); 
       } 


     } 
    }); 
} 

public void showMessage(String title, String message){ 
    AlertDialog.Builder builder=new AlertDialog.Builder(this); 
    builder.setCancelable(true); 
    builder.setTitle(title); 
    builder.setMessage(message); 
    builder.show(); 
}*/ 



@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_graf, menu); 
    return true; 
} 

private String getDate() { 
    SimpleDateFormat dateFormat = new SimpleDateFormat(
      "yyyy-MM-dd", Locale.getDefault()); 
    Date date = new Date(); 
    return dateFormat.format(date); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

} 

activity_graf.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:tools="http://schemas.android.com/tools" 
android:orientation="vertical" 
tools:context="info.androidhive.materialdesign.activity.Graf"> 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:textSize="45dp" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="LAPORAN MINGGUAN ANDA" 
    android:id="@+id/textView7" 
    android:textStyle="bold" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 



<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/date" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Tarikh" /> 

    <TextView 
     android:id="@+id/water" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:hint="Penambahan Air" /> 

    <TextView 
     android:id="@+id/inc" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Peningkatan Peratus" /> 
</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"> 
    </TableLayout> 
</ScrollView> 



</LinearLayout> 

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper { 

DatabaseHelper myDb; 
SQLiteDatabase db = this.getWritableDatabase(); 

public static final String DATABASE_NAME = "HU.db"; 
public static final String TABLE_NAME = "User_table"; 
public static final String COL_1 = "ID"; 
public static final String COL_2 = "NAME"; 
public static final String COL_4 = "GENDER"; 
public static final String COL_5 = "WEIGHT"; 
public static final String COL_6 = "HEIGHT"; 
public static final String COL_7 = "ACTIVENESS"; 
public static final String COL_8 = "TARGET"; 

public static final String TABLE2_NAME = "Drink_Table"; 
public static final String COL2_1 = "DRINK_ID"; 
public static final String COL2_2 = "DATE"; 
public static final String COL2_3 = "AMOUNT"; 
public static final String COL2_4 = "PERCENT"; 
public static final String COL2_5 = "ID"; 

public DatabaseHelper(Context context) { 
    super(context, DATABASE_NAME, null, 1); 

} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("create table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, GENDER TEXT, WEIGHT DOUBLE, HEIGHT DOUBLE, ACTIVENESS INTEGER, TARGET DOUBLE)"); 
    db.execSQL("create table " + TABLE2_NAME + " (DRINK_ID INTEGER PRIMARY KEY AUTOINCREMENT, DATE DATETIME DEFAULT CURRENT_DATE, AMOUNT DOUBLE, PERCENT DOUBLE, ID INTEGER, FOREIGN KEY(ID) REFERENCES " + TABLE_NAME + "(ID))"); 
    db.execSQL("create table "+TABLE3_NAME+" (Entry_ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , ID INTEGER, DATE DATETIME DEFAULT CURRENT_DATE, TOTALAMOUNT DOUBLE, TOTAL PERC DOUBLE, FOREIGN KEY(DATE) REFERENCES "+TABLE2_NAME+"(DATE), FOREIGN KEY(ID) REFERENCES "+TABLE_NAME+"(ID))"); 


} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXIST " + TABLE_NAME); 
    onCreate(db); 

} 

public boolean insertData(String name, String gender, String weight, String height, String activeness, String target) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COL_2, name); 
    contentValues.put(COL_4, gender); 
    contentValues.put(COL_5, weight); 
    contentValues.put(COL_6, height); 
    contentValues.put(COL_7, activeness); 
    contentValues.put(COL_8, target); 
    long result = db.insert(TABLE_NAME, null, contentValues); 
    if (result == -1) 
     return false; 
    else 
     return true; 

} 

public boolean updateData(String id, String name, String gender, String weight, String height, String activeness, String target) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COL_1, id); 
    contentValues.put(COL_2, name); 
    contentValues.put(COL_4, gender); 
    contentValues.put(COL_5, weight); 
    contentValues.put(COL_6, height); 
    contentValues.put(COL_7, activeness); 
    contentValues.put(COL_8, target); 
    db.update(TABLE_NAME, contentValues, "ID = ?", new String[]{id}); 
    return true; 
} 

public boolean addWater(String date, String id, double amount) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    double target = getTargetu(id); 
    contentValues.put(COL2_2, date); 
    contentValues.put(COL2_3, amount); 
    contentValues.put(COL2_4, (amount/target) * 100); 
    contentValues.put(COL2_5, id); 
    long result = db.insert(TABLE2_NAME, null, contentValues); 
    if (result == -1) 
     return false; 
    else 
     return true; 

} 

public Cursor readEntry(String date) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    String selectQuery = ("SELECT DATE, AMOUNT, PERCENT FROM "+TABLE2_NAME+"WHERE DATE= ?"); 
    Cursor c = db.query(TABLE2_NAME,new String[]{COL2_2,COL2_3,COL2_4},COL2_2+"=?", new String[] { String.valueOf(date)}, null, null, null, null); 

    if (c != null) { 
     c.moveToFirst(); 
    } 
    return c; 

} 

private String getDate() { 
    SimpleDateFormat dateFormat = new SimpleDateFormat(
      "yyyy-MM-dd", Locale.getDefault()); 
    Date date = new Date(); 
    return dateFormat.format(date); 
} 
} 

Antwort

0

denke ich, das Problem mit Kommissionierung falsch Layout params ist. Sollte sein:

row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
    TableRow.LayoutParams.WRAP_CONTENT)); 
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 
    TableRow.LayoutParams.WRAP_CONTENT)); 

Tabellenlayout erfordert Tabelle Layoutparameter.

+0

Ich habe versucht, aber es gibt keine Änderungen. Die Tabelle wird nicht angezeigt –