2016-07-29 25 views
-5

Ich bin unsicher was ich tun soll. Ich habe verschiedenen Code ausprobiert, aber keiner funktioniert.Wie kann ich meine SQLite-Datenbank in einem Tabellenlayout anzeigen?

Ich möchte eine Tabelle mit den Spalten "Ranking", "Team Name" und "Completed" anzeigen. Die Rangliste bezieht sich auf die Position jedes Teams (abhängig von der Anzahl der Orte, die sie absolviert haben) und die Spalte Abgeschlossen bedeutet die Anzahl der Standorte, die jedes Team aktuell absolviert hat.

Die folgende Datenbanktabelle ist die, die ich verwende. Dies ist das Bild der Benutzeroberfläche, die ich in der Tabelle anzeigen möchte: table UI

Bitte sagen Sie mir, wie soll ich es nähern? und die Codes, mit denen ich sie codieren kann. Ich habe versucht, wie arraylist oder Cursor, aber es funktioniert nicht.

Meine Datenbank

public class DatabaseHelperGameMaster extends SQLiteOpenHelper { 
//private static final int DATABASE_VERSION = 1; 
public static final String DATABASE_NAME = "gemquest2.db"; 
public static final String TABLE_NAME = "gameMaster_table"; 
public static final String COL_ID = "ID"; 
public static final String COL_Team_Name = "TeamName"; 
public static final String COL_Location = "Location"; 

public DatabaseHelperGameMaster(Context context) { 
    super(context, DATABASE_NAME, null, 1); 
    SQLiteDatabase db = this.getWritableDatabase(); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("create table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, TeamName TEXT, Location INTEGER)"); 

    db.execSQL("INSERT INTO " + TABLE_NAME + "(TeamName, Location) VALUES ('TeamBurden', 1)"); 
    db.execSQL("INSERT INTO " + TABLE_NAME + "(TeamName, Location) VALUES ('Fantastic4', 1)"); 
    db.execSQL("INSERT INTO " + TABLE_NAME + "(TeamName, Location) VALUES ('TeamMN', 1)"); 
    db.execSQL("INSERT INTO " + TABLE_NAME + "(TeamName, Location) VALUES ('JamesFamily', 2)"); 
    db.execSQL("INSERT INTO " + TABLE_NAME + "(TeamName, Location) VALUES ('GossipBoys', 2)"); 
    db.execSQL("INSERT INTO " + TABLE_NAME + "(TeamName, Location) VALUES ('TeamMN', 3)"); 
    db.execSQL("INSERT INTO " + TABLE_NAME + "(TeamName, Location) VALUES ('Fantastic4', 3)"); 
    db.execSQL("INSERT INTO " + TABLE_NAME + "(TeamName, Location) VALUES('TeamBurden', 5)"); 


} 

@Override 
public void onUpgrade(SQLiteDatabase db, int i, int i1) { 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
    onCreate(db); 
} 

meinem Tisch Layout-Code

<TableLayout 
        android:layout_width="fill_parent" 
        android:layout_height="300dp" 
        > 

        <TableRow 
        android:background="@drawable/table_border_style" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_margin="10dp" 
        android:baselineAligned="false" 
        android:focusable="false"> 

        <TextView 

         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textAppearance="?android:attr/textAppearanceLarge" 
         android:text="Ranking" 
         android:id="@+id/txtRanking" 
         android:layout_margin="5dp" 
         android:textSize="24dp" /> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textAppearance="?android:attr/textAppearanceLarge" 
         android:text="Team Name" 
         android:id="@+id/txtName" 
         android:layout_margin="5dp" 
         android:textSize="24dp" /> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textAppearance="?android:attr/textAppearanceLarge" 
         android:text="Completed" 
         android:id="@+id/txtMissionCompleted" 
         android:layout_margin="5dp" 
         android:textSize="24dp" /> 
       </TableRow> 

