• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How to populate a grid view from sqlite

KittyA

Lurker
Hello All,

I am new to android and i am having some difficulties in populating a Grid View with data from SQLite.

I am using Eclipse.

This is my code in the Main class:

Code:
public class GridImages extends Activity {
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        populateGridView();
    }

public void populateGridView()
    {
        DBAdapter db = new DBAdapter(this);
        GridView gv = (GridView)this.findViewById(R.id.gdImages);
        
        Cursor c = db.getAllDishes();
        startManagingCursor(c);
        
        String[] cols = new String[]{"IMAGEPATH"};
        int[] names = new int[]{R.id.imgItem};
        
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.imggriditem ,c,cols,names);
        gv.setAdapter(adapter);
}
}


And the DBAdpater class follows:

Code:
public class DBAdapter
{
    private final Context context;    
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        db = SQLiteDatabase.openDatabase("/sdcard/gridimages.sqlite", null, SQLiteDatabase.OPEN_READONLY);

    }
}


And the imggriditem.xml file for the layout is:

Code:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/imgItem" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
</ImageView>


The line
Code:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.imggriditem ,c,cols,names);
in class GridImages is not working!! :confused:

Please help me with this!! I'm completely new to android and i'm really stuck with this!!
 
this is the introduction section. you just say Hi here. If you want app help, please go to the developer's sub section. find it by going to the root url, there'll be a list there.

Welcome to Android Forums!! Glad you signed up!! :D
 
Thanks you all. Is there a way to move this thread to the appropriate section automatically? Or i will need to type this whole message from scratch??
 
Back
Top Bottom