1

I am not sure whether the question title is proper or not, but after searching a lot I am asking this.

In my SQLite table, I have the columns

1: _id   2: position   3: path

position: the position of the gridView where the Image is to set
path:  the path of the SDCard having corresponding image

How would I get the image from the path and set into the GridView

GridView grid = (GridView) findViewById(R.id.play_grid_view);
DBAdapter adapter = new DBAdapter(this); //My costum adapter for databse operation
adapter.open();
Cursor cusor = adapter.getAllImages(); //returns cursor with 3 columns mentioned above
startManagingCursor(cusor);

After this what should I do?

Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • could you further explain, how would you use `2: position` ? Is it position of your image in your grid? – hendrix Mar 06 '12 at 12:12
  • suppose grid is of 4*4 and position returns 5, then that image should be displayed in 2nd row and 2nd column of the grid. – Chandra Sekhar Mar 06 '12 at 12:27

2 Answers2

1

You are looking for the SimpleCursorAdapter. You need to customize the bindView() method by overriding to show what you want.

You could also try the CursorAdapter itself. Here is a tutorial..

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
1

If your position column means the position of image in grid, you can just sort your query with this column, then cursorAdapter will fill your grid according to positions set in your DB.

This is not usable, if you will skip some of gridview cells (suppose you have following positions in your database: 1,2,4 - then your this adapter will fill positions 1,2,3 as there is actually no position checking)

public class ImageCursorAdapter extends CursorAdapter {
    public ImageCursorAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        String pos = cursor.getString(positionColumnIndex);
        String path = cursor.getString(pathColumnIndex);
        ImageView image = (ImageView)view;
        image.setImageDrawable(Drawable.createFromPath(path));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = new ImageView(context);
        bindView(v, context, cursor);
        return v;
    }
}
zyamys
  • 1,609
  • 1
  • 21
  • 23
hendrix
  • 3,364
  • 8
  • 31
  • 46
  • Can you say what is the use of ayoutInflater and when should we use it. I have seen a number of examples using this, but am not able to understand why they use this. – Chandra Sekhar Mar 06 '12 at 12:32
  • layout inflater is used to create instance of View from xml file. – hendrix Mar 06 '12 at 12:34
  • But I don't have any XML file with ImageViews. In my XML file I just declared a GriView and want to set the Bitmaps on it. Thats why I am confused what to write instead of ImageView image = (ImageView)view.findViewById(R.id.yourImageView); in the bindView() – Chandra Sekhar Mar 06 '12 at 12:39
  • comments may only be edited for 5 minutes, but this i added: layout inflater is used to create instance of View from xml layout file. This View instance can then be used in your program to programatically manage views at runtime. From example above, after doing `View v = inflater.inflate(R.layout.yourGridViewItem, parent, false);` and sending `v` as parameter to `bindView(v, context, cursor)`, we could get instance of ImageView defined in xml layout file, see `bindView` method: `ImageView image = (ImageView)view.findViewById(R.id.yourImageView);` – hendrix Mar 06 '12 at 12:41
  • The thing is, people often want to have custom view (structured from many views like imageview) as their item views in gridView. If your item view in gridView will only be ImageView, then i suppose you dont need additional XML file for it, and also dont need to inflate. – hendrix Mar 06 '12 at 12:43
  • i edited my answer not to use custom item layout and got rid of inflating. – hendrix Mar 06 '12 at 12:46
  • How could I achieve that, actually my requirement is completely related to that issue. I want to place the images as per the position mentioned in the database. – Chandra Sekhar Mar 06 '12 at 13:00