0

onCreate()方法:

public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.database);

       ListView lv=(ListView)findViewById(R.id.mylist);    
       dbh = new DatabaseHelper(this);
       c = dbh.getReadableDatabase().rawQuery("SELECT _id, " + 
                        DatabaseHelper.NAME + 
                ", " + DatabaseHelper.LASTNAME + 
                ", " + DatabaseHelper.ans2 + 
                " FROM " +
                DatabaseHelper.TABLE_NAME, null); // initializing 


       String[] dataFrom ={DatabaseHelper.NAME, DatabaseHelper.LASTNAME, DatabaseHelper.ans2};
       int[] dataTo = {R.id.name, R.id.value1, R.id.value2};

       SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
                    R.layout.row, c, dataFrom, dataTo);
       lv.setAdapter(adapter);
       registerForContextMenu(lv);


    }

我的row.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="match_parent"
android:orientation="horizontal" >


<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:layout_weight="30"
    android:id="@+id/name"
    />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
   android:layout_weight="40"
    android:gravity="center_horizontal"
    android:id="@+id/value1"
    />
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="30"
    android:gravity="right"
    android:id="@+id/value2"
    />


<ImageView
  android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:gravity="right"
   android:src="@drawable/ic_launcher"
   android:id="@+id/icon"
   />


</LinearLayout>

我已经成功地用 SQLite 创建了一个表。我想在每个数据库行中显示一个图标,该图标将被显示(使用ListView),并且该图标应该取决于该行中的某个元素。例如,如果性别 ( DatabaseHelper.SEX) 是男性,那么我们将显示一个男性图标,如果性别是女性,我们将显示一个女性图标。如果您查看我的 xml 文件,我已经显示了一个图标(这是默认的 android 图标,但这不是我想要的)。有任何想法吗?

4

1 回答 1

1

您可以创建自己的适配器(扩展SimpleCursorAdapter)并仅覆盖该bindView()方法并根据需要设置图标:

class CustomAdapter extends SimpleCursorAdapter {

    public CustomAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // let the adapter fill the other items in the row
        super.bindView(view, context, cursor); 
       // find the icon in the list row
        ImageView icon = (ImageView) view.findViewById(R.id.icon); 
       // I don't know how you set up the DatabaseHelper.SEX column in your 
       //database, I assumed you have an int value, 0 for male and 1 for female
        int sex = cursor.getInt(cursor.getColumnIndex("sex"));
        if (sex == 0) {
                            //if male
            icon.setImageResource(R.drawable.male_icon);
        } else {
                            //if female
            icon.setImageResource(R.drawable.female_icon);
        }
    }

}

为您使用此适配器ListView,当然不要忘记将DatabaseHelper.SEX列添加到查询中:

c = dbh.getReadableDatabase().rawQuery("SELECT _id, " + 
                        DatabaseHelper.NAME + 
                ", " + DatabaseHelper.LASTNAME + 
                ", " + DatabaseHelper.ans2 + ", " + DatabaseHelper.SEX +
                " FROM " +
                DatabaseHelper.TABLE_NAME, null); // initializing 

编辑:

或者您可以使用ViewBinder

c = dbh.getReadableDatabase().rawQuery("SELECT _id, " + 
                        DatabaseHelper.NAME + 
                ", " + DatabaseHelper.LASTNAME + 
                ", " + DatabaseHelper.ans2 + ", " + DatabaseHelper.SEX + 
                " FROM " +
                DatabaseHelper.TABLE_NAME, null); // initializing 


       String[] dataFrom ={DatabaseHelper.NAME, DatabaseHelper.LASTNAME, DatabaseHelper.ans2, DatabaseHelper.SEX};
       int[] dataTo = {R.id.name, R.id.value1, R.id.value2, R.id.icon};

       SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
                    R.layout.row, c, dataFrom, dataTo);
       adapter.setViewBinder(new ViewBinder(){

            @Override
            public boolean setViewValue(View view, Cursor cursor,
                    int columnIndex) {
                if (view.getId() == R.id.icon) {
                    int sex = cursor.getInt(columnIndex);
                    if (sex == 0) {
                        ((ImageView) view).setImageResource(R.drawable.male_icon);
                    } else if (sex == 1){
                        ((ImageView) view).setImageResource(R.drawable.female_icon);
                    }

                    return true;    
                } else {
                    return false;
                }

            }

        });
   lv.setAdapter(adapter);
于 2012-04-12T16:37:01.623 回答