如何在android中的sqlite数据库中显示listview中的图像

时间:2015-10-16 11:33:52

标签: java android sqlite

如何在sqlite db的列表视图中显示图像

 SimpleCursorAdapter adapter;
    adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.activity_column, c
            ,new String[] {"MemberID","Name","Tel","Photo"}
            ,new int[] {R.id.textView1, R.id.textView2, R.id.textView3, R.id.imageview}); 
  

此代码无图像工作,但我从照片调用图像我得到错误。如何在列表视图中显示图像。

2 个答案:

答案 0 :(得分:0)

您需要拥有自己的自定义游标适配器。

你这是一个提示:

public class CustomListViewCursorAdapter extends SimpleCursorAdapter {

    private static final String LOG_TAG = CustomListViewCursorAdapter.class.getSimpleName();


    String[] from;

    int[] to;

    Context context;

    public CustomListViewCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags){

        super(context, layout, c, from, to, flags);

        this.from = from;
        this.to = to;

    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        try {

            for( int i = 0; i < to.length; ++i){

                int columnIndex = cursor.getColumnIndex(from[i]);

                String columnValue = cursor.getString(columnIndex);

                TextView textView = (TextView) view.findViewById(to[i]);

                textView.setText(columnValue);

            }


            //get your image view and set its content here.
            view.findViewById(R.id.photo).setImageBitmap(yourImageBitmap);

        } catch (IllegalArgumentException e) {

            Log.e(LOG_TAG, e.getMessage(), e);

        }

    }



}

答案 1 :(得分:0)

我担心你必须编写自己的布局并使用自定义适配器:

http://developer.android.com/guide/topics/ui/declaring-layout.html

这是一个可以帮助您的教程:http://www.vogella.com/tutorials/AndroidListView/article.html