0

我正在做一个 Android 应用程序,我必须在我的数据库中的列表视图记录中显示。但是,我调用了可绘制对象并将其分配给数组的照片。如何使用 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);

}

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

4

3 回答 3

1

您需要的是一个从BaseAdapter. 在那里,您可以为每个 listItem 定义自己的布局。

因此,为您的行项目创建一个自定义布局(就像我理解它应该只是一个ImageView)。此布局应在getView()您的BaseAdapter. 但请阅读本文以确保您的 listView 的“平滑滚动”。

您的 CustomAdapter 应如下所示:

public class MyCustomAdapter extends BaseAdapter{

    // MEMBER
    private int[] mImgs;

    public MyCustomAdapter(Activity context, int[] imgs){
        mImgs = imgs;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mImgs.length;
    }

    @Override
    public Object getItem(int position) {
        return mImgs[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // --- detail settings of single views
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // check if your layout is null
        if(convertView == null){
            convertView = createNewView();              
        }

        // get the ViewHolder of your view
        ViewHolder holder = (ViewHolder) convertView.getTag();

        // --- set data to listItem
        holder.imageView.setImageDrawable(mImgs[position])

        return convertView;
    }


    // new View
    private View createNewView() {
        View convertView = null;
        int layout = 0;

        ViewHolder holder = new ViewHolder();

        layout = R.layout.customlayout;
        convertView = mInflater.inflate(layout, null);
        holder.imageView = (ImageView) convertView.findViewById(R.id.customlayout_imageview);

        convertView.setTag(holder);
        return convertView;
    }

    //ViewHolder
    public static class ViewHolder{
        public ImageView imageView;
    }

}
于 2013-10-29T06:47:28.900 回答
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;
        }
    });
于 2013-10-29T07:09:16.173 回答
0

首先:参数“from”和“to”应该具有相同的大小,第二:使用 SimpleCursorAdapter。视图绑定器

于 2013-10-29T06:44:17.087 回答