notifyDataSetChanged();不在android工作

时间:2016-07-13 14:30:19

标签: android

这是我的代码。

@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();
    }
}

2 个答案:

答案 0 :(得分: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;
}

在适配器代码中:

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();
    }
}

答案 1 :(得分:2)

您将空Map传递给MyAdapter的构造函数。异步查询完成后,Map将被填充,但适配器中的brandList对新数据一无所知。所以你想要做的是将新项目添加到适配器内的List

将以下方法添加到适配器:

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

并在异步查询完成后调用它。