android - Layout For SQLite Resuts -


i'm trying create layout looks below every row returns database (will last 7 days). appreciated.

xml created

    <quickcontactbadge             android:id="@+id/image"             android:layout_height="wrap_content"             android:layout_width="wrap_content"             android:scaletype="centercrop"/>          <textview android:id="@+id/date"                   android:layout_width="match_parent"                   android:layout_height="wrap_content"                   android:layout_torightof="@+id/image"                   android:gravity="center_vertical"                   android:layout_alignparentright="true"                   android:layout_alignparenttop="true"                   android:text="date"/>             <textview                android:id="@+id/temp"                android:layout_width="150dp"                android:layout_height="wrap_content"                android:layout_alignleft="@+id/date"                android:layout_below="@+id/date"                android:gravity="center_vertical"                android:text="temp" />             <textview                 android:id="@+id/fertile"                 android:layout_width="150dp"                 android:layout_height="wrap_content"                 android:layout_above="@+id/notes"                 android:layout_torightof="@+id/temp"                 android:gravity="center_vertical"                 android:text="fertile" />              <textview                 android:id="@+id/notes"                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:layout_alignbottom="@+id/image"                 android:layout_alignleft="@+id/temp"                 android:layout_below="@+id/temp"                 android:gravity="center_vertical"                 android:text="notes" />      </relativelayout> 

