2016-07-13 11 views
0

これは私のコードです。notifyDataSetChanged();アンドロイドでは動作しません

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

    View v = inflater.inflate(R.layout.recycler_list, container, false); 
    recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view); 

    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 3); 

    adapter = new FragmentBrandList.MyAdapter(Utility.getBrandMap(), getActivity()); 
    recyclerView.setAdapter(adapter); 

    // asynchronous call 
    query.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) { 
       Brand brand = dataSnapshot1.getValue(Brand.class); 
       brandMap.put(brand.getId(), brand); 
      } 

      Utility.setBrandMap(brandMap); 
      adapter.notifyDataSetChanged(); 
     } 

     @Override 
     public void onCancelled(DatabaseError error) { 
      // Failed to read value 
      Log.w(TAG, "Failed to read value.", error.toException()); 
      Utility.displayToast("Failed to read value." + error.toException()); 
     } 
    }); 
    return v; 
} 

最初はUtility.getBrandMap()がnullです。それで注目は表示されています。非同期呼び出しでは、値を取得して更新していて、adapter.notifyDataSetChanged()を呼び出しています。それでもUIは更新されません。

ここで何が間違っていますか?

編集:アダプターコード

private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { 

    private List<Brand> brandList = new ArrayList<>(); 
    private Context context; 

    public class MyViewHolder extends RecyclerView.ViewHolder { 
     ImageView imageView; 

     public MyViewHolder(View view) { 
      super(view); 
      imageView = (ImageView) view.findViewById(R.id.thumbnail); 
     } 
    } 

    public MyAdapter(Map<String, Brand> brands, Context context) { 
    //public MyAdapter(List<Brand> brands, Context context) { 
     this.brandList = new ArrayList<>(brandMap.values()); 
     //this.brandList = brands; 
     this.context = context; 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.card_brand, parent, false); 

     return new MyViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 
     if (brandList != null && brandList.size() > 0) { 
      Brand brand = brandList.get(position); 
      Glide.with(context).load(String.valueOf(brand.getImage())) 
        .error(R.drawable.placeholder) 
        .placeholder(R.drawable.placeholder) 
        .into(holder.imageView); 
     } 
    } 

    @Override 
    public int getItemCount() { 
     return brandList.size(); 
    } 
} 
+0

アダプタコード全体を送信してください。特にアダプター(brandMap)を埋めるべき領域を見ることができる部分。 – reVerse

+0

@reVerseアダプタコードを追加しました。 –

答えて

2

この試してみてください:あなたは、あなたのMyAdapterのコンストラクタに空Mapを渡している

private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { 

    private List<Brand> brandList = new ArrayList<>(); 
    private Context context; 

    public class MyViewHolder extends RecyclerView.ViewHolder { 
     ImageView imageView; 

     public MyViewHolder(View view) { 
      super(view); 
      imageView = (ImageView) view.findViewById(R.id.thumbnail); 
     } 
    } 

    public MyAdapter(Map<String, Brand> brands, Context context) { 
    //public MyAdapter(List<Brand> brands, Context context) { 
     this.brandList = new ArrayList<>(brandMap.values()); 
     //this.brandList = brands; 
     this.context = context; 
    } 

    public void setBrandMap(Map<String, Brand> brandMap){ 
     this.brandList = new ArrayList<>(brandMap.values()); 
     notifyDataSetChanged(); 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.card_brand, parent, false); 

     return new MyViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 
     if (brandList != null && brandList.size() > 0) { 
      Brand brand = brandList.get(position); 
      Glide.with(context).load(String.valueOf(brand.getImage())) 
        .error(R.drawable.placeholder) 
        .placeholder(R.drawable.placeholder) 
        .into(holder.imageView); 
     } 
    } 

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

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

    View v = inflater.inflate(R.layout.recycler_list, container, false); 
    recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view); 

    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 3); 

    adapter = new FragmentBrandList.MyAdapter(Utility.getBrandMap(), getActivity()); 
    recyclerView.setAdapter(adapter); 

    // asynchronous call 
    query.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) { 
       Brand brand = dataSnapshot1.getValue(Brand.class); 
       brandMap.put(brand.getId(), brand); 
      } 

      adapter.setBrandMap(brandMap); 

     } 

     @Override 
     public void onCancelled(DatabaseError error) { 
      // Failed to read value 
      Log.w(TAG, "Failed to read value.", error.toException()); 
      Utility.displayToast("Failed to read value." + error.toException()); 
     } 
    }); 
    return v; 
} 

とアダプタコードでの。非同期クエリが完了すると、マップは埋められますが、アダプタ内のbrandListは新しいデータについて何も知らないためです。だからあなたがしたいのは、アダプタ内のListに新しいアイテムを追加することです。

public void addValues(Map<String, Brand> brands){ 
    brandList.clear(); 
    brandList.addAll(brands.values()); 
    notifyDataSetChanged(); 
} 

を非同期クエリが完了したら、それを呼び出す:

は、アダプタに次のメソッドを追加します。

関連する問題