c# - Can't receive data from intent -
i using monodroid make application. in application user clicks button goes listview, listview has multiple options, user selects on option , option sent original class textview changed same text option on listview selected.
below class sends data
protected override void onlistitemclick(listview l, view v, int position, long id) { int t = labels[position]; string text = t + ""; intent send = new intent(this, typeof(createvehicle)); send.setflags(activityflags.cleartop | activityflags.singletop); send.putextra("t", "data firstactivity"); startactivity(send); }
below receiving class:
protected override void onresume() { base.onresume(); // call superclass first. textview tvyearchange = findviewbyid<textview>(resource.id.tvyearchange); string text = intent.getstringextra("t") ?? "work"; tvyearchange.settext(text, null); }
if figure out why i'm not receiving data great.
i not sure if got problem correctly guess main activity calling activity has listview , want result in main activity, right?
if so, code displayed not right way want. looking use startactivityforresult
, overriding onactivityresult
method in main activity.
i don't know how use monodroid , c# can give example in java sure understand how want:
let's listview activity called mylist , extends listactivity , main activity called mainactivity. below onlistitemclick method of mylist:
@override protected void onlistitemclick(listview l, view v, int position, long id){ super.onlistitemclick(l, v, position, id); string text = string.valueof(labels[position]); // create intent extras intent send = new intent(); send.putextra("text_from_list", text); // set result ok (meaning extras put expected caller) setresult(result_ok, send); // need caller (mainactivity) knows user completed // activity (mylist) expected (clicking on item return result) // , didn't leave activity (back button) // close list activity finish(); }
below method in main activity calls mylist:
private void calllistactivity(){ intent call = new intent(mainactivity.this, mylist.class); startactivityforresult(call, 1); // second field number identify activity // returned result when result received (you can have // several calls different activities expecting results // each 1 of them). number called requestcode. }
you have override onactivityresult
in mainactivity this:
@override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); // check if returned data came mylist (requestcode 1) , if // data received (check if resultcode = result_ok) if (requestcode == 1) { if (resultcode == result_ok) { string text = data.getstringextra("text_from_list"); // have use same string identifier used // when put in intent in mylist textview tvyearchange = (textview)findviewbyid(r.id.tvyearchange); tvyearchange.settext(text); } else { // if activity didn't return data // (resultcode != result_ok) } } // if expecting results other activities put more if // clauses here appropriate request code, example: // if (requestcode == 2) { // dosomething; // } // , on... }
it shouldn't hard modify java code c# code can use monodroid. also, take @ this link android official documentation, have lot of useful stuff there.
i hope helps you.
Comments
Post a Comment