jeudi 21 janvier 2016

Populate custon GridView from SQLite

I have seen so many different methods of binding data to a list/gridview in these last few hours and am still no closer to getting mine to work.

I'll post code I think is relevant but can add anything I may have missed which will help answer my question. I'm trying to bind my SQLite data to a custom gridview.

SQLiteHelper.java

public List<Site> getAllSites(){
    List<Site> sites = new ArrayList<Site>();
    String selectQuery = "SELECT * FROM " + TABLE_SITES;

    Log.e(LOG, selectQuery);

    SQLiteDatabase database = this.getReadableDatabase();
    Cursor c = database.rawQuery(selectQuery, null);

    if (c.moveToFirst()){
        do {
            Site st = new Site();
            st.setSiteID(c.getInt(c.getColumnIndex(KEY_SITE_ID)));
            st.setSiteName(c.getString(c.getColumnIndex(KEY_SITE_NAME)));
            st.setSiteLat(c.getDouble(c.getColumnIndex(KEY_SITE_LAT)));
            st.setSiteLon(c.getDouble(c.getColumnIndex(KEY_SITE_LON)));
            st.setSiteCreatedDateTime(c.getString(c.getColumnIndex(KEY_CREATED_AT)));

            sites.add(st);
        } while (c.moveToNext());
    }
    return sites;
}

Site.java

public class Site {

    int _id;
    String _sitename;
    double _lat;
    double _lon;
    String _createddatetime;


    // Constructors
    public Site() {

    }

    public Site(int id, String sitename, double lat, double lon, String createddatetime) {
        this._id = id;
        this._sitename = sitename;
        this._lat = lat;
        this._lon = lon;
        this._createddatetime = createddatetime;
    }

    public Site(String sitename, double lat, double lon, String createddatetime) {
        this._sitename = sitename;
        this._lat = lat;
        this._lon = lon;
        this._createddatetime = createddatetime;
    }

    // Getters and Setters
    public void setSiteID(int id){
        this._id = id;
    }

    public int getSiteID(){
        return this._id;
    }

    public void setSiteName(String sitename){
        this._sitename = sitename;
    }

    public String getSiteName(){
        return this._sitename;
    }

    public void setSiteLat(double lat){
        this._lat = lat;
    }

    public void setSiteLon(double lon){
        this._lon = lon;
    }

    public double getSiteLat(){
        return this._lat;
    }

    public double getSiteLon(){
        return this._lon;
    }

    public String getSiteCreatedDateTime(){
        return this._createddatetime;
    }

    public void setSiteCreatedDateTime(String createddatetime){this._createddatetime = createddatetime;}
}

map_tile.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://ift.tt/nIICcg"
    xmlns:app="http://ift.tt/GEGVYd"
    xmlns:map="http://ift.tt/GEGVYd"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://ift.tt/nIICcg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp"
        android:orientation="horizontal">

        <android.support.v7.widget.CardView
            android:id="@+id/cardviewMapTile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:elevation="@dimen/cardview_default_elevation"
            app:cardCornerRadius="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/map_tile_title"
                android:orientation="vertical">
                    <TextView
                        android:id="@+id/textMapTileTitle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="10dp"
                        android:lines="1"
                        android:ellipsize="end"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textColor="@color/common_google_signin_btn_text_dark_pressed"
                        android:text="Map Tile Name"/>
                <TextView
                    android:id="@+id/textMapTileDate"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingRight="10dp"
                    android:paddingLeft="10dp"
                    android:paddingBottom="10dp"
                    android:textColor="@color/common_google_signin_btn_text_dark_pressed"
                    android:text="6th March 1986"/>
            </LinearLayout>

            <com.google.android.gms.maps.MapView
                android:id="@+id/mapviewMapTiles"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                map:liteMode="true"
                map:mapType="normal" />

        </android.support.v7.widget.CardView>

    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

MainActivity.java (OnCreate)

I was going to post what I have tried here but there are so many I don't know where to start

Aucun commentaire:

Enregistrer un commentaire