2014-03-30 1 views
2

나는 단추를 몇 번이나 counter는지 계산하는 카운터가 있습니다. 다음왜 텍스트 뷰에서 값을 삽입 할 때 내 문자열을 원하는대로 정렬 할 수 없습니까?

public void onClick(View v) { 

      counter++; 
      text.setText("Score"+counter); 



     } 

이게 내 삽입 함수 인이

 text = (TextView) findViewById(R.id.textView1); 
       final String Score = text.getText().toString(); 
       mySQLiteAdapter.openToWrite(); 

       mySQLiteAdapter.insert("sccore="Score); 

       mySQLiteAdapter.close(); 

값 내 DB에 삽입

public long insert(String content){ 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(KEY_CONTENT, content); 
    return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues); 
} 

그래서 불렀어 삽입 된 카운터 값> 점수 = 2, 점수 = 1 , 점수 = 111, 점수 = 3, 점수 = 21 < - (해당 카운터 값)

다음은보고 싶습니다. 그래서 다른 texview에서 자신의 점수 :

mySQLiteAdapter = new SQLiteAdapter(this); 
    /* 
    * Open the same SQLite database 
    * and read all it's content. 
    */ 
    mySQLiteAdapter = new SQLiteAdapter(this); 
    mySQLiteAdapter.openToRead(); 
    String contentRead = mySQLiteAdapter.queueAll(); 
    listContent.setText(contentRead); 
    mySQLiteAdapter.close(); 

내 fucntion의 queueAll는()과 같이 보인다 :

public String queueAll(){ 
    String[] columns = new String[]{KEY_CONTENT}; 
    Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, 
      null, null, null, null, KEY_CONTENT+" desc"); 
    String result = ""; 

    int index_CONTENT = cursor.getColumnIndex(KEY_CONTENT); 
    for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){ 
     result = result + cursor.getString(index_CONTENT) + "\n"; 
    } 

    return result; 
} 

확인 및 텍스트보기 나는이 보았다

Score = 3 , Score=21 , Score=2 , Score=111, Score=1 

하지만 난

을 원하는을
score=111, score=21, score=3, score=2, score=1. 

왜 주문 DESC하지 않았나요 DESC에서 원하는가? 왜 이렇게 생각하니? 왜 삽입 된 카운터 값이 정렬되지 않았습니까?

public static final String MYDATABASE_NAME = "MY_DATABASE"; 
public static final String MYDATABASE_TABLE = "MY_TABLE"; 
public static final int MYDATABASE_VERSION = 1; 
public static final String KEY_CONTENT = "Content"; 

//create table MY_DATABASE (ID integer primary key, Content text not null); 
private static final String SCRIPT_CREATE_DATABASE = 
    "create table " + MYDATABASE_TABLE + " (" 
    + KEY_CONTENT + " text not null);"; 

솔직히 말해서 내 열 KEY_CONTENT는 STRING이 아닌 NUMBER 여야합니다. 이것을 변경하는 방법? str을 num로 변경하면 DESC가 원하는대로 작업을 수행합니까?

+0

감사를 잘 준비된 질문에 대해. – Szymon

답변

0

숫자를 text으로 저장 했으므로 알파벳 순으로 정렬됩니다. 당신은 쿼리 할 때

당신은 integer에 숫자를 변환 할 수 있습니다

Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, 
     null, null, null, null, "CAST(" + KEY_CONTENT + " as integer) desc"); 
+0

u는 위자드입니다. D. 이제 작동합니다. 어떻게 작동하는지 알 수 있습니까? ""CAST ("+ KEY_CONTENT +"정수) desc ")"무엇이 캐스트입니까? – Jakozo

+0

@ user3202351''Content' ('text'로 저장된) 컬럼을'integer' 데이터 타입으로 변환하고 있습니다. 모든 레코드는 유효한 정수를 가지므로 문제없이 캐스트 한 다음 숫자 값을 기반으로 정렬 할 수 있습니다. – Szymon

+0

하지만 삽입 된 값이 "qweq1qweqweScore = 15"이면 어떻게 될까요? 그것은 무엇을 던질 것인가? – Jakozo

관련 문제