How to populate a grid view from sqlite


Last Updated:

  1. KittyA

    KittyA New Member This Topic's Starter

    Joined:
    Aug 23, 2010
    Messages:
    3
    Likes Received:
    0
    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 (Text):
    1.  
    2. public class GridImages extends Activity {
    3. @Override
    4.     public void onCreate(Bundle savedInstanceState) {
    5.         super.onCreate(savedInstanceState);
    6.         setContentView(R.layout.main);
    7.  
    8.         populateGridView();
    9.     }
    10.  
    11. public void populateGridView()
    12.     {
    13.         DBAdapter db = new DBAdapter(this);
    14.         GridView gv = (GridView)this.findViewById(R.id.gdImages);
    15.        
    16.         Cursor c = db.getAllDishes();
    17.         startManagingCursor(c);
    18.        
    19.         String[] cols = new String[]{"IMAGEPATH"};
    20.         int[] names = new int[]{R.id.imgItem};
    21.        
    22.         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.imggriditem ,c,cols,names);
    23.         gv.setAdapter(adapter);
    24. }
    25. }

    And the DBAdpater class follows:

    Code (Text):
    1. public class DBAdapter
    2. {
    3.     private final Context context;    
    4.     private SQLiteDatabase db;
    5.  
    6.     public DBAdapter(Context ctx)
    7.     {
    8.         this.context = ctx;
    9.         db = SQLiteDatabase.openDatabase("/sdcard/gridimages.sqlite", null, SQLiteDatabase.OPEN_READONLY);
    10.  
    11.     }
    12. }

    And the imggriditem.xml file for the layout is:

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <ImageView
    3.   xmlns:android="http://schemas.android.com/apk/res/android"
    4.   android:id="@+id/imgItem"
    5.   android:layout_width="wrap_content"
    6.   android:layout_height="wrap_content">
    7. </ImageView>

    The line
    Code (Text):
    1. 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!!
     

    Advertisement
  2. Steven58

    Steven58 Reformed PH VIP Member

    Joined:
    Feb 19, 2010
    Messages:
    32,921
    Likes Received:
    25,271
    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
     
  3. yojoe600

    yojoe600 Well-Known Member

    Joined:
    Apr 14, 2010
    Messages:
    2,188
    Likes Received:
    207
    as steven said before... go to that section for help but yea ill give you a welcome since your here :p
     
  4. Leftyguy

    Leftyguy Well-Known Member

    Joined:
    Jul 25, 2010
    Messages:
    2,668
    Likes Received:
    102
    Hi - Welcome to the Android Forums - Cheers ;)
     
  5. KittyA

    KittyA New Member This Topic's Starter

    Joined:
    Aug 23, 2010
    Messages:
    3
    Likes Received:
    0
    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??
     

Share This Page

Loading...