android - how to display a listview in other class? -
i made custom animation sliding menu (like fb) , have insert listview in sublayout. listview in different class parsed through json using lazyadapter.
this mainactivity
package com.android.appslu; import android.annotation.suppresslint; import android.app.activity; import android.content.intent; import android.content.res.resources; import android.os.bundle; import android.util.log; import android.view.display; import android.view.motionevent; import android.view.view; import android.view.view.ontouchlistener; import android.view.viewgroup.layoutparams; import android.view.animation.animation; import android.view.animation.animation.animationlistener; import android.view.animation.translateanimation; import android.widget.button; import android.widget.listview; import android.widget.relativelayout; import android.widget.textview; import com.android.appslu.jsonparsing.jsonparsingactivity; public class mainactivity extends activity { private button buttonswitch; private button button1; private view sublayout; private view toplayout; private listview mylist; private string arraylist; // ={"android","iphone","blackberry","androidpeople"}; private display display; private view fakelayout; private animationlistener al; // values after animation private int oldleft; private int oldtop; private int newleft; private int newtop; private int screenwidth; private int animtopostion; // todo change name of animtopostion better explanation. private boolean menuopen = false; /** called when activity first created. */ @suppresslint("resourceascolor") @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); buttonswitch = (button) findviewbyid(r.id.button); button1 = (button) findviewbyid(r.id.button1); sublayout = findviewbyid(r.id.layout); toplayout = findviewbyid(r.id.layouttwo); mylist = (listview) findviewbyid(r.layout.list_item); fakelayout = findviewbyid(r.id.fake_layouy); // textview yourtextview = (textview)findviewbyid(titleid); // yourtextview.sethighlightcolor(r.color.red); // subviewlistview.setadapter((listadapter) new // arrayadapter<string>(this,android.r.layout.simple_list_item_1 , // arraylist)); // listadapter adapter = new simpleadapter(this, // mylist,r.layout.list_item, new string[]{tag_name,tag_url}, new // int[]{r.id.name, r.id.url}); display = getwindowmanager().getdefaultdisplay(); screenwidth = display.getwidth(); int calcanimationposition = (screenwidth / 3); // value ontop layer has animate // max width of layout underneath // set layout params sublayout according calculation animtopostion = screenwidth - calcanimationposition; relativelayout.layoutparams params = new relativelayout.layoutparams( animtopostion, layoutparams.fill_parent); sublayout.setlayoutparams(params); toplayout.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { if (event.getaction() == motionevent.action_down) { if (menuopen == true) { animslideleft(); } } return false; } }); buttonswitch.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (menuopen == false) { animslideright(); } else if (menuopen == true) { animslideleft(); } } }); button1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent newintent = new intent(mainactivity.this, jsonparsingactivity.class); startactivity(newintent); } }); al = new animationlistener() { @override public void onanimationstart(animation animation) { buttonswitch.setclickable(false); toplayout.setenabled(false); } @override public void onanimationrepeat(animation animation) { // todo auto-generated method stub } @override public void onanimationend(animation animation) { if (menuopen == true) { log.d("", "open"); toplayout.layout(oldleft, oldtop, oldleft + toplayout.getmeasuredwidth(), oldtop + toplayout.getmeasuredheight()); menuopen = false; buttonswitch.setclickable(true); toplayout.setenabled(true); } else if (menuopen == false) { log.d("", "false"); toplayout.layout(newleft, newtop, newleft + toplayout.getmeasuredwidth(), newtop + toplayout.getmeasuredheight()); toplayout.setenabled(true); menuopen = true; buttonswitch.setclickable(true); } } }; } public void animslideright() { fakelayout.setvisibility(view.visible); newleft = toplayout.getleft() + animtopostion; newtop = toplayout.gettop(); translateanimation slideright = new translateanimation(0, newleft, 0, 0); slideright.setduration(500); slideright.setfillenabled(true); slideright.setanimationlistener(al); toplayout.startanimation(slideright); } public void animslideleft() { fakelayout.setvisibility(view.gone); oldleft = toplayout.getleft() - animtopostion; oldtop = toplayout.gettop(); translateanimation slideleft = new translateanimation(newleft, oldleft, 0, 0); slideleft.setduration(500); slideleft.setfillenabled(true); slideleft.setanimationlistener(al); toplayout.startanimation(slideleft); } }`
the jsonparsingactivity
package com.android.appslu.jsonparsing; import java.util.arraylist; import java.util.hashmap; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.listactivity; import android.content.intent; import android.graphics.bitmap; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.imageview; import android.widget.listadapter; import android.widget.listview; import android.widget.textview; import com.android.appslu.mainactivity; import com.android.appslu.r; public class jsonparsingactivity extends listactivity { private static string url = "http:example.com"; // json node names private static final string tag_contacts = "categories"; private static final string tag_catbook = "buzzbyappcategory"; private static final string tag_apps = "buzzbysource"; static final string tag_name = "catname"; private static final string tag_id = "catid"; static final string tag_email = "caticon"; //private static final string tag_phone_home = "home"; // private static final string tag_phone_office = "office"; bitmap image; // contacts jsonarray jsonobject catlist = null; jsonarray catbook = null; jsonarray catapps = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.list_item); // hashmap listview arraylist<hashmap<string, string>> catlist = new arraylist<hashmap<string, string>>(); // creating json parser instance jparser jparser = new jparser(); // getting json string url jsonobject json = jparser.getjsonfromurl(url); try { // getting array of contacts catlist = json.getjsonobject(tag_contacts); catbook = catlist.getjsonarray(tag_catbook); // looping through contacts for(int = 0; < catbook.length(); i++){ jsonobject c = catbook.getjsonobject(i); // storing each json item in variable string id = c.getstring(tag_id); string name = c.getstring(tag_name); string email = c.getstring(tag_email); // string address = c.getstring(tag_address); // string gender = c.getstring(tag_gender); // phone number agin json object // jsonobject phone = c.getjsonobject(tag_phone); // string mobile = phone.getstring(tag_phone_mobile); // string home = phone.getstring(tag_phone_home); // string office = phone.getstring(tag_phone_office); // // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // adding each child node hashmap key => value map.put(tag_id, id); map.put(tag_name, name); map.put(tag_email, email); // map.put(tag_phone_mobile, mobile); // adding hashlist arraylist catlist.add(map); } catapps = catlist.getjsonarray(tag_apps); // looping through contacts for(int = 0; < catapps.length(); i++){ jsonobject c = catapps.getjsonobject(i); // storing each json item in variable string id = c.getstring(tag_id); string name = c.getstring(tag_name); string email = c.getstring(tag_email); // string address = c.getstring(tag_address); // string gender = c.getstring(tag_gender); // phone number agin json dobject // jsonobject phone = c.getjsonobject(tag_phone); // string mobile = phone.getstring(tag_phone_mobile); // string home = phone.getstring(tag_phone_home); // string office = phone.getstring(tag_phone_office); // // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // adding each child node hashmap key => value map.put(tag_id, id); map.put(tag_name, name); map.put(tag_email, email); // map.put(tag_phone_mobile, mobile); // adding hashlist arraylist catlist.add(map); } } catch (jsonexception e) { e.printstacktrace(); } /** * updating parsed json data listview * */ listadapter adapter = new lazyadapter(this, catlist); setlistadapter(adapter); // selecting single listview item listview lv = getlistview(); // launching new screen on selecting single listitem lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // getting values selected listitem string name = ((textview) view.findviewbyid(r.id.name)).gettext().tostring(); string cost = ((imageview) view.findviewbyid(r.id.email)).gettag().tostring(); // string description = ((textview) view.findviewbyid(r.id.mobile)).gettext().tostring(); // starting new intent intent in = new intent(getapplicationcontext(), singlemenuitemactivity.class); in.putextra(tag_name, name); in.putextra(tag_email, cost); //in.putextra(tag_phone_mobile, description); startactivity(in); } }); // intent i=new intent(jsonparsingactivity.this,mainactivity.class); // i.putstringarraylistextra("key",arl); // startactivity(i); // } }
and xml main activity is
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativelayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <surfaceview android:id="@+id/surfaceview1" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <relativelayout android:id="@+id/layout" android:layout_width="220dp" android:layout_height="fill_parent" android:background="#ffffff" android:orientation="vertical" > <linearlayout android:id="@+id/fake_layouy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:visibility="gone" > </linearlayout> <listview android:id="@+id/listview1" android:layout_width="match_parent" android:layout_height="wrap_content" > <textview android:id="@+id/name_label" android:layout_width="match_parent" android:layout_height="wrap_content"/> <imageview android:id="@+id/email_label" android:layout_width="match_parent" android:layout_height="wrap_content"/> </listview> </relativelayout> <relativelayout android:id="@+id/layouttwo" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/scrollback" android:orientation="vertical" > <linearlayout android:id="@+id/linearlayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_margin="2dp" android:orientation="vertical" > <listview android:id="@+id/layoutthree" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@+id/button" > </listview> <button android:id="@+id/button" android:layout_width="50dp" android:layout_height="wrap_content" android:text="slide" /> </linearlayout> </relativelayout>
my question how display listview jsonparsingactivity in sublayout of mainactivity.
im new android programming.... plz help..
thanks in advance..
extend jsonparsingactivity
listfragment. load jsonparsingactivity
in main activity.
updated code
fragmentmanager fragmentmanager = getfragmentmanager() fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction();
you can add fragment using add() method, specifying fragment add , view in insert it.
examplefragment fragment = new examplefragment(); fragmenttransaction.add(r.id.fragment_container, fragment); fragmenttransaction.commit();
please refer link ,everything fragment explained here.
this way can set userinterface in fragment. solution first 1 1.
public static class examplefragment extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment return inflater.inflate(r.layout.example_fragment, container, false); } }
solution second problem.
lazyadapter(getactivity(), arraylist<hashmap<string,string>>)
Comments
Post a Comment