将对象[]转换为自定义项[]

时间:2013-08-11 15:53:18

标签: android listview casting arraylist

我有我的ArrayList

ArrayList<Item> itemList = new ArrayList<Item>();

然后填充项目。 稍后,我想在循环中访问items属性以获取数据,但

Item[] itemArr = (Item[]) itemList.toArray();

这给了我错误:

 java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.raynemartin.SAAndroidAppCompetition.Item[]

如果这注定要失败,我还能如何访问Items的属性? Items位于ArrayList中,因此可以填充和安装ListView。现在我想对ArrayList进行排序,但是需要访问我的Item类的.getRating()方法。

Item[] itemArr= (Item[])itemList.toArray();

    for(int i =0;i<itemArr.length;i++){
            for(int j=i;i<itemArr.length;j++){
                if(itemArr[j].getRating()>itemArr[i].getRating()){
                        Item temp = itemArr[j];
                        itemArr[j] = itemArr[i];
                        itemArr[i] = temp;
                }
            }
        }
    }

编辑 - 这是项目类代码

public class Item {

    private String title;
    private String category;
    private String downloads;
    private double rating;
    private Bitmap icon;

    public Item(String title, String category, String downloads,double rating,Bitmap icon) {
        this.title = title;
        this.category = category;
        this.downloads = downloads;
        this.icon = icon;
        this.rating = rating;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getDownloads() {
        return downloads;
    }

    public void setDownloads(String downloads) {
        this.downloads = downloads;
    }

    public Bitmap getIcon() {
        return icon;
    }

    public void setIcon(Bitmap icon) {
        this.icon = icon;
    }

    public double getRating() {
        return rating;
    }

    public void setRating(double rating) {
        this.rating = rating;
    }
}

3 个答案:

答案 0 :(得分:1)

让班级实现接口Comparable并使用

Collections.sort(itemList);

对您的收藏品进行排序。因此,您不必构建数组并自行排序。

答案 1 :(得分:1)

如果你真的需要以数组形式访问元素,那么最好使用

itemList.toArray(new Item[0]);

itemList.toArray(new Item[itemList.size()]);

因为这会给你一个Item类型的数组而不是一个object数组。关于这个问题还有很多关于网络的讨论。

答案 2 :(得分:1)

要排序,您可以尝试下面的内容。

在你的Item类中实现toString

public String toString()
{
    return String.valueOf(rating);
}

实施比较器

 class SortingRating implements Comparator<Item>
{

        @Override
        public int compare(Item arg0, Item arg1) {
            if (arg0.getRating() < arg1.getRating()) return -1;
            if (arg0.getRating() > arg1.getRating()) return 1;
            return 0;
    }

}

然后对列表进行排序

   Collections.sort(itemList, new SortingRating());

示例:如果我错了,请纠正我。

public class MainClass {
    static ArrayList<Item> aa = new ArrayList<Item>();
    public static void main(String[] args) {

        Item i1 = new Item("soap","sales","yes",4.0);
        aa.add(i1);
        Item i2 = new Item("soap","sales","yes",5.2);
        aa.add(i2);
        Item i3 = new Item("soap","sales","yes",3.8);
        aa.add(i3);
        Item i4 = new Item("soap","sales","yes",5.1);
        aa.add(i4);

         Collections.sort(aa, new SortingRating());
         for(int i=0;i<aa.size();i++)
         {
         System.out.println("Title  : " +aa.get(i).getTitle()+"--"+"Category : "+aa.get(i).getCategory()+"--"+"Downlaods : "+aa.get(i).getDownloads()+"--"+"Rating : "+aa.get(i).getRating());
         }

    }
}
class SortingRating implements Comparator<Item>
{

        @Override
        public int compare(Item arg0, Item arg1) {
            if (arg0.getRating() < arg1.getRating()) return -1;
            if (arg0.getRating() > arg1.getRating()) return 1;
            return 0;
        }   
}

项目类。省略了位图图标

public class Item  {

    private String title;
    private String category;
    private String downloads;
    private double rating;


    public Item(String title, String category, String downloads,double rating) {
        this.title = title;
        this.category = category;
        this.downloads = downloads;
        this.rating = rating;
    }
    public String toString()
    {
        return String.valueOf(rating);
    }
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getDownloads() {
        return downloads;
    }

    public void setDownloads(String downloads) {
        this.downloads = downloads;
    }

    public double getRating() {
        return rating;
    }

    public void setRating(double rating) {
        this.rating = rating;
    }
}

输出

Title  : soap--Category : sales--Downlaods : yes--Rating : 3.8
Title  : soap--Category : sales--Downlaods : yes--Rating : 4.0
Title  : soap--Category : sales--Downlaods : yes--Rating : 5.1
Title  : soap--Category : sales--Downlaods : yes--Rating : 5.2