Reading from a server in a list/details Fragment in Android -
Reading from a server in a list/details Fragment in Android -
i'm using code/concept :
android fragments
what wanting accomplish is, on item selected (as seen below), want send item server, have process on side (which can do) , retrieve string
sent , utilize string update details part of list/details fragment. there help me in achieving through code?
my code far follows:
public class mainactivity extends fragmentactivity implements headlinesfragment.onheadlineselectedlistener { xmlcreator x = new xmlcreator(); /** called when activity first created. */ public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.news_articles); // check whether activity using layout version // fragment_container framelayout. if so, must add together first fragment if (findviewbyid(r.id.fragment_container) != null) { // however, if we're beingness restored previous state, // don't need , should homecoming or else // end overlapping fragments. if (savedinstancestate != null) { return; } // create instance of examplefragment headlinesfragment firstfragment = new headlinesfragment(); // in case activity started special instructions intent, // pass intent's extras fragment arguments firstfragment.setarguments(getintent().getextras()); // add together fragment 'fragment_container' framelayout getsupportfragmentmanager().begintransaction() .add(r.id.fragment_container, firstfragment).commit(); } posttoserver(); } public void posttoserver() { new connection().execute(); } public void onarticleselected(int position) { // user selected headline of article headlinesfragment // capture article fragment activity layout articlefragment articlefrag = (articlefragment)getsupportfragmentmanager().findfragmentbyid(r.id.article_fragment); if (articlefrag != null) { // if article frag available, we're in two-pane layout... // phone call method in articlefragment update content string day = ""; day = ipsum.headlines[position]; document doc; seek { doc = x.createdoc(); element tutor = doc.createelement("query"); tutor.appendchild(x.createday(doc, day, "getsessionbyday")); doc.appendchild(tutor); string s = x.getstringfromdocument(doc); //connection.senddata(s); //want send , receive strings here } grab (exception e) { e.printstacktrace(); } articlefrag.updatearticleview(position); } else { // if frag not available, we're in one-pane layout , must swap frags... // create fragment , give argument selected article articlefragment newfragment = new articlefragment(); bundle args = new bundle(); args.putint(articlefragment.arg_position, position); newfragment.setarguments(args); fragmenttransaction transaction = getsupportfragmentmanager().begintransaction(); // replace whatever in fragment_container view fragment, // , add together transaction stack user can navigate transaction.replace(r.id.fragment_container, newfragment); transaction.addtobackstack(null); // commit transaction transaction.commit(); } } public class connection extends asynctask<void, void, string> { // i/o streams receiving/sending info from/to // server private objectoutputstream output; private objectinputstream input; private string message = ""; private socket client; @override protected void onpreexecute() { // important: method synched ui thread, can access ui super.onpreexecute(); } @override protected string doinbackground(void... params) { seek { client = new socket("10.111.1.1", 5001); } grab (unknownhostexception e1) { // todo auto-generated grab block e1.printstacktrace(); } grab (ioexception e1) { // todo auto-generated grab block e1.printstacktrace(); } seek { output = new objectoutputstream(client.getoutputstream()); output.flush(); input = new objectinputstream(client.getinputstream()); } grab (ioexception e1) { // todo auto-generated grab block e1.printstacktrace(); } { seek { message = (string) input.readobject(); } grab (classnotfoundexception classnotfoundexception) { } grab (optionaldataexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (ioexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (exception e) { // todo auto-generated grab block e.printstacktrace(); } } while (!message.equals("server>>> terminate")); seek { output.writeobject(message); output.flush(); } grab (ioexception ioexception) { } // sleep bit, task isn't on before starts seek { thread.sleep(500); } grab (interruptedexception e) { log.e("task", "error sleeping in calculatetask"); } // homecoming value passed onpostexecute of task initiator homecoming message; } @override protected void onprogressupdate(void... progress) { // important: method synched ui thread, can access ui } @override protected void onpostexecute(string result) { // important: method synched ui thread, can access ui super.onpostexecute(result); // update ui } }
}
just phone call server fragment, method getdata()
, within that, think have implement asynktask
. when response of request in doinbackground()
, pass onpostexecute()
, utilize response , update ui whatever want.
android android-fragments
Comments
Post a Comment