带有ImageView和TextViews的Android SimpleCursorAdapter

时间:2013-10-29 06:22:47

标签: android sqlite simplecursoradapter

我正在做一个Android应用程序,我必须在我的数据库的listview记录中显示。但是,照片我称之为drawables并将其分配给数组。如何使用SimpleCursorAdapter在ImageView上显示照片?

这是我到目前为止所尝试的内容:

    static final int[] imgs = {
        R.drawable.dinaretreat, // 0
        R.drawable.cobterrace, // 1
        R.drawable.ventassostreet, // 2
        R.drawable.summerhillblvddrouin, // 3
        R.drawable.todmanstreetdrouin, // 4
        R.drawable.aqueductroad // 5
};

    private DBHelper dbHelper;
SimpleCursorAdapter dataAdapter;
Cursor cursor;

Button back, filter;
TextView highest, lowest, location;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewhouseandland);

    initControls();

    displayRecords();
}

private void displayRecords() {
    // TODO displayRecords
    // TODO CheckDBConnection
    checkDatabaseConnection();

    // TODO 
    cursor = dbHelper.getAllHouses();


    // The desired columns to be bound
    String[] columns = new String[] {
      DBHelper.KEY_HOUSE,
      DBHelper.KEY_PRICE
    };

    // the XML defined views which the data will be bound to
    int[] to = new int[] {
      R.id.image,
      R.id.text1,
      R.id.text2
    };

    // create the adapter using the cursor pointing to the desired data
    //as well as the layout information
    dataAdapter = new SimpleCursorAdapter(
      this, R.layout.listrow,
      cursor,
      columns,
      to,
      0);

    lv.setAdapter(dataAdapter);

}

我很难在listview上显示图像。有任何想法吗?我很乐意感谢你的帮助。感谢。

3 个答案:

答案 0 :(得分:1)

您需要的是一个从BaseAdapter延伸的类(> CustomAdapter)。在那里,您可以为每个listItem定义自己的布局。

因此,为您的行项创建自定义布局(我知道它应该只是ImageView)。应使用getView()的{​​{1}}方法自定义此布局。但请阅读this article以确保listView的“平滑滚动”。

您的CustomAdapter应如下所示:

BaseAdapter

答案 1 :(得分:0)

首先:参数“from”和“to”应该具有相同的大小,第二个:使用SimpleCursorAdapter。 ViewBinder

答案 2 :(得分:0)

添加DBHelper.KEY_IMAGE_TYPE等列,将“from”的大小固定为“to”,使用ViewBinder的示例代码如下:

    // The desired columns to be bound
    String[] columns = new String[] { DBHelper.KEY_IMAGE_TYPE, DBHelper.KEY_HOUSE,
            DBHelper.KEY_PRICE };

    // the XML defined views which the data will be bound to
    int[] to = new int[] {  
            R.id.image,
            R.id.text1,
            R.id.text2};

    SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(this,
            R.layout.listrow, cursor, columns, to, 0);
    dataAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor,
                int columnIndex) {
            switch (view.getId()) {
            case R.id.image:
                int imageType = cursor.getInt(columnIndex);
                int imageToBeShown=0;
                switch (imageType) {
                case TYPE_ONE:
                    imageToBeShown=imgs[0];
                    break;
                case TYPE_TWO:
                    imageToBeShown=imgs[1];
                    break;
                case TYPE_THREE:
                    imageToBeShown=imgs[2];
                    break;
                case TYPE_FOUR:
                    imageToBeShown=imgs[3];
                    break;
                case TYPE_FIVE:
                    imageToBeShown=imgs[4];
                    break;
                case TYPE_SIX:
                    imageToBeShown=imgs[5];
                    break;
                }
                ((ImageView)view).setImageResource(imageToBeShown);
                return true;
            }
            return false;
        }
    });