SimpleCursorAdapter with ImageView and TextView

前端 未结 2 1967
情书的邮戳
情书的邮戳 2020-12-30 09:40

can you have a layout with an imageview and textview for a row in a SimpleCursorAdapter with a listview?

this would be the lay

相关标签:
2条回答
  • 2020-12-30 10:25

    To expand on the answer from @Francesco Vadicamo, this is a fuctions that is part of a larger activity. I split it off because I needed to call it from multiple areas of the code. databaseHandler and listView are defined as a class variables and initialized in onCreat().

    private void updateListView() {
        // Get a Cursor with the current contents of the database.
        final Cursor cursor = databaseHandler.getCursor();
    
        // The last argument is 0 because no special behavior is required.
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.listview,
                cursor,
                new String[] { databaseHandler.ICON, databaseHandler.BOWLER_TXT },
                new int[] { R.id.icon, R.id.bowler_txt },
                0);
    
        // Override the handling of R.id.icon to load an image instead of a string.
        adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                if (view.getId() == R.id.imageview) {
                    // Get the byte array from the database.
                    byte[] iconByteArray = cursor.getBlob(columnIndex);
    
                    // Convert the byte array to a Bitmap beginning at the first byte and ending at the last.
                    Bitmap iconBitmap = BitmapFactory.decodeByteArray(iconByteArray, 0, iconByteArray.length);
    
                    // Set the bitmap.
                    ImageView iconImageView = (ImageView) view;
                    iconImageView.setImageBitmap(iconBitmap);
                    return true;
                } else {  // Process the rest of the adapter with default settings.
                    return false;
                }
            }
        });
    
        // Update the ListView.
        listView.setAdapter(adapter);
    }
    
    0 讨论(0)
  • 2020-12-30 10:28

    When the view to bind is an ImageView and there is no existing ViewBinder associated SimpleCursorAdapter.bindView() calls setViewImage(ImageView, String). By default, the value will be treated as an image resource. If the value cannot be used as an image resource, the value is used as an image Uri.

    If you need to filter in other ways the value retrieved from the database you need a ViewBinder to add to the ListAdapter as follow:

    listAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
       /** Binds the Cursor column defined by the specified index to the specified view */
       public boolean setViewValue(View view, Cursor cursor, int columnIndex){
           if(view.getId() == R.id.your_image_view_id){
               //...
               ((ImageView)view).setImageDrawable(...);
               return true; //true because the data was bound to the view
           }
           return false;
       }
    });
    
    0 讨论(0)
提交回复
热议问题