android - How to populate listview with some specific data from SQLite ?? not whole -
in sms app android, don't want show these sms in native inbox , sent items. have created sqlite database storing sent , received sms private app.
sqlite database code :
protected static final string table1=("create table " +table_sms+" (" + key_messageid+ " integer primary key autoincrement, " +sms_time+ " text, " +phone_number+ " text, " +message_body+ " text, " +flag+" text);"); public void insert_sms_data(string formatedtime, string number, string body,string flag){ try{ sqlitedatabase db = this.getwritabledatabase(); contentvalues cv = new contentvalues(); cv.put(sms_time, formatedtime); cv.put(phone_number, number); cv.put(message_body, body); cv.put(flag, flag); db.insert(table_sms, null, cv); db.close(); } catch(exception ex) { log.e("error in insertion", ex.tostring()); } } public cursor return_all(){ sqlitedatabase db = this.getreadabledatabase(); cursor cur = db.rawquery("select * "+"sms_table_rcv", null); return cur; }
sms received , stored in db follows:
flag = "0"; dbmanager= new dbmanager(context); dbmanager.open(); dbmanager.insert_sms_data(formatedtime ,number,body,flag); dbmanager.close();
code sending sms , showing sms in listview follows:
public class chatactivity extends listactivity { private mylistadapter adapter; arraylist<string> item_id = new arraylist<string>(); arraylist<string> item_phone_num = new arraylist<string>(); arraylist<string> item_msg_body = new arraylist<string>(); arraylist<string> item_time = new arraylist<string>(); arraylist<string> item_flag = new arraylist<string>(); arraylist<string> items = new arraylist<string>(); private button btn_send; dbmanager manager; cursor cursor; viewholder holder12; string contact_for_chat; string contact_no; string message_body = ""; calendar c; simpledateformat sdf; string time; edittext et_chat; string flag; string msg = ""; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); bundle bundle = getintent().getextras(); contact_for_chat = bundle.getstring("contact_name"); contact_for_chat = contact_for_chat.replace(" ", ""); contact_no = util.getcontactnumber(contact_for_chat, chatactivity.this); toast.maketext(getapplicationcontext(), contact_no, toast.length_long).show(); final viewholder holder = new viewholder(); manager = new dbmanager(this); cursor = manager.return_all(); showevents(cursor); c = calendar.getinstance(); sdf = new simpledateformat("dd:mmmm:yyyy hh:mm:ss a"); time = sdf.format(c.gettime()); setactionbar(); findviewsbyid(); adapter = new mylistadapter(this); setlistadapter(adapter); btn_send = (button) findviewbyid(r.id.button1); btn_send.setonclicklistener(new onclicklistener() { public void onclick(view v) { sendsms(); } }); } protected void sendsms() { smsmanager sms_manager = smsmanager.getdefault(); message_body = et_chat.gettext().tostring(); arraylist<string> parts = sms_manager.dividemessage(message_body); sms_manager.sendmultiparttextmessage(contact_no, null, parts, null, null); flag = "1"; manager.insert_sms_data(time, contact_no, message_body,flag); msg+= "sms :" + contact_for_chat + " \n"; msg += "having number:" + contact_no + " \n"; msg += "as" +message_body + " \n"; msg += "at"+ time + " \n"; toast.maketext(getapplicationcontext(), ""+msg , toast.length_long).show(); } private void setactionbar() { actionbar mactionbar = getactionbar(); mactionbar.setdisplayhomeasupenabled(false); mactionbar.setdisplayshowtitleenabled(false); layoutinflater minflater = layoutinflater.from(this); view mcustomview = minflater.inflate(r.layout.actionbar_chat, null); textview tv_chat = (textview)mcustomview.findviewbyid(r.id.title_text); tv_chat.settext(contact_for_chat); colordrawable colordaawable = new colordrawable(color.parsecolor("#cfcfc4")); mactionbar.setbackgrounddrawable(colordaawable); mactionbar.setlogo(r.drawable.ic_launcher); mactionbar.setdisplayhomeasupenabled(true); mactionbar.setcustomview(mcustomview); mactionbar.setdisplayshowcustomenabled(true); } private void findviewsbyid() { et_chat = (edittext)findviewbyid(r.id.et_chat); btn_send = (button)findviewbyid(r.id.btn_send); } private void showevents(cursor cursor) { item_id = new arraylist<string>(cursor.getcount()); item_phone_num = new arraylist<string>(cursor.getcount()); item_msg_body = new arraylist<string>(cursor.getcount()); item_time = new arraylist<string>(cursor.getcount()); item_flag = new arraylist<string>(cursor.getcount()); int i=0; while (cursor.movetonext()) { item_id.add(i+""); item_time.add(cursor.getstring(1)); item_msg_body.add(cursor.getstring(3)); item_phone_num.add(cursor.getstring(2)); item_flag.add(cursor.getstring(4)); i++; } } public class mylistadapter extends baseadapter { context con; private layoutinflater layoutinf; arraylist<boolean> itemchecked = new arraylist<boolean>(); arraylist<string> items_ = new arraylist<string>(); public mylistadapter(chatactivity context) { con = context; } public int getcount() { return item_id.size(); } public object getitem(int position) { return item_id.size(); } public long getitemid(int position) { return item_id.get(position).hashcode(); } public view getview(final int position, view arg1, viewgroup arg2) { view v = arg1; viewholder holder = null; if (v == null) { layoutinf = (layoutinflater) getsystemservice(context.layout_inflater_service); v = layoutinf.inflate(r.layout.row_chat, null); holder = new viewholder(); holder.tv_contact = (textview) v.findviewbyid(r.id.phone_num); holder.tv_sms_body = (textview) v.findviewbyid(r.id.msg_body); holder.tv_time = (textview) v.findviewbyid(r.id.time); v.settag(holder); } else { holder = (viewholder) v.gettag(); } if(item_flag.get(position).equals("1")) { holder.tv_sms_body.setbackgroundresource(r.drawable.bubble_green); } else { holder.tv_sms_body.setbackgroundresource(r.drawable.bubble_yellow); } holder.tv_contact.settext("" + item_phone_num.get(position)); holder.tv_sms_body.settext(item_msg_body.get(position)); holder.tv_time.settext(item_time.get(position)); return v; } } public class viewholder { private textview tv_contact; private textview tv_sms_body; private textview tv_time; }
}
i retreive following result :
result http://dc728.4shared.com/img/u4aarl8pce/s3/144ab8d92d0/device-2014-03-10-151709.png
contact no of angelina 5556 , list shows sms sent other numbers well, how can poupulate listview specific contact numbers selecetd. appreciated. in advance !!
you can add clause in return_all() function of sqllite database code. pass value of when call return_all().
like
public cursor return_all(arraylist where){
sqlitedatabase db = this.getreadabledatabase(); string query= "where "; for(int i=0;i<where.size;i++) { query=query+"contact = "+where[i]+ " or "; } cursor cur = db.rawquery("select * "+"sms_table_rcv" + query , null); return cur; }
Comments
Post a Comment