私は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[]
これが失敗する運命にある場合、他にどのようにアイテムの属性にアクセスできますか? 項目は ArrayList にあるため、ListView にデータを入力して Android にすることができます。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;
}
}