列表视图的自定义适配器

哈莎MV

我想custom adapter为我的列表视图创建一个有什么文章可以指导我如何创建它,并解释其工作原理?

拉希塔
public class ListAdapter extends ArrayAdapter<Item> {

    private int resourceLayout;
    private Context mContext;

    public ListAdapter(Context context, int resource, List<Item> items) {
        super(context, resource, items);
        this.resourceLayout = resource;
        this.mContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(mContext);
            v = vi.inflate(resourceLayout, 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;
    }

}

这是我在项目中使用的课程。您需要收藏要显示的商品,在我的情况下是<Item>您需要重写View getView(int position, View convertView, ViewGroup parent)方法。

R.layout.itemlistrow定义的行ListView

<?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>

在这样的MainActivity定义中ListView

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);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

自定义适配器不显示列表视图

如何使用Hashmap为自定义列表视图创建自定义适配器

使用自定义适配器视图时,如何使列表视图可点击?

使用自定义适配器和视图持有器的Android列表视图

自定义文本视图适配器不适用于列表视图

在自定义列表视图适配器中使用多个视图

列表视图中的页脚按钮,如何从自定义列表适配器获取价值

将列表视图的数据从Asynctask传递到自定义列表适配器类

自定义视图的适配器中的NullPointerException

自定义适配器视图(带有图片)

如何使用自定义适配器单击列表视图中的项目

使用带有文本和图像的自定义适配器在列表视图中搜索

自定义列表视图和适配器覆盖getView,更改文本颜色

如何创建自定义适配器以将 cardview 用于片段内的列表视图

自定义适配器不在列表视图中显示文本

对于使用一个自定义适配器的多个列表视图

在自定义可扩展列表适配器中无法充实视图

在自定义列表视图适配器(kotlin)中设置默认突出显示的项目

如何为用于图像和文本的列表视图创建自定义光标适配器?

如何为列表视图创建自定义适配器?获取RessourceNotFoundException

如何通过自定义适配器为列表视图动态设置imageView源?

自定义适配器不会使列表视图中的行膨胀

无法解析适配器中自定义列表视图的布局

在自定义适配器列表视图中突出显示搜索到的文本

使用自定义列表视图适配器和不同布局的结果不正确

使用外部XML文件进行自定义列表视图适配器?

自定义数组适配器到列表视图错误

滚动列表视图和自定义适配器的问题

Android Java:用于滚动和过滤问题的列表视图的自定义适配器