1. Download our Official Android App: Forums for Android!

How to populate a grid view from sqlite

Discussion in 'Introductions' started by KittyA, Aug 23, 2010.

  1. KittyA

    KittyA Lurker
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    3
    Joined:
    Aug 23, 2010

    Aug 23, 2010
    3
    0
    25
    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!! :thinking:

    Please help me with this!! I'm completely new to android and i'm really stuck with this!!
     

    Advertisement

  2. Steven58

    Steven58 Reformed PH
    Rank:
     #4
    Points:
    3,933
    Posts:
    32,959
    Joined:
    Feb 19, 2010

    Feb 19, 2010
    32,959
    25,351
    3,933
    Male
    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 Android Expert
    Rank:
    None
    Points:
    143
    Posts:
    2,188
    Joined:
    Apr 14, 2010

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

    Leftyguy Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    2,668
    Joined:
    Jul 25, 2010

    Jul 25, 2010
    2,668
    102
    163
    Oregon Coast
    Hi - Welcome to the Android Forums - Cheers ;)
     
  5. KittyA

    KittyA Lurker
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    3
    Joined:
    Aug 23, 2010

    Aug 23, 2010
    3
    0
    25
    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...