android How to refresh my main activity after updating database? -
i have 3 activities,the first 1 used hold values database,and list view showed on second one,when click item in list view i'll jumping third activity , able edit item selected,after updating data in database,new datas shown in second activity,but when return first one,nothing refreshed.how can updated data in first activity too? appreciated.
the second activity code:
import android.content.contentvalues; import android.content.context; import android.content.intent; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.util.log; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.listadapter; import android.widget.listview; import android.widget.toast; public class setupactivity extends appcompatactivity { private context context; static string = "values"; listview mlistview; final string[] setitems = {"name","birthday","type","sex"}; @override public void oncreate(bundle savedinstance){ super.oncreate(savedinstance); setcontentview(r.layout.setuplist); mlistview = (listview)findviewbyid(r.id.listview); context = this; updateview(); } private void updateview(){ dogdatabasehelper dbhelper= new dogdatabasehelper(getapplicationcontext()); sqlitedatabase db = dbhelper.getwritabledatabase(); cursor cursor = db.query("dog",null,null,null,null,null,null,null); if(cursor.movetofirst()){ string name = cursor.getstring(cursor.getcolumnindex("name")); string birthday = cursor.getstring(cursor.getcolumnindex("birthday")); string size = cursor.getstring(cursor.getcolumnindex("size")); string sex = cursor.getstring(cursor.getcolumnindex("sex")); final string[] setvalues = {name,birthday,size,sex}; listadapter listadapter = new customadapter(this,setitems,setvalues); mlistview.setadapter(listadapter); mlistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int position, long id) { string setupitemvalue = setvalues[position]; string setupitem = setitems[position]; intent intent; if(setupitem.equals("name")){ intent = new intent(setupactivity.this,changename.class); intent.putextra(extra,setupitemvalue); startactivityforresult(intent,1); } else if(setupitem.equals("birthday")){ intent = new intent(setupactivity.this,changebirthday.class); intent.putextra(extra,setupitemvalue); startactivityforresult(intent,2); }else if(setupitem.equals("type")){ intent = new intent(setupactivity.this,changetype.class); intent.putextra(extra,setupitemvalue); startactivityforresult(intent,3); }else{ intent = new intent(setupactivity.this,changesex.class); intent.putextra(extra,setupitemvalue); startactivityforresult(intent,4); } } }); } cursor.close(); } @override protected void onactivityresult(int requestcode,int resultcode,intent data){ dogdatabasehelper dbhelper= new dogdatabasehelper(getapplicationcontext()); sqlitedatabase db = dbhelper.getwritabledatabase(); cursor cursor = db.query("dog",null,null,null,null,null,null,null); switch (requestcode){ case 1: if (resultcode == result_ok){ contentvalues cv = new contentvalues(); cv.put("name",data.getstringextra("return_name")); db.update("dog",cv,"id=?",new string[]{"1"}); } break; case 2: toast.maketext(this, "update2", toast.length_short).show(); if(resultcode == result_ok){ contentvalues cv = new contentvalues(); cv.put("birthday",data.getstringextra("return_birthday")); db.update("dog",cv,"id=?",new string[]{"1"}); } break; case 3: toast.maketext(this, "update3", toast.length_short).show(); if(resultcode == result_ok){ contentvalues cv = new contentvalues(); cv.put("size",data.getstringextra("return_type")); db.update("dog",cv,"id=?",new string[]{"1"}); } break; case 4: if(resultcode == result_ok){ contentvalues cv = new contentvalues(); cv.put("sex",data.getstringextra("return_sex")); toast.maketext(this, data.getstringextra("return_sex"), toast.length_short).show(); int r = db.update("dog",cv,"id=?",new string[]{"1"}); if(r != 1){ toast.maketext(this, "update3", toast.length_short).show(); } } break; } db.close(); updateview(); } @override public boolean oncreateoptionsmenu(menu menu){ menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.add,menu); return true; } @override public boolean onoptionsitem selected(menuitem item){ int id = item.getitemid(); if(id == r.id.add){ } return super.onoptionsitemselected(item); } }
you should open second activity startactivityforresult(intent,requestcode)
, can implement onactivityresult(...)
in first 1 can fetch data db once more , refresh views.
Comments
Post a Comment