android - Calling API and adding buttons programmatically -
long story short, i'm making api phone call retrieves list of names.
for each name retrieved, i'd add together relativelayout (with children) parent linearlayout. relative layout looks this:
<relativelayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/border_bottom" android:clickable="true"> <imageview android:layout_width="100dp" android:layout_height="100dp" android:layout_weight="11.79" android:background="#58585a" android:id="@+id/imageview" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="large text" android:id="@+id/textview2" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="26dp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancesmall" android:text="small text" android:id="@+id/textview3" android:layout_below="@+id/textview2" android:layout_alignstart="@+id/textview2" /> </relativelayout>
i'd alter 'small text' , 'large text' fields info received querying server.
what best way go achieving this? can store above layout in separate xml file, , alter text values, , drop existing linear layout?
listing 1:
xml layout of each list item -
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/ apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <imageview android:id="@+id/listimage" android:layout_width="100dp" android:layout_height="100dp" android:layout_alignparentleft="true" android:layout_centervertical="true" android:layout_margin="10dp" android:background="#ffcccccc" /> <textview android:id="@+id/listtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/listimage" android:layout_torightof="@+id/listimage" android:text="a list item title" android:textsize="16sp" android:textstyle="bold" /> <textview android:id="@+id/listdescription" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/listtitle" android:layout_margintop="5dp" android:maxlines="4" android:layout_torightof="@+id/listimage" android:text="the list item description" android:textsize="14sp" /> </relativelayout>
listing 2:
xml layout containing listview -
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <listview android:id="@android:id/list" android:fadingedge="vertical" android:fadingedgelength="10dp" android:longclickable="true" android:listselector="@drawable/list_selector_background" android:layout_width="match_parent" android:layout_height="match_parent" > </listview> <!-- --> <textview android:id="@android:id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="loading feed data..." /> <!-- --> </linearlayout> #1 listview id of "list" #2 textview id of "empty"
listing 3:
listactivity dynamic listview -
public class dynamiclistviewactivity extends listactivity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.list); imagelistadapter adapter = new imagelistadapter(this); setlistadapter(adapter); loadfeeddata loadfeeddata = new loadfeeddata(adapter); loadfeeddata.execute(); } }
listing 4:
loadfeeddata class used loading feed data -
public class loadfeeddata extends asynctask<void, void,="" arraylist<entry="">> { private final string murl = "url_quering_to_server"; private final imagelistadapter madapter; public loadfeeddata(imagelistadapter adapter) { madapter = adapter; } private inputstream retrievestream(string url) { defaulthttpclient client = new defaulthttpclient(); httpget httpget = null; httpget = new httpget(url); httpresponse httpresponse = null; seek { httpresponse = client.execute(httpget); httpentity getresponseentity = httpresponse.getentity(); homecoming getresponseentity.getcontent(); } grab (ioexception e) { httpget.abort(); } homecoming null; } @override protected arraylist<entry> doinbackground(void... params) { inputstream source = retrievestream(murl); reader reader = null; seek { reader = new inputstreamreader(source); } grab (exception e) { homecoming null; } gson gson = new gson(); searchresult result = gson.fromjson(reader,searchresult.class); homecoming result.getfeed().getentry(); } protected void onpostexecute(arraylist<entry> entries) { madapter.updateentries(entries); } }
listing 5:
imagelistadapter used populate listview info , images -
public class imagelistadapter extends baseadapter { private context mcontext; private layoutinflater mlayoutinflater; private arraylist<entry> mentries = new arraylist<entry>(); private final imagedownloader mimagedownloader; public imagelistadapter(context context) { mcontext = context; mlayoutinflater = (layoutinflater) mcontext .getsystemservice(context.layout_inflater_service); mimagedownloader = new imagedownloader(context); } @override public int getcount() { homecoming mentries.size(); } @override public object getitem(int position) { homecoming mentries.get(position); } @override public long getitemid(int position) { homecoming position; } @override public view getview(int position, view convertview, viewgroup parent) { relativelayout itemview; if (convertview == null) { itemview = (relativelayout) mlayoutinflater.inflate( r.layout.list_item, parent, false); } else { itemview = (relativelayout) convertview; } imageview imageview = (imageview) itemview.findviewbyid(r.id.listimage); textview titletext = (textview) itemview.findviewbyid(r.id.listtitle); textview descriptiontext = (textview) itemview.findviewbyid(r.id.listdescription); string imageurl = mentries.get(position).getcontent().getsrc(); mimagedownloader.download(imageurl, imageview); string title = mentries.get(position).gettitle().get$t(); titletext.settext(title); string description = mentries.get(position).getsummary().get$t(); if (description.trim().length() == 0) { description = "sorry, no description image."; } descriptiontext.settext(description); homecoming itemview; } public void updateentries(arraylist<entry> entries) { mentries = entries; notifydatasetchanged(); } }
great! way load dynamic info server , display in listview. obviously, may alter of or more add together suitable application. cheers!
android
No comments:
Post a Comment