android - How to display sqlite data in fragments..! -


i have activity can put 2 text in , saved in db , have 3 tabs each 1 has different picture in please can me code can display 2 texts on each tab! don't know how can make it!

sqlite activity:

sqlite activity

tab1:

tab1

public class databasehelper extends sqliteopenhelper {  public static final string database_name = "big.db"; public static final string table_name = "images"; public static final string name = "name"; public static final string place = "place"; public static final string key_id = "id";   public databasehelper(context context) {      super(context, database_name, null, 1);     sqlitedatabase db = this.getwritabledatabase();  }  @override public void oncreate(sqlitedatabase db) {     string create_images_table = "create table images ( " +             "id" + "integer primary key autoincrement, " +              "name text, " +             "place text )";      db.execsql(create_images_table); }   @override public void onupgrade(sqlitedatabase db, int i, int i1) {     db.execsql("drop table if exists images");     this.oncreate(db); }  public void insertentry(string name, string place) {      sqlitedatabase db = this.getwritabledatabase();     contentvalues contentvalues = new contentvalues();     contentvalues.put(name, name);     contentvalues.put(place, place);      db.insert(table_name, null, contentvalues); } 

}

public class tab1 extends fragment {     databasehelper db; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     view v = inflater.inflate(r.layout.tab1, container, false);      imageview imageview = (imageview) v.findviewbyid(r.id.img1);     string photopath = environment.getexternalstoragedirectory() + "/download/image1.jpg";     bitmap bitmap1 = bitmapfactory.decodefile(photopath);     bitmapfactory.options options = new bitmapfactory.options();     options.insamplesize = 8;     final bitmap b = bitmapfactory.decodefile(photopath, options);      imageview.setimagebitmap(b);       return v;  } 

just use below code retrive data table

class dbresponse{ public string name; public string place; } 

now

public dbresponse getdata(){   dbresponse obj = new dbresponse();   sqlitedatabase db = this.getwritabledatabase();   cursor cursor = db.rawquery("select * " + table_name, null);   if (cursor.getcount() > 0) {         cursor.movetofirst();   obj.name = cursor.getstring(cursor.getcolumnindex(name));   obj.place = cursor.getstring(cursor.getcolumnindex(place));   }   cursor.close();   return obj; } 

in fragment class write

databasehelper db = new databasehelper(getactivity()); dbresponse response = db.getdata();  textview.settext(response.name+" - "+response.place); 

hope out


Comments

Popular posts from this blog

many to many - Django Rest Framework ManyToMany filter multiple values -

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

Java Entity Manager - JSON reader was expecting a value but found 'db' -