can show me how loop through sql layout desire?

            public class historyfragment extends fragment {          @override         public void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);          }                  @override          public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {                view view = inflater.inflate(r.layout.fragment_history, container, false);              //get data sql             sqlhelper entry = new sqlhelper(getactivity());             entry.open();             cursor results = entry.getalldata();             entry.close();                return view;          }      } 

sql

public class sqlhelper {     //database information     private static final string database_name = "nfp";     private static final int database_version = 1;      //table information     private static final string database_table = "charting";     public static final string key_rowid = "_id";     public static final string key_name = "persons_name";     public static final string key_charting_date = "date";     public static final string key_charting_temperature = "temperature";     public static final string key_charting_stamps = "stamps";     public static final string key_charting_fertile = "fertile";     public static final string key_charting_notes = "notes";     public static final string key_charting_proc = "proc";       private dbhelper ourhelper;     private final context ourcontext;     private static sqlitedatabase ourdatabase;      private static class dbhelper extends sqliteopenhelper{     public sqlhelper(context c) {         ourcontext = c;     }              public dbhelper(context context) {             super(context, database_name, null, database_version);         }          @override         public void oncreate(sqlitedatabase db) {             db.execsql( "create table " + database_table + " (" +                                         key_rowid + " integer  primary key autoincrement, " +                                         key_charting_date + " text primary key not null, " +                                         key_name + " text, " +                                         key_charting_temperature + " integer, " +                                         key_charting_stamps  + " integer, " +                                         key_charting_fertile + " text, " +                                         key_charting_notes + " text, " +                                         key_charting_proc +  " ); "                     );         }      public sqlhelper open() throws sqlexception{         ourhelper = new dbhelper(ourcontext);         ourdatabase = ourhelper.getwritabledatabase();         return this;     }      public void close(){         ourhelper.close();     }      public long createentry(string date, string temperature, string fertile, string notes) {         contentvalues cv = new contentvalues();         cv.put(key_charting_date, date);         cv.put(key_charting_temperature, temperature);         cv.put(key_charting_fertile, fertile);         cv.put(key_charting_notes, notes);         return ourdatabase.insertorthrow (database_table, null, cv);      }      public string getdata() {         string[] columns = new string[]{ key_charting_date, key_charting_temperature, key_charting_stamps, key_charting_fertile, key_charting_notes };         cursor c = ourdatabase.query(database_table, columns, null, null, null, null, null);          string result = "";          int idate = c.getcolumnindex(key_charting_date);         int itemp = c.getcolumnindex(key_charting_temperature);         int istamps = c.getcolumnindex(key_charting_stamps);         int ifertile = c.getcolumnindex(key_charting_fertile);         int inotes = c.getcolumnindex(key_charting_notes);          (c.movetofirst(); !c.isafterlast(); c.movetonext()) {             result = result + "" + c.getstring(idate) + "/n" + c.getstring(itemp) + "/n" + c.getstring(istamps) + "/n" + c.getstring(ifertile) + "/n" + c.getstring(inotes) + "|";         }          return result;     }     public cursor getalldata() {           string[] columns = new string[]{ key_charting_date, key_charting_temperature, key_charting_stamps, key_charting_fertile, key_charting_notes };         cursor cursor = ourdatabase.query(database_table, columns, null, null, null, null, null);           return cursor;       } } 

there standard solution task, consisting of following layers:

database layer

i know nothing data source using: local or remote, no advice here

content provider, presenting data database.

this not necessary, allows usage of built-in loaders , cursoradapters minimal modification.

your custom adapter extending, say, simplecursoradapter, uses custom layout of row shown on example picture. typically layout stored in separate xml layout file. adapter contains logic of binding data database cursor row newly created row view.

finally, listview in fragment. set listview's adapter adapter.

of course, quite long way , not one, 1 minimum sweat , blood, believe.

the details lengthy cover in single answer, sure study corresponding tutorials:

custom adapter , listview

content provider on top of sqlite database

you should: i. wrap content provider around database in tutorial link above. result: class mycontentprovider.

 static {     surimatcher = new urimatcher(urimatcher.no_match);     surimatcher.adduri(authority, database_table, match_all);      projectionmap = new hashmap<string, string>();     projectionmap.put(key_rowid, key_rowid);     projectionmap.put(key_name, key_name);     //...the same other fields     projectionmap.put(key_charting_proc, key_charting_proc);  }   @override public boolean oncreate() {     ourhelper = new dbhelper(getcontext());     return true; }  @override public string gettype(uri uri) {     switch (surimatcher.match(uri)) {         case match_all:             return my_content_type;         default:             throw new illegalargumentexception("unknown uri " + uri);     } }  @override public int delete(uri uri, string where, string[] whereargs) {     int count;     sqlitedatabase db = ourhelper.getwritabledatabase();     switch (surimatcher.match(uri)) {         case match_all: {             count = db.delete(database_table, where, whereargs);             getcontext().getcontentresolver().notifychange(uri, null);             return count;         }         default:             throw new illegalargumentexception("unknown uri " + uri);     } } //insert , update same: copy tutorial same changes  @override public cursor query(uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) {     sqlitequerybuilder qb = new sqlitequerybuilder();      sqlitedatabase db = dbhelper.getreadabledatabase();      switch (surimatcher.match(uri)) {         case match_all:             qb.settables(database_table);             qb.setprojectionmap(projectionmap);             break;         default:             throw new illegalargumentexception("unknown uri " + uri);     }      cursor c = qb.query(db, projection, selection, selectionargs, null, null, sortorder);     c.setnotificationuri(getcontext().getcontentresolver(), uri);     return c; } 

ii. create xml layout single database row (you have one). result: entry.xml

iii. write class myadapter extending simplecursoradapter, overriding it's newview() , bindview(). shoud write class constructor.

    class myadapter extends simplecursoradapter{     private int layout;      public myadapter (context context, int layout, cursor c, string[] from, int[] to) {         super(context, layout, c, from, to); //deprecated, used here simplicity         this.layout = layout; //your entry.xml id     }      @override     public view newview(context context, cursor cursor, viewgroup parent) {          cursor c = getcursor();          //creating row view entry.xml         final layoutinflater inflater = layoutinflater.from(context);         view v = inflater.inflate(layout, parent, false);          bindview(v, context, c)              return v;     }      @override     public void bindview(view v, context context, cursor c) {         //get data current cursor position         int mycol = c.getcolumnindex("my_field");         int myfield = c.getint(mycol);          //find widget in view, inflated entry.xml         imageview imgtype = (imageview) v.findviewbyid(r.id.imgmyfield);         if (imgtype != null){             if (myfield == 0)                 imgtype.setimageresource(r.drawable.img_zero);             else                 imgtype.setimageresource(r.drawable.img_non_zero);         }          return v;     }     } 

note, newview()/bindview called automatically each , every row in cursor when set adapter listview or datasource changes.

iv. in fragment's oncreateview: view v

        //deprecated: simplicity         //get database cursor provider         cursor cursor = managedquery(myprovider.my_content_uri, projection, null, null, basecolumns.id + " asc");     string[] datacolumns = {/* columns here*/ } ;     adapter             = new myadapter(             this.getactivity(),                             // context listview             r.layout.entry,          // points xml list item             cursor,                           // cursor items             datacolumns,             null     );     listview listview = (listview) v.findviewbyid(r.id.listview);     listview.setadapter(adapter); //here list view filled 

if not working, check searchabledictionary , notepad samples android sdk

important note: don't stick deprecated managedquery after listview gets work. use loaders instead.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -