我工作的一个Android应用程序,需要显示一个列表[表],里面的布置[观点]
i am working on an Android app that needs showing a list[table], inside the layout[view]
我来自iPhone开发objC地,我有一个应用程序,显示视图中的表格[名单] [布局]
I come from iPhone dev objC land, and i have an app that shows a table[list] inside the view[layout]
因此,如何显示我的布局中的列表,并将它放置在指定位置[心]
So how to show a list inside my layout, and place it to specified location [center],
PS。我还没有发现在XML的图形化布局编辑器,其中是列表[表]列表? 2.我已经做了与列表视图一些测试,但它是一个观点,即取代XML视图,我想是我的XML中,,
ps. I havent found a list in the graphical layout editor of the xml, where is the list[table]? 2. I have done some tests with list views, but is a view, that replace the xml view, i want it inside my xml,,
非常感谢!
是的,当然,你可以做到这一点。
Yes, of course, you can do that
1),你需要有 listholder.xml
在这里,你可以从头开始在你布局视图任何事情,无论是ImageView的,textview..etc。只是不要忘了添加的ListView里面。例如:
1) you need to have listholder.xml
here, you can scratch anything in you layout view, either imageview, textview..etc. just don't forget to add ListView inside it. for example:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/head_logo_bg">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_label">
<TextView
android:id="@+id/city_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:text="Sydney"
android:textStyle="bold"
android:textSize="17sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="40sp">
<ListView
android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_centerVertical="true"
android:scrollingCache="false"/>
</LinearLayout>
2)对于自定义自己的列表项,必须创建 listitem.xml
即
2) For custom your own list item, you have to create listitem.xml
i.e.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listitemone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10sp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<ImageView android:id="@+id/user_image"
android:layout_width="80px" android:layout_height="80px"
android:layout_alignParentLeft="true"
android:layout_marginRight="5px"
android:src="@drawable/icon"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5sp"
android:orientation="vertical">
<TextView
android:id="@+id/date_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/date"
android:textStyle="bold"
android:textSize="16sp" />
<TextView
android:id="@+id/date_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBaseline="@id/date_label"
android:layout_marginRight="20sp"
android:textColor="#FFF"
android:text="MM/dd/YYYY"
android:textStyle="bold"
android:textSize="16sp" />
</RelativeLayout>
</LinerLayout>
3)您的活动创造customAdapter,它是这样的;
3) create customAdapter in your activity, it would look like this;
public class MyListActivity extends ListActivity {
private ArrayList<Yourdata> yourdata = new ArrayList<Youdata>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listholder);
// yourdata might be array, arraylist etc.
MyCustomAdapter listadapter = new MyCustomAdapter(this, R.layout.listitem, yourdata);
setListAdapter(listadapter);
}
private class MyCustomAdapter extends ArrayAdapter<Yourdata>{
//this case, i use Yourdata as type
private ArrayList<Yourdata> items;
public PreviousAdapter(Context context, int textViewResourceId,
ArrayList<Yourdata> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
}
Yourdata yt = items.get(position);
if(yt != null){
// Don't forget to use v.findView...., otherwise, it might force close when run app.
TextView dateStr = (TextView)v.findViewById(R.id.date_value);
dateStr.setText(yt.getDate());
}
return v;
}
}
}
P.S。上述code可能没有完全正确的...只是给你一个想法:) 下面是关于自定义列表源(你可能已经看到它)希望它有用
P.S. the above code might not exactly right... just give you an idea :) Here is a source about custom list (you might have seen it) hope it useful
http://www.vogella.de/articles/AndroidListView/article.html