Show some values from database android? -
i have 4 class. 1:databasehandler
package as.d.d; import java.util.arraylist; import java.util.list; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqlitedatabase.cursorfactory; import android.database.sqlite.sqliteopenhelper; public class databasehandler extends sqliteopenhelper{ private static final int database_version = 1; private static final string database_name = "contactsmanager"; private static final string table_contacts = "contacts"; private static final string key_id = "id"; private static final string key_name = "name"; private static final string key_ph_no = "phone_number"; public databasehandler(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { string create_contacts_table = "create table " + table_contacts + "(" + key_id + " integer primary key," + key_name + " text," + key_ph_no + " text" + ")"; db.execsql(create_contacts_table); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + table_contacts); oncreate(db); } void addcontact(studentinfo studentinfo) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_name, studentinfo.getname()); // contact name values.put(key_ph_no, studentinfo.getphonenumber()); // contact phone db.insert(table_contacts, null, values); db.close(); } studentinfo getcontact(int id) { sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.query(table_contacts, new string[] { key_id, key_name, key_ph_no }, key_id + "=?", new string[] { string.valueof(id) }, null, null, null, null); if (cursor != null) cursor.movetofirst(); studentinfo studentinfo = new studentinfo(integer.parseint(cursor.getstring(0)), cursor.getstring(1), cursor.getstring(2)); // return contact return studentinfo; } public list<studentinfo> getallstudentinfo() { list<studentinfo> studentlist = new arraylist<studentinfo>(); string selectquery = "select * " + table_contacts; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); if (cursor.movetofirst()) { { studentinfo studentinfo = new studentinfo(); studentinfo.setid(integer.parseint(cursor.getstring(0))); studentinfo.setname(cursor.getstring(1)); studentinfo.setphonenumber(cursor.getstring(2)); studentlist.add(studentinfo); } while (cursor.movetonext()); } return studentlist; } public int updatecontact(studentinfo studentinfo) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_name, studentinfo.getname()); values.put(key_ph_no, studentinfo.getphonenumber()); return db.update(table_contacts, values, key_id + " = ?", new string[] { string.valueof(studentinfo.getid()) }); }}
2:studentinfo
package as.d.d; public class studentinfo { int _id; string _name; string _phone_number; public studentinfo(){ } public studentinfo(int id, string name, string _phone_number){ this._id = id; this._name = name; this._phone_number = _phone_number; } public studentinfo(string name, string _phone_number){ this._name = name; this._phone_number = _phone_number; } public int getid(){ return this._id; } public void setid(int id){ this._id = id; } public string getname(){ return this._name; } public void setname(string name){ this._name = name; } public string getphonenumber(){ return this._phone_number; } public void setphonenumber(string phone_number){ this._phone_number = phone_number; }}
3:add
package as.d.d; import java.util.list; import android.app.activity; import android.content.intent; import android.os.bundle; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast; public class add extends activity implements onclicklistener{ button b1,b2; edittext e2,e3; textview t1; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.add); b1=(button)findviewbyid(r.id.button1); b2=(button)findviewbyid(r.id.button2); e2=(edittext)findviewbyid(r.id.edittext2); e3=(edittext)findviewbyid(r.id.edittext3); t1=(textview)findviewbyid(r.id.atextview1); b1.setonclicklistener(this); b2.setonclicklistener(this); databasehandler db = new databasehandler(this); list<studentinfo> studentinfo = db.getallstudentinfo(); (studentinfo cn : studentinfo){ t1.settext("id:"+(cn.getid()+1)); } } @override public void onclick(view v) { if(v==b2){ startactivity(new intent(add.this, fdactivity.class)); } else if(v==b1){ string s2=e2.gettext().tostring(); string s3=e3.gettext().tostring(); if(s2.trim().equals("")||s3.trim().equals("")){ toast.maketext(getapplicationcontext(), "please submit student information",toast.length_short).show(); } else{ databasehandler db = new databasehandler(this); log.d("insert: ", "inserting .."); db.addcontact(new studentinfo(s2,s3)); log.d("reading: ", "reading contacts.."); list<studentinfo> studentinfo = db.getallstudentinfo(); (studentinfo cn : studentinfo){ t1.settext("id:"+(cn.getid()+1)); }}}}}
4:addmark
package as.d.d; import java.util.arraylist; import java.util.list; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; import android.widget.autocompletetextview; import android.widget.textview; import android.widget.toast; public class addmark extends activity{ autocompletetextview a1; textview t1,t2; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.addmark); a1=(autocompletetextview)findviewbyid(r.id.autocompletetextview1); t1=(textview)findviewbyid(r.id.amtextview1); t2=(textview)findviewbyid(r.id.amtextview2); databasehandler db = new databasehandler(this); final list<studentinfo> studentinfo = db.getallstudentinfo(); final arraylist<string> s1 = new arraylist<string>(); final arraylist<string> s3 = new arraylist<string>(); (studentinfo cn : studentinfo) { s1.add(cn.getname()); s3.add(cn.getphonenumber()); } arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_dropdown_item_1line,s1); a1.setthreshold(1); a1.setadapter(adapter); a1.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { } });
in 3rd class(add) using textview autogenerated student id(numerical(int) , 2 text field submit student name , phone number database pressing button.here student id,name , phone number submitted succesfully.in fourth class use autocomplete textview showing names submitted class 3.when select name suppose farhan(which submitted database , id 4 , phone number 99876 stored in database) want see id , phone number in 2 textview(textview t1,t2;)?
use query:
cursor cursor = db.query(table_name, new string[] { key_id, key_ph_no }, key_name +"= inserted_value", null, null, null);
Comments
Post a Comment