2012-09-15 2 views
1

열 제목에 내용을 정렬하는 방법은 다음과 같습니다.항목을 열 제목과 동일하게 정렬하는 방법

이름 ---------------- - 번호 ----------------- 나이

얀 여기 1232312 25

는 SQLite 데이터베이스

<?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="wrap_content" 
    android:orientation="vertical" > 

    <TableLayout 
     android:id="@+id/table" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <TableRow> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_weight="1" 
       android:layout_height="fill_parent" 
       android:text="Name" > 
      </TextView> 

      <TextView 
      android:id="@+id/Hot" 
       android:layout_width="fill_parent" 
        android:layout_weight="1" 
       android:layout_height="fill_parent" 
       android:text="Number" > 
      </TextView> 
     </TableRow> 

      </TableLayout> 
    <TextView 
       android:id="@+id/tvSqlInfo" 
       android:layout_width="fill_parent" 
       android:layout_weight="1" 
       android:layout_height="fill_parent" 
       android:text="Name" > 
      </TextView> 

     /> 

</LinearLayout> 

이 볼 내 XML 코드 하나는 내 Activityclass는 XML을 볼 수 있습니다, 제발 가르쳐주세요 어디에 t를 넣어 ablelayout 매개 변수 감사합니다

`public class SQLView extends Activity { 
    TextView tv; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.sqlview); 
    TableLayout table = (TableLayout) findViewById(R.id.table); 
    TableRow row= new TableRow(this); 
    TextView text = new TextView(this); 
    row.addView(text); 
    table.addView(row); 


      TableLayout.LayoutParams l = 
       new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
    l.weight = 1; 

     tv= (TextView)findViewById(R.id.tvSqlInfo); 
      HotorNot info = new HotorNot(this); 
      info.open(); 
      String data = info.getData(); 
      info.close(); 
      tv.setText(data); 



here is my database class 

    public class HotorNot { 

     public static final String KEY_ROWID= "_id"; 
     public static final String KEY_NAME= "Person_Names"; 
     public static final String KEY_HOT= "personHOT"; 

     private static final String DATABASE_name= "HotorNotdb"; 
     private static final String DATABASE_TABLE= "PeopleTable"; 
     private static final int DATABASE_VERSION= 1; 

     private DbHelper ourHelper; 
     private final Context ourContext; 
     private SQLiteDatabase ourDatabase; 

     public static class DbHelper extends SQLiteOpenHelper{ 

      public DbHelper(Context context) { 
       super(context, DATABASE_name, null, DATABASE_VERSION); 
       // TODO Auto-generated constructor stub 
      } 

      @Override 
      public void onCreate(SQLiteDatabase db) { 
       // TODO Auto-generated method stub 

       db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
         KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +  
         KEY_NAME + " TEXT NOT NULL, " + 
         KEY_HOT + " TEXT NOT NULL);" 


       ); 
      } 

      @Override 
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
       // TODO Auto-generated method stub 
       db.execSQL("DROP TABLES IF EXIST" + DATABASE_TABLE); 
       onCreate(db); 
      } 


     } 
     public HotorNot(Context c){ 
      ourContext = c; 

     } 
     public HotorNot open() throws SQLException { 
      ourHelper = new DbHelper(ourContext); 
      ourDatabase = ourHelper.getWritableDatabase(); 
      return this; 
     } 

     public void close(){ 
      ourHelper.close(); 
     } 
     public long createEntry(String name, String hotness) { 
      // TODO Auto-generated method stub 
      ContentValues cv = new ContentValues(); 
      cv.put(KEY_NAME, name); 
      cv.put(KEY_HOT, hotness); 
     return ourDatabase.insert(DATABASE_TABLE, null, cv); 


     } 
     public String getData() { 
      // TODO Auto-generated method stub 
      String[] columns = new String []{ KEY_ROWID, KEY_NAME, KEY_HOT}; 
      Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
      String results = ""; 

      int iRow = c.getColumnIndex(KEY_ROWID); 
      int iName = c.getColumnIndex(KEY_NAME); 
      int iHotness = c.getColumnIndex(KEY_HOT); 

      for (c.moveToFirst(); !c.isAfterLast();c.moveToNext()){ 
       results = results + c.getString(iRow) + " " + c.getString(iHotness) + " " + c.getString(iName)+"\n"; 

      } 

      return results; 


     } 
     public String getName(long l) { 
      // TODO Auto-generated method stub 
      String[] columns= new String []{KEY_ROWID, KEY_NAME,KEY_HOT}; 
      Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "_" + l, null, null, null, null); 
      if (c != null){ 

       c.moveToFirst(); 
       String name = c.getString(1); 
       return name; 
      } 

      return null; 
     } 
     public String getHotness(long l) { 
      // TODO Auto-generated method stub 
      String[] columns= new String []{KEY_ROWID, KEY_NAME,KEY_HOT}; 
      Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null); 
      if (c != null){ 

       c.moveToFirst(); 
       String hotness = c.getString(2); 
       return hotness; 
      } 
      return null; 
     } 
    } 

답변

0

나는 동일한 문제가 있었고 나는봤을 때 간단한 대답을 찾을 수 없었다.

여기 내 코드는 어떻게 했는가? 내가 작업하고있는 게임에서 나온 것인데, 단지 두 개의 열만 더 쉽게 추가 할 수 있습니다. 행은 최대 점수로 정렬됩니다.

당신이 최고 점수 보는 활동입니다 :

public class Highscore extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.highscore); 
     TextView tvname = (TextView) findViewById(R.id.tvName); 
     TextView tvscore = (TextView) findViewById(R.id.tvScore); 
     SQLScore info = new SQLScore(this); 
     info.open(); 
     String name = info.getName(); 
     String score = info.getScore(); 
     info.close(); 
     tvname.setText(name); 
     tvscore.setText(score); 
     } 
} 

을 그리고 이것은 그것을위한 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="wrap_content" 
    android:orientation="vertical" > 

    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:stretchColumns="*" > 

     <TableRow> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="Name" /> 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="Score" /> 
     </TableRow> 

     <TableRow> 

      <TextView 
       android:id="@+id/tvName" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:text="Name" /> 

      <TextView 
       android:id="@+id/tvScore" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:text="Score" /> 
     </TableRow> 
    </TableLayout> 

</LinearLayout> 

그리고 마지막으로, 내 SQL 처리기 파일의 방법 :

public String getName() { 
    // TODO Auto-generated method stub 
    String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_SCORE}; 
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,  null, KEY_SCORE + " DESC", "10"); 
    String result = ""; 

    int iName = c.getColumnIndex(KEY_NAME); 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
     result = result + c.getString(iName) + "\n"; 
    } 

    return result; 
} 

public String getScore() { 
    // TODO Auto-generated method stub 
    String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_SCORE}; 
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, KEY_SCORE + " DESC", "10"); 
    String result = ""; 

    int iScore = c.getColumnIndex(KEY_SCORE); 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
     result = result + c.getString(iScore) + "\n"; 
    } 

    return result; 
} 

기본적으로 행을 가져 오는 중이지만 모든 ro 내의 각 열에서 데이터를 반환하는 한 가지 방법이 필요합니다. w. 나는 안드로이드 프로그래밍에 익숙하지 않아서 내 솔루션이 완벽하지 않을 수도 있지만 적어도 나를 위해 일했습니다.

관련 문제