通过使用自定义适配器产品名称排序的ListView?产品名称、自定义、适配器、ListView

2023-09-05 09:27:20 作者:找不到解药

我要排序的ListView中的项目按产品名称。我有一个名为数据向量这是一种类。

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&LT; RowData&GT;

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

我的排序$ C $词都写在的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.

我是新到Android,请帮助我。

I am new to android please help me.

感谢亚历克斯·洛克伍德,我使用类型的自定义适配器 ArrayAdaptor&LT;类&GT;

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 类,我要重写排序这样,

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

请帮我,如果你可以修改上面的code或建议我看看你的code。

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

在此先感谢。

推荐答案

如果您使用的是 ArrayAdapter ,你可以叫sort适配器上,并通过比较。你如何实现你的比较是由你。

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.

修改我:

您应该不需要重写排序 ...的 ArrayAdapter 类实现了你。所有你需要做的是建立自己的比较将排序的泛型类型的对象,你希望它们出现的方式。另外,如果你在运行时对列表进行排序,你可能需要通知该数据集已经改变了应用程序。您的code,如果你使用这种方法,应该可以正常工作。

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