2017-02-16 26 views
1

firebaseデータベースにオブジェクトのArrayListをアップロードします。ArrayListをダウンロードしてArrayListの値を入力すると、「HashMapはjava.util.ArrayListにキャストできません」というメッセージが表示されます。しかし、私はArrayListをアップロードし、ArrayListをダウンロードし、ArrayListという値に入れます。なぜ、エラーがHashMapと言うのですか?HashMapをArrayListにキャストできません

public class TileContentFragment extends Fragment { 

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

    private FirebaseAuth mFirebaseAuth; 
    private FirebaseUser mFirebaseUser; 
    private DatabaseReference mDatabase; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 

     // Initialize Firebase Auth and Database Reference 
     mFirebaseAuth = FirebaseAuth.getInstance(); 
     mFirebaseUser = mFirebaseAuth.getCurrentUser(); 
     mDatabase = FirebaseDatabase.getInstance().getReference(); 

     RecyclerView recyclerView = (RecyclerView) inflater.inflate(R.layout.recycler_view, container, false); 
     ContentAdapter adapter = new ContentAdapter(recyclerView.getContext(),mItems); 
     recyclerView.setAdapter(adapter); 
     recyclerView.setHasFixedSize(true); 
     // Set padding for Tiles 
     int tilePadding = getResources().getDimensionPixelSize(R.dimen.tile_padding); 
     recyclerView.setPadding(tilePadding, tilePadding, tilePadding, tilePadding); 
     recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2)); 
     return recyclerView; 
    } 
    @Override 
    public void onStart() { 
     super.onStart(); 

     mDatabase.addListenerForSingleValueEvent(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot snapshot) { 
       if (snapshot.hasChild("ItemsArray")) { 

        final ValueEventListener userListener = new ValueEventListener() { 
         @Override 
         public void onDataChange(DataSnapshot dataSnapshot) { 
          mItems = new ArrayList<>((ArrayList) dataSnapshot.getValue()); 

         } 

         @Override 
         public void onCancelled(DatabaseError databaseError) { 
          Log.w("AppInfo", "onCancelled: ",databaseError.toException()); 

         } 
        }; 
        mDatabase.child("ItemsArray").addValueEventListener(userListener); 
       } 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 
    } 

    public static class ViewHolder extends RecyclerView.ViewHolder { 

     public ImageView picture; 
     public TextView name; 

     public ViewHolder(LayoutInflater inflater, ViewGroup parent) { 
      super(inflater.inflate(R.layout.item_tile, parent, false)); 
      picture = (ImageView) itemView.findViewById(R.id.tile_picture); 
      name = (TextView) itemView.findViewById(R.id.tile_title); 
      itemView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Context context = v.getContext(); 
        Intent intent = new Intent(context, DetailActivity.class); 
        intent.putExtra(DetailActivity.EXTRA_POSITION, getAdapterPosition()); 
        context.startActivity(intent); 
       } 
      }); 
     } 
    } 

    /** 
    * Adapter to display recycler view. 
    */ 
    public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> { 
     // Set numbers of Tiles in RecyclerView. 
     private static final int LENGTH = 18; 

     private final String[] mPlaces; 
     private ArrayList<Item> mPlacePictures; 

     public ContentAdapter(Context context,ArrayList<Item> mPlacePictures) { 
      Resources resources = context.getResources(); 
      mPlaces = resources.getStringArray(R.array.places); 
      this.mPlacePictures=mPlacePictures; 
     } 

     @Override 
     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      return new ViewHolder(LayoutInflater.from(parent.getContext()), parent); 
     } 

     @Override 
     public void onBindViewHolder(ViewHolder holder, int position) { 
      Picasso.with(holder.picture.getContext()).load(mPlacePictures.get(position).getProfilePic()).into(holder.picture); 
      // holder.picture.setImageDrawable(mPlacePictures[position % mPlacePictures.length]); 
      holder.name.setText(mPlaces[position % mPlaces.length]); 
     } 

     @Override 
     public int getItemCount() { 
      return mPlacePictures.size(); 
     } 
    } 
} 

public class Item { 

    private String user; 
    private String name; 
    private String desription; 
    private int price; 
    private ArrayList<String> url; 
    private String profilePic; 

    public Item(){} 
    public Item(String user,String name, String desription, int price, ArrayList<String> url){ 
     this.user=user; 
     this.name=name; 
     this.desription=desription; 
     this.price=price; 
     this.url=url; 
    } 


    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getDesription() { 
     return desription; 
    } 

    public void setDesription(String desription) { 
     this.desription = desription; 
    } 

    public int getPrice() { 
     return price; 
    } 

    public void setPrice(int price) { 
     this.price = price; 
    } 

    public ArrayList<String> getUrl() { 

     return url; 
    } 

    public void setUrl(ArrayList<String> url) { 

     this.url = url; 
    } 

    public String getUser() { 
     return user; 
    } 

    public void setUser(String user) { 
     this.user = user; 
    } 

    public String getProfilePic() { 
     return profilePic; 
    } 

    public void setProfilePic(String profilePic) { 
     this.profilePic = profilePic; 
    } 
} 

E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: com.packtpub.materialdesign4, PID: 713 
       java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map 
        at com.packtpub.materialdesign4.TileContentFragment$1$1.onDataChange(TileContentFragment.java:82) 
        at com.google.android.gms.internal.zzakg.zza(Unknown Source) 
        at com.google.android.gms.internal.zzalg.zzcxk(Unknown Source) 
        at com.google.android.gms.internal.zzalj$1.run(Unknown Source) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5527) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
DynamiteModule: Local module descriptor class for com.google.firebase.auth not found. 
+0

を修正する必要があり、あなたの 'Item'クラスを投稿してもらえますか? –

+0

私はアイテムクラスを入れました。Heyelp – bojan

答えて

0

はここDataSnapshotのドキュメントを見てください:https://www.firebase.com/docs/java-api/javadoc/com/firebase/client/DataSnapshot.html#getValue--

のgetValue()ネイティブとしてこのスナップショットに含まれるデータを返します。タイプ。返される可能性タイプは次のとおりです。

  • Boolean
  • Long
  • Double
  • Map <String, Object>
  • List<Object>

012を返しているdataSnapshot.getValue()方法は、あなたが期待していたList<Object>ではなく最初Mapへのキャスト

、その結果からArrayListを作成すると、あなたの問題

mItems = new ArrayList(((Map<String, Item>) dataSnapshot.getValue()).values()); 
+0

今、私はエラーメッセージ "java.util.ArrayListはjava.util.Mapにキャストできません" – bojan

+0

あなたのスタックトレースを投稿できますか? – LCartwright

+0

heres my trace ... – bojan

関連する問題