自定义适配器列表视图 [英] Custom Adapter for List View
问题描述
我想创建一个我的列表视图自定义适配器
。是否有任何物品,可以通过走我如何创建一个,也解释了它是如何工作?
公共类ListAdapter扩展ArrayAdapter<项目> {
公共ListAdapter(上下文的背景下,INT textViewResourceId){
超(背景下,textViewResourceId);
}
公共ListAdapter(上下文的背景下,INT资源,列表和LT;项目>项目){
超(背景下,资源,项目);
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
视图V = convertView;
如果(V == NULL){
LayoutInflater六;
VI = LayoutInflater.from(的getContext());
V = vi.inflate(R.layout.itemlistrow,NULL);
}
项目P =的getItem(位置);
如果(P!= NULL){
TextView的TT1 =(TextView中)v.findViewById(R.id.id);
TextView的TT2 =(TextView中)v.findViewById(R.id.categoryId);
TextView中TT3 =(TextView中)v.findViewById(R.id.description);
如果(TT1!= NULL){
tt1.setText(p.getId());
}
如果(TT2!= NULL){
tt2.setText(p.getCategory()的getId());
}
如果(TT3!= NULL){
tt3.setText(p.getDescription());
}
}
返回伏;
}
}
这是一类我用了我的项目。你需要对你的项目,你要显示的集合,在我的情况下,它<项目>
。你需要重写查看getView(INT位置,查看convertView,ViewGroup中父)
方法。
R.layout.itemlistrow是XML布局定义了ListView中的行。检查如下:
< XML版本=1.0编码=UTF-8&GT?;
< TableLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_height =WRAP_CONTENT机器人:方向=垂直
机器人:layout_width =FILL_PARENT>
<的TableRow机器人:layout_width =FILL_PARENT
机器人:ID =@ + ID / TableRow01
机器人:layout_height =WRAP_CONTENT>
< TextView的Android版本:文字颜色=#FFFFFF
机器人:ID =@ + D / I
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=ID的Android版本:TEXTSTYLE =黑体
机器人:重力=左
机器人:layout_weight =1
机器人:字体=等宽
机器人:身高=40SP/>
< /的TableRow>
<的TableRow安卓layout_height =WRAP_CONTENT
机器人:layout_width =FILL_PARENT>
< TextView的Android版本:文字颜色=#FFFFFF
机器人:ID =@ + ID /的categoryId
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=的categoryId
机器人:layout_weight =1
机器人:身高=20SP/>
< TextView的Android版本:layout_height =WRAP_CONTENT
机器人:layout_width =FILL_PARENT
机器人:layout_weight =1
机器人:文字颜色=#FFFFFF
机器人:重力=右
机器人:ID =@ + ID /说明
机器人:文本=说明
机器人:身高=20SP/>
< /的TableRow>
< / TableLayout>
我希望你有一个定义一个ListView的主要活动。
在你的Activity类,你可以这样做。
的ListView yourListView =(ListView控件)findViewById(R.id.itemListView);
//由ListAdapter得到从表数据
ListAdapter customAdapter =新ListAdapter(这一点,R.layout.itemlistrow,名单,其中,yourItem>);
yourListView .setAdapter(customAdapter);
希望这有助于。如果您有任何疑问,请询问。
I want to create a custom adapter
for my list view. Is there any article which can walk me through how to create one and also explain how it works?
public class ListAdapter extends ArrayAdapter<Item> {
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.itemlistrow, null);
}
Item p = getItem(position);
if (p != null) {
TextView tt1 = (TextView) v.findViewById(R.id.id);
TextView tt2 = (TextView) v.findViewById(R.id.categoryId);
TextView tt3 = (TextView) v.findViewById(R.id.description);
if (tt1 != null) {
tt1.setText(p.getId());
}
if (tt2 != null) {
tt2.setText(p.getCategory().getId());
}
if (tt3 != null) {
tt3.setText(p.getDescription());
}
}
return v;
}
}
This is a class I had used for my project. You need to have a collection of your items which you want to display, in my case it's <Item>
. You need to override View getView(int position, View convertView, ViewGroup parent)
method.
R.layout.itemlistrow is the XML layout which defines the row of the ListView. Check below.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="vertical"
android:layout_width="fill_parent">
<TableRow android:layout_width="fill_parent"
android:id="@+id/TableRow01"
android:layout_height="wrap_content">
<TextView android:textColor="#FFFFFF"
android:id="@+id/id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="id" android:textStyle="bold"
android:gravity="left"
android:layout_weight="1"
android:typeface="monospace"
android:height="40sp" />
</TableRow>
<TableRow android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView android:textColor="#FFFFFF"
android:id="@+id/categoryId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="categoryId"
android:layout_weight="1"
android:height="20sp" />
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:gravity="right"
android:id="@+id/description"
android:text="description"
android:height="20sp" />
</TableRow>
</TableLayout>
I hope you have the main activity with a ListView defined.
In your Activity class, you can do like this.
ListView yourListView = (ListView) findViewById(R.id.itemListView);
// get data from the table by the ListAdapter
ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, List<yourItem>);
yourListView .setAdapter(customAdapter);
Hope this helps. If you have any queries, please ask.
这篇关于自定义适配器列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!