android - Why should I use an additional layout file to present a ListView? -
i new in android technology , learning via net tutorials. while binding list view in confusion why create separate layout file , call layout file in arrayadapter
source code shown below: http://www.vogella.com/articles/androidlistview/article.html
when creating simple listview
, use this:
xml:
<?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:orientation="vertical" > <listview android:id="@+id/mylist" android:layout_width="match_parent" android:layout_height="wrap_content" > </listview> </linearlayout>
java:
listview listview = (listview) findviewbyid(r.id.mylist); string[] values = new string[] { "android", "iphone", "windowsmobile", "blackberry", "webos", "ubuntu", "windows7", "max os x", "linux", "os/2" }; // define new adapter // first parameter - context // second parameter - layout row // third parameter - id of textview data written // forth - array of data arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, android.r.id.text1, values); // assign adapter listview listview.setadapter(adapter);
when creating custom listview
, typically this:
package de.vogella.android.listactivity; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.textview; public class mysimplearrayadapter extends arrayadapter<string> { private final context context; private final string[] values; public mysimplearrayadapter(context context, string[] values) { super(context, r.layout.rowlayout, values); this.context = context; this.values = values; } @override public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); view rowview = inflater.inflate(r.layout.rowlayout, parent, false); textview textview = (textview) rowview.findviewbyid(r.id.label); imageview imageview = (imageview) rowview.findviewbyid(r.id.icon); textview.settext(values[position]); // change icon windows , iphone string s = values[position]; if (s.startswith("iphone")) { imageview.setimageresource(r.drawable.no); } else { imageview.setimageresource(r.drawable.ok); } return rowview; } }
in example, if @ line: view rowview = inflater.inflate(r.layout.rowlayout, parent, false);
, r.layout.rowlayout
custom layout used show custom listview
.
refer source link @ top of answer detailed tutorial on listview's
.
Comments
Post a Comment