Sunday, 15 May 2011

android - How to add item in Custom listView in onActivityResult method? -



android - How to add item in Custom listView in onActivityResult method? -

i developing simple app videos using android camera. in app have button having name "make video " , list view show video name recorded app. click button "make video " opens mobile photographic camera recording when finish recording photographic camera gives me 2 options. "save" , "discard". clicking "save" option, want add together name of recorded video list view. have developed code in respect , works fine,but facing issue how add together name of recorded video in listview in onactivityresult method , update list view. please help me thankful you.

you can check code below.

public class mainactivity extends listactivity { private arraylist<string> cameravideolist = new arraylist<string>(); context ctx; // resources res; int request_video_captured =1; uri urivideo = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); ctx = getapplicationcontext(); button makevideo = (button) findviewbyid(r.id.button1); makevideo.setonclicklistener(new onclicklistener() { public void onclick(view v) { //intent intent = new intent(android.provider.mediastore.action_image_capture); intent intent = new intent(android.provider.mediastore.action_video_capture); startactivityforresult(intent, request_video_captured); } }); listview videolist = getlistview(); videolist.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int position,long arg3) { toast.maketext(mainactivity.this, "" + position, toast.length_short).show(); } }); setlistadapter(new imageandtextadapter(ctx, r.layout.list_item_icon_text, cameravideolist)); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { // todo auto-generated method stub super.onactivityresult(requestcode, resultcode, data); if (resultcode == result_ok) { if (requestcode == request_video_captured) { urivideo = data.getdata(); // toast.maketext(mainactivity.this, urivideo.getpath(), // toast.length_long).show(); // // toast.maketext(mainactivity.this, urivideo.tostring(), // toast.length_long).show(); cameravideolist.add(getfilenamefromurl(urivideo.getpath().tostring())); } } } public string getfilenamefromurl(string path) { string[] patharray = path.split("/"); homecoming patharray[patharray.length - 1]; } public class imageandtextadapter extends arrayadapter<string> { private layoutinflater minflater; private arraylist<string> mstrings; private int mviewresourceid; public imageandtextadapter(context context, int textviewresourceid,arraylist<string> objects) { super(context, textviewresourceid, objects); minflater = (layoutinflater)ctx.getsystemservice(context.layout_inflater_service); mstrings = objects; mviewresourceid = textviewresourceid; } public view getview(int position, view convertview, viewgroup parent) { convertview = minflater.inflate(mviewresourceid, null); imageview iv = (imageview)convertview.findviewbyid(r.id.icon); iv.setimageresource(r.drawable.video_icon); textview tv = (textview)convertview.findviewbyid(r.id.text); tv.settext(mstrings.get(position)); homecoming convertview; } } }

as going right way here alter requirement.

as create custom adapter class object not work because when alter info in list object have notify adapter list view content.

declare imageandtextadapter adapter; global , private object

oncreate(){ adapter = new imageandtextadapter(ctx, r.layout.list_item_icon_text, cameravideolist); videolist.setadapter(adapter); }

ok in onactivityresult after adding new record list object phone call notifydatasetchange() of adapter class

@override protected void onactivityresult(int requestcode, int resultcode, intent data) { // todo auto-generated method stub super.onactivityresult(requestcode, resultcode, data); if (resultcode == result_ok) { if (requestcode == request_video_captured) { urivideo = data.getdata(); cameravideolist.add(getfilenamefromurl(urivideo.getpath().tostring())); adapter.notifydatasetchanged(); // here } } }

android listview

No comments:

Post a Comment