使用自定义适配器按产品名称对 ListView 进行排序? [英] Sorting of ListView by Name of the Product using Custom adaptor?

查看:20
本文介绍了使用自定义适配器按产品名称对 ListView 进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按产品名称对 ListView 的项目进行排序.我有一个名为data"的向量,它是一种类.

I want to sort items of the ListView by product name. I have a vector called "data" which is a type of class.

我的课程是:

public static class RowData implements Comparable<RowData>{
    public String mProductName;
    protected int mId;

    protected int mOnHand;
    protected double mPrice;
    protected boolean mIsColleaction;
    protected boolean mIsPrePack;

    RowData(int id ,String productName,int onhand,double price,boolean IsColleaction, boolean IsPrePack) {
        mId= id;
        mProductName= productName;
        mOnHand =onhand;
        mPrice = price;
        mIsColleaction =IsColleaction;
        mIsPrePack = IsPrePack;
    }

    @Override
    public String toString() {
        return mProductName;
    }

    public int compareTo(RowData other) {
        return mProductName.compareTo(other.mProductName);
    }

    public static Comparator<RowData> COMPARE_BY_PRODUCTNAME = new Comparator<RowData>() {
        public int compare(RowData one, RowData other) {
            return one.mProductName.compareTo(other.mProductName);
        }
    };
}

并且我采用了一个自定义适配器,它扩展了 ArrayAdapter.

and i have taken a custom adapter which extends ArrayAdapter<RowData>.

我在onCreate()中编写的排序代码如下,

My sorting code i have written in onCreate() is as follows,

Collections.sort(data, RowData.COMPARE_BY_PRODUCTNAME);
adapter = new CustomAdapter(this, R.layout.list,R.id.title, data);

setListAdapter(adapter);
adapter.notifyDataSetChanged();

我必须使用自定义适配器,因为要显示产品的价格以及产品的在手数量

I have to use a custom adaptor because to show price of the product as well as quantity in hand of the product

数据是 RowData 类型的向量,我在调试后得到我想要的产品名称的排序顺序,但是当绑定到 ListView 时它不是按排序顺序显示.

the data which is a vector of type RowData I am getting after debugging in sorted order of the product Name which I want but when bind to the ListView it is not displaying in sorted order.

我是安卓新手,请帮助我.

I am new to android please help me.

谢谢 Alex Lockwood,我正在使用 ArrayAdaptor 类型的自定义适配器.

Thanks Alex Lockwood, I am using custom adapter of type ArrayAdaptor<Class>.

在我的 onCreate() 方法中,我正在实现如下排序方法,

In my onCreate() method I am implementing sorting method like the below,

adapter = new CustomAdapter(this, R.layout.list,R.id.title, data);

adapter.sort(new Comparator<RowData>() {
    public int compare(RowData arg0, RowData arg1) {
        return arg0.mProductName.compareTo(arg1.mProductName);
    }
});

setListAdapter(adapter);

在我的 customAdaptor 类中,我必须像这样覆盖 sort

In my customAdaptor class, I have to override sort like this,

public void sort(Comparator<? super RowData> comparator) {
    // TODO Auto-generated method stub
    super.sort(comparator);
}

如果你能修改上面的代码或建议我你的代码,请帮助我.

Please help me if you can modify the above code or suggest me your code.

提前致谢.

推荐答案

如果您使用的是 ArrayAdapter,您可以调用 sort适配器并传递一个 Comparator.您如何实现 Comparator 取决于您.

If you're using an ArrayAdapter, you can call sort on the adapter and pass a Comparator. How you implement your Comparator is up to you.

编辑我:

您不需要覆盖 sort...ArrayAdapter 类会为您实现.您需要做的就是创建您自己的 Comparator,它将按照您希望它们出现的方式对泛型类型对象进行排序.此外,如果您在运行时对列表进行排序,您可能需要通知应用程序数据集已更改.如果您使用这种方法,您的代码应该可以正常工作.

You shouldn't need to override sort... the ArrayAdapter class implements that for you. All you need to do is create your own Comparator that will sort the generic type objects the way you want them to appear. Also, if you sort the list during runtime, you might need to notify the application that the data set has changed. Your code should work correctly if you use this approach.

adapter = new CustomAdapter(this, R.layout.list,R.id.title, data);

adapter.sort(new Comparator<RowData>() {
    public int compare(RowData arg0, RowData arg1) {
        return arg0.mProductName.compareTo(arg1.mProductName);
    }
});

setListAdapter(adapter);
adapter.notifyDataSetChanged();

这篇关于使用自定义适配器按产品名称对 ListView 进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