本文介绍了按字母顺序对Enum.values()进行排序,然后将它们添加到Spinner ArrayAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发可与Web服务一起使用的Android应用程序。

I'm developing an Android application that works with a Web service.

我想按字母顺序排序 Enum.values()的描述。

I want to sort alphabetically Enum.values() by its description.

我有这个 Enum

public enum Stature
{
    tall (0, MSDApplication.getAppContext().getString(R.string.stature_tall)),
    average(1, MSDApplication.getAppContext().getString(R.string.stature_average)),
    small(2, MSDApplication.getAppContext().getString(R.string.stature_small));

    private final int stature;
    private final String statureString;

    Stature(int anStature, String anStatureString) { stature = anStature; statureString = anStatureString; }

    public int getValue() { return stature; }

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

也许,这不是使用<$ c $的最佳选择c>枚举,但是我在Java开发方面是一个新手。

Maybe, this is not the best choice to use an Enum, but I'm very new on Java development.

您知道如何使用其字母顺序对其进行排序 statureString

Do you know how can I sort it alphabetically using its statureString?

我想这样做是因为我想使用这些 ArrayAdapter 上的> Enum.values():

I want to do this because I want to use these Enum.values() on an ArrayAdapter:

mSpinStature.setAdapter(new ArrayAdapter<Stature>(mActivity, android.R.layout.simple_dropdown_item_1line, Stature.values()));

我想选择 Stature 此处的项目:

private OnItemSelectedListener listener = new OnItemSelectedListener()
{

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long aid)
    {
        if (parent.equals(mSpinStature))
        {
            Stature item = (Stature)parent.getAdapter().getItem(pos);
            Log.v("TAG", item.toString() + item.getValue());
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0)
    {
    }

};


推荐答案

如何向枚举添加方法

enum Stature {
    tall, average, small;

    public static Stature[] getSortedVaules() {
        Stature[] statures = values();
        Arrays.sort(statures, EnumByNameComparator.INSTANCE);
        return statures;
    }

    private static class EnumByNameComparator implements Comparator<Enum<?>> {

        public static final Comparator<Enum<?>> INSTANCE = new EnumByNameComparator();

        public int compare(Enum<?> enum1, Enum<?> enum2) {
            return enum1.name().compareTo(enum2.name());
        }

    }

}

public class StatureTest {

    @Test
    public void getSortedValues() {
        Stature[] normalValues = Stature.values();
        Assert.assertEquals(Stature.tall, normalValues[0]);
        Assert.assertEquals(Stature.average, normalValues[1]);
        Assert.assertEquals(Stature.small, normalValues[2]);

        Stature[] sortedVaules = Stature.getSortedVaules();
        Assert.assertEquals(Stature.average, sortedVaules[0]);
        Assert.assertEquals(Stature.small, sortedVaules[1]);
        Assert.assertEquals(Stature.tall, sortedVaules[2]);
    }
}

这篇关于按字母顺序对Enum.values()进行排序,然后将它们添加到Spinner ArrayAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 02:54