2016-07-29 3 views
-5

나는 내가해야 할 일을 확신하지 못합니다. 다양한 코드를 시도했지만 아무도 작동하지 않습니다.SQLite 데이터베이스를 테이블 레이아웃에 표시하는 방법은 무엇입니까?

"순위", "팀 이름"및 "완료 됨"열이있는 테이블을 표시하려고합니다. 순위는 각 팀의 위치 (완료 한 위치 수에 따라)를 나타내고 완료 열은 각 팀이 현재 완료 한 위치의 수를 의미합니다.

아래 데이터베이스 테이블은 내가 사용하고있는 테이블입니다. 이 표에 표시 할 UI의 이미지는 table UI

어떻게 접근해야합니까? 코드를 코딩 할 수 있습니다. arraylist 또는 커서를 사용하는 방법을 시도했지만 작동하지 않습니다.

내 데이터베이스

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

내 테이블 레이아웃 코드는

<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

사용자 지정 목록보기 사용 – Razgriz

답변

1

난 당신이 목록보기 작업을 SimpleCursorAdapter 커서를 사용할 수 있다고 생각합니다. 결과는 원하는 것과 같습니다.

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

감사를 참조하시기 바랍니다.

+0

팀 수에 따라 완료된 num 수를 계산하고 데이터베이스에 다시 저장하는 방법을 알고 있습니까? –

+0

사용 : 전용 커서 getNumberCompleted (SQLiteDatabase db, String teamName) { String selectNumberCompleted = "gameMaster_table에서 Num_Completed를 선택하십시오. 여기서 TeamName ="+ teamName; 커서 cursor = db.rawQuery (selectNumberCompleted, null); return cursor; } – sonnv1368

+0

어디에서이 메서드를 호출합니까? 안드로이드 안드로이드 스튜디오 –

관련 문제