如何更改ListView中设置为SimpleCursorAdapter的ImageView资源

问题描述:

我试图为我的应用程序创建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

我并不完全按照你的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

你必须创建一个自定义适配器做这样的处理。一个简单的光标不会为你做。

样品: 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)