        <TableRow 

        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
         android:background="#9ffdf4" 
         android:layout_marginLeft="10dp" 
         android:layout_marginRight="10dp"> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceLarge" 
          android:text="1" 
          android:id="@+id/txtP1" 
          android:textAlignment="center" 
          android:textSize="20dp" /> 


         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceMedium" 
          android:text="JamesFamily" 
          android:id="@+id/txtN1" 
          android:textAlignment="center" 
          android:textSize="20dp" /> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceMedium" 
          android:text="4" 
          android:id="@+id/txtM1" 
          android:textAlignment="center" 
          android:textSize="20dp" /> 
        </TableRow> 

        <TableRow 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:layout_marginLeft="10dp" 
         android:layout_marginTop="10dp" 
         android:layout_marginRight="10dp"> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceLarge" 
          android:text="2" 
          android:id="@+id/txtP2" 
          android:textAlignment="center" 
          android:textSize="20dp" /> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceMedium" 
          android:text="TeamMN" 
          android:id="@+id/txtN2" 
          android:textAlignment="center" 
          android:textSize="20dp" /> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceMedium" 
          android:text="3" 
          android:id="@+id/txtM2" 
          android:textAlignment="center" 
          android:textSize="20dp" /> 
        </TableRow> 

        <TableRow 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:background="#9ffdf4" 
         android:layout_marginLeft="10dp" 
         android:layout_marginTop="10dp" 
         android:layout_marginRight="10dp"> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceLarge" 
          android:text="3" 
          android:id="@+id/txtP3" 
          android:textAlignment="center" 
          android:textSize="20dp" /> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceMedium" 
          android:text="TeamBurden" 
          android:id="@+id/txtN3" 
          android:textAlignment="center" 
          android:textSize="20dp" /> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceMedium" 
          android:text="3" 
          android:id="@+id/txtM3" 
          android:textAlignment="center" 
          android:textSize="20dp" /> 
        </TableRow> 

        <TableRow 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:layout_marginLeft="10dp" 
         android:layout_marginTop="10dp" 
         android:layout_marginRight="10dp"> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceLarge" 
          android:text="4" 
          android:id="@+id/txtP4" 
          android:textAlignment="center" 
          android:textSize="20dp" /> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceMedium" 
          android:text="Fantastic4" 
          android:id="@+id/txtN4" 
          android:textAlignment="center" 
          android:textSize="20dp" /> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceMedium" 
          android:text="2" 
          android:id="@+id/txtM4" 
          android:textAlignment="center" 
          android:textSize="20dp" /> 
        </TableRow> 

        <TableRow 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:layout_marginLeft="10dp" 
         android:layout_marginTop="10dp" 
         android:layout_marginRight="10dp" 
         android:background="#9ffdf4"> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceLarge" 
          android:text="5" 
          android:id="@+id/txtP5" 
          android:textAlignment="center" 
          android:textSize="20dp" /> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceMedium" 
          android:text="GossipBoys" 
          android:id="@+id/txtN5" 
          android:textAlignment="center" 
          android:textSize="20dp" /> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceMedium" 
          android:text="1" 
          android:id="@+id/txtM5" 
          android:textAlignment="center" 
          android:textSize="20dp" /> 
        </TableRow> 

       </TableLayout> 
+0

Benutzerdefinierte Listenansicht verwenden – Razgriz

Antwort

1

Ich glaube, Sie SimpleCursorAdapter und Cursor können mit Listenansicht zu arbeiten. Ergebnis wird das selbe Sie wollen. Bitte lesen

https://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/

Dank.

+0

Wissen Sie, wie Sie die Zahl basierend auf der Nummer des Teamnamens berechnen und in der Datenbank speichern können? –

+0

verwenden: private Cursor getNumberCompleted (SQLiteDatabase db, Zeichenfolge teamName) { String selectNumberCompleted = "wählen Sie Num_Completed aus gameMaster_table, wobei TeamName =" + teamName; Cursor cursor = db.rawQuery (selectNumberCompleted, null); Rückkehrcursor; } – sonnv1368

+0

Wo rufe ich diese Methode? Tut mir leid zu fragen, einfach qns .. ich bin sehr neu in android Studio –