in android application need add filechooser in listview rows.the listview contains imagebutton open filechooser.the listview filled using arrayadapter.if choose file, need show filename in listview, need pass selected listitem activity result parent activity arrayadapter.but when click on gallery using filechooser shows error in device below
the application camera(process com.android.gallery) has stoped unexpectedly.
its because of line
intent.putextra("browsecoa", itemtobrowse);
in advattachmentadapter class.i need pass itemtobrowse parent activity(ie,addadvance) activity result.
how solve this?below code
addadvance.java:
public class addadvance extends activity{ private static final int file_select_code = 2; advattachmentadapter atcmtadapter; resttemplate resttemplate=new resttemplate(); string constr; int advpar; decimalformat df=new decimalformat("#.00"); listview lstattachment; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.add_advance); constr=getresources().getstring(r.string.base_url); curacct=(coaaccount) getintent().getserializableextra("coaacct"); advpar=getintent().getextras().getint("advpar"); lstattachment=(listview) findviewbyid(r.id.lstattachment); addattachment(); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { switch (requestcode) { case file_select_code: if (resultcode == result_ok) { // uri of selected file uri uri = data.getdata(); coaaccount advattmtcoa=(coaaccount) data.getserializableextra("browsecoa"); string path = getpath(this, uri); file file = new file(path); int reppos=atcmtadapter.getposition(advattmtcoa); atcmtadapter.remove(advattmtcoa); advattmtcoa.setstrname(file.getname()); advattmtcoa.setaltname(path); atcmtadapter.insert(advattmtcoa, reppos); } break; } super.onactivityresult(requestcode, resultcode, data); } public static string getpath(context context, uri uri) { if ("content".equalsignorecase(uri.getscheme())) { string[] projection = { "_data" }; cursor cursor = null; try { cursor = context.getcontentresolver().query(uri, projection, null, null, null); int column_index = cursor.getcolumnindexorthrow("_data"); if (cursor.movetofirst()) { return cursor.getstring(column_index); } } catch (exception e) { // eat } } else if ("file".equalsignorecase(uri.getscheme())) { return uri.getpath(); } return null; } private void addattachment(){ if(advpar==-26){ atcmtadapter=new advattachmentadapter(addadvance.this, r.layout.attachment_list, new arraylist<coaaccount>()); lstattachment.setadapter(atcmtadapter); coaaccount newattmt=new coaaccount(); newattmt.settransdate(curacct.getglobaldate()); newattmt.setstrname(""); newattmt.setaltname(""); atcmtadapter.add(newattmt); } } }
advattachmentadapter.java:
public class advattachmentadapter extends arrayadapter<coaaccount>{ private context context; private addadvance advcontext; private final int resourceid; private list<coaaccount> items; decimalformat df=new decimalformat("#.00"); simpledateformat sdf = new simpledateformat("dd/mm/yyyy"); private static final int file_select_code = 2; public advattachmentadapter(addadvance advcontext, int resource, list<coaaccount> items) { super(advcontext, resource,items); this.advcontext = advcontext; this.resourceid = resource; this.items=items; } public static class advanceatmtholder{ coaaccount advattcoa; textview txtattachment; imagebutton imgbrowse; } @override public int getcount() { // todo auto-generated method stub return items.size(); } @override public coaaccount getitem(int position) { // todo auto-generated method stub return items.get(position); } @override public view getview(int position, view convertview, viewgroup parent) { df.setminimumintegerdigits(1); advanceatmtholder advattholder=new advanceatmtholder(); layoutinflater inflater=advcontext.getlayoutinflater(); view rowview=inflater.inflate(resourceid, parent, false); advattholder.advattcoa=items.get(position); advattholder.txtattachment=(textview) rowview.findviewbyid(r.id.txtattachment); advattholder.imgbrowse=(imagebutton) rowview.findviewbyid(r.id.imgbrowse); advattholder.imgbrowse.settag(advattholder.advattcoa); browserow(advattholder); setupitem(advattholder); return rowview; } private void browserow(advanceatmtholder advattholder) { advattholder.imgbrowse.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { coaaccount itemtobrowse = (coaaccount)v.gettag(); intent intent = new intent(intent.action_get_content); intent.settype("*/*"); intent.addcategory(intent.category_openable); intent.putextra("browsecoa", itemtobrowse); try { advcontext.startactivityforresult(intent.createchooser(intent, "select file upload"),file_select_code); } catch (exception ex) { system.out.println(ex); } } }); } private void setupitem(advanceatmtholder advattholder) { advattholder.txtattachment.settext(advattholder.advattcoa.getaltname()); } }
coaaccount.java:
public class coaaccount implements serializable { private int id; private string strname; private string altname; private float budamount; public coaaccount() { super(); // todo auto-generated constructor stub } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getstrname() { return strname; } public void setstrname(string strname) { this.strname = strname; } public string getaltname() { return altname; } public void setaltname(string altname) { this.altname = altname; } @override public string tostring() { return strname; } }
to pass serializable
object, can put bundle
, put bundle
intent
.
bundle bundle = new bundle(); bundle.putserializable("browsecoa", itemtobrowse); intent.putextras(bundle);
or can use parcelable
instead of serializable
advantages.
Comments
Post a Comment