自定义适配器列表视图

编程入门 行业动态 更新时间:2024-10-25 02:21:22
本文介绍了自定义适配器列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想创建一个我的列表视图自定义适配器。是否有任何物品,可以通过走我如何创建一个,也解释了它是如何工作?

解决方案

公共类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:机器人=htt​​p://schemas.android/apk/res/android     机器人:layout_height =WRAP_CONTENT机器人:方向=垂直     机器人:layout_width =FILL_PARENT>     <的TableRow机器人:layout_width =FILL_PARENT               机器人:ID =@ + ID / TableRow01               机器人:layout_height =WRAP_CONTENT>         < TextView的Andr​​oid版本:文字颜色=#FFFFFF                   机器人:ID =@ + D / I                   机器人:layout_width =FILL_PARENT                   机器人:layout_height =WRAP_CONTENT                   机器人:文本=ID的Andr​​oid版本:TEXTSTYLE =黑体                   机器人:重力=左                   机器人:layout_weight =1                   机器人:字体=等宽                   机器人:身高=40SP/>     < /的TableRow>     <的TableRow安卓layout_height =WRAP_CONTENT               机器人:layout_width =FILL_PARENT>         < TextView的Andr​​oid版本:文字颜色=#FFFFFF                   机器人:ID =@ + ID /的categoryId                   机器人:layout_width =FILL_PARENT                   机器人:layout_height =WRAP_CONTENT                   机器人:文本=的categoryId                   机器人:layout_weight =1                   机器人:身高=20SP/>         < TextView的Andr​​oid版本: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="schemas.android/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.

更多推荐

自定义适配器列表视图

本文发布于:2023-11-26 21:30:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635258.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   适配器   视图   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!