2012-01-11 35 views
1

我試圖爲我的應用程序創建HighscoreList。 此列表基於SQL數據庫。保存名稱和分數工作得很好。 ListView控件是基於該行的XML文件:如何更改ListView中設置爲SimpleCursorAdapter的ImageView資源

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:paddingTop="4dip" 
    android:paddingBottom="6dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView android:id="@+id/NAME_CELL" 
     android:layout_width="250dip" 
     android:layout_height="wrap_content" 
     android:textSize="20dip" /> 

    <TextView android:id="@+id/SCORE_CELL" 
     android:layout_width="20dip" 
     android:layout_height="wrap_content" 
     android:textSize="20dip" /> 

    <ImageView android:id="@+id/PICTURE_CELL" 
     android:layout_width="50dip" 
     android:layout_height="20dip" 
     android:src="@drawable/no_picture" /> 

而且我想,對於例如,如果玩家達到分數的10 PICTURE_CELL ImageView的應顯示圖像。 現在我有一個光標它是通過數據庫會和找榜:

public void drawPictures() 
    {  
     if (dbCursor.moveToFirst()) {  
      do 
      { 
           if(dbCursor.getInt(2)<=4) 
       { 
          Log.d(TAG, "no picture"); 



       } 
       else if((dbCursor.getInt(2) > 4) && (dbCursor.getInt(2) <= 9)) 
       { 
        Log.d(TAG, "picture1"); 


       } 

       else if((dbCursor.getInt(2) > 9) && (dbCursor.getInt(2) <= 19)) 
       { 
        Log.d(TAG, "picture2"); 
       } 

       else if(dbCursor.getInt(2) > 20) 
       { 
        Log.d(TAG, "picture3"); 
       } 
      } 
      while (dbCursor.moveToNext()); 

但我不知道我怎樣才能改變各行中的每個的ImageView的資源。 請幫助我,這是殺了我好幾天!

謝謝!

+0

你應該避免像 「dbCursor.getInt(2)」 這個消息不是很好的做法。而不是兩個,你應該有一個字符串常量,我從數據庫幫助程序公開的標識行的字符串常量。 – JoxTraex 2012-01-11 16:23:33

回答

0

我並不完全按照你的drawPictures方法,但我建議你讓你的查詢包含一個ORDER關鍵字,這樣你的值就被排序而不是搜索。

然後在適配器getView方法使用getCursor().getInt(<score column>),並檢查分數大於10

if(score > 10) 

如果它是你可以做的:

ImageView imageView = (ImageView) view.findViewById(R.id.PICTURE_CELL) 

然後調用:

// where drawable is whatever image you would like to present 
imageView.setImageDrawable(Drawable drawable) 

UPDATE from評論

您的SimpleCursorAdapter是CursorAdapter的子集,它具有getView,您只需要擴展SimpleCursorAdapter並添加下面的方法。

您需要在駕馭這一方法,像這樣:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    int score = dbCursor.getInt(<score column>) 
    // You might need to call moveToPosition(position) or maybe you should use getItem(position) I have not tested it 
    if(score > 10) { 
     ImageView imageView = (ImageView) view.findViewById(R.id.PICTURE_CELL) 
     // where drawable is whatever image you would like to present 
     imageView.setImageDrawable(Drawable drawable) 
    } 
} 
+0

我沒有getView方法...... 我simpleCursorAdapter看起來是這樣的: – user1128895 2012-01-11 15:58:58

+0

我沒有getView方法...... 我simpleCursorAdapter看起來是這樣的: 的String [] =文字新的String [] {highscoreDBAdapter.KEY_PLAYERNAME,highscoreDBAdapter。 KEY_SCORE}; int [] where = new int [] {R.id.NAME_CELL,R.id.SCORE_CELL};新的SimpleCursorAdapter(this,R.layout.row,dbCursor,text,where); – user1128895 2012-01-11 16:01:48

+0

我不明白我必須把getView()方法和我的適配器放在一起。 – user1128895 2012-01-11 16:35:55

0

你必須創建一個自定義適配器做這樣的處理。一個簡單的光標不會爲你做。

樣品: http://android.vexedlogic.com/2011/04/02/android-lists-listactivity-and-listview-ii-%E2%80%93-custom-adapter-and-list-item-view/

雖然這個例子使用ArrayAdapter,它不管那麼多了到PROCESSS。這個過程是一樣的。您需要必須創建您自己的自定義適配器。一旦創建了自定義的遊標類,那麼將使用預處理邏輯覆蓋getView(...)方法。

評論

的說明在你的數據庫幫助:

public static final int COL_NAME = 2; 

在你的代碼,

dbCursor.getInt([Database Helper Object].COL_NAME) 
相關問題