notifyDataSetChanged()不会刷新我的recyclerview

时间:2018-05-24 19:02:45

标签: android android-recyclerview searchview notifydatasetchanged

我需要一些帮助,在尝试使用SearchView时遇到以下问题,我会将它们放在上下文中。 我有一个主要的活动,包含2个标签,每个标签都有各自的片段,在片段1中我必须包含,我想使用SearchView来搜索和更新可用的列表,从片段1我有一个RecyclerViewAdapter,它实现了可过滤的内容方法获得比较和搜索结果,问题是比较是10寄存器4的例子是匹配搜索因子但不更新我的适配器。

我的MyRecyclerViewAdapter:

  package com.rchile.piper.radioschile;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.Filter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.rchile.piper.radioschile.DataDB.DataBaseManager;
import com.rchile.piper.radioschile.MainActivity;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import android.widget.Filter;
import android.widget.Filterable;

import java.util.ArrayList;

class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> implements Filterable{

    Context context;
    public  ArrayList<MainActivity.Radios> getDataAdapter;
    private ArrayList<MainActivity.Radios>  items;
    private ArrayList<MainActivity.Radios> itemsFiltered;

    Intent detailIntent;

    private static DataBaseManager manager;
    private static Cursor cursor;
    CustomFilter filter;


  /*  public MyRecyclerViewAdapter(ArrayList<MainActivity.Radios> getDataAdapter, Context context, ArrayList<MainActivity.Radios> items) {

        super();

        this.getDataAdapter = getDataAdapter;
        this.itemsFiltered = getDataAdapter;
        this.context = context;
        this.items = items;

    }*/

    public MyRecyclerViewAdapter(Context ctx,ArrayList<MainActivity.Radios> players)
    {

        this.context=ctx;
        this.getDataAdapter=players;

    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_row, parent, false);

        ViewHolder viewHolder = new ViewHolder(v, context, getDataAdapter);

        return viewHolder;
    }
    public MyRecyclerViewAdapter(ArrayList<MainActivity.Radios> getDataAdapter) {

        this.getDataAdapter = getDataAdapter;
    }
   /* public MyRecyclerViewAdapter(Context context) {
        this.context = context;
    }*/

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {





        holder.label.setText(getDataAdapter.get(position).getNombre());
        holder.idRadio.setText(getDataAdapter.get(position).getIdRadio());
        holder.sintonia.setText(getDataAdapter.get(position).getSintonia());
        String urlIcono = getDataAdapter.get(position).getUrlIcono();
        String esFavorito = getDataAdapter.get(position).getEsFavorito();
        Context contextt = holder.icono.getContext(); //<----- Add this line
        Context contextt2 = holder.imageButton.getContext();

        Picasso.with(contextt).load(urlIcono).into(holder.icono);


        if(esFavorito.equals("0")){
            Picasso.with(contextt2).load(R.drawable.es_favorito).into(holder.imageButton);
        }else{
            Picasso.with(contextt2).load(R.drawable.no_favorito).into(holder.imageButton);
        }


        holder.imageButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                fav(position,v);

            }
        });



    }

    @Override
    public int getItemCount() {
        return getDataAdapter.size();
    }
    @Override
    public Filter getFilter() {
        if(filter==null)
        {
            filter=new CustomFilter( getDataAdapter,this);
        }

        return filter;
    }





    class ViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener {

        TextView label,idRadio,sintonia;
        TextView dateTime;
        RelativeLayout relativeLayout;
        ImageView icono;
        ImageButton imageButton;

        ImageButton fav;

        Context context;
        ArrayList<MainActivity.Radios> getDataAdapter;

        public ViewHolder(View itemView, Context context, ArrayList<MainActivity.Radios> getDataAdapter) {

            super(itemView);
            itemView.setOnClickListener(this);
            this.getDataAdapter = getDataAdapter;
            this.context = context;


            label = (TextView) itemView.findViewById(R.id.txt_nombre);
            idRadio = (TextView) itemView.findViewById(R.id.txt_id);
            sintonia = (TextView) itemView.findViewById(R.id.txt_sintonia);
            icono = (ImageView) itemView.findViewById(R.id.imgIcono);
            imageButton = (ImageButton) itemView.findViewById(R.id.buttonInfo);

        }


        @Override
        public void onClick(View v) {

            int position = getAdapterPosition();
            MainActivity.Radios getDataAdapter = this.getDataAdapter.get(position);



            detailIntent = new Intent(v.getContext(), Reproducir.class);
            detailIntent.putExtra("numero",  getDataAdapter.getIdRadio());
            v.getContext().startActivity(detailIntent);

        }
    }



    public void fav(final int position,View v) {
        ImageButton imageButton2;
        imageButton2 = (ImageButton) v.findViewById(R.id.buttonInfo);
        MainActivity.Radios getDataAdapter = this.getDataAdapter.get(position);
        manager = new DataBaseManager(v.getContext());

        cursor = manager.buaca1(getDataAdapter.getIdRadio());

        String esFavorito = "";
        if (cursor.getCount() == 1){
        if (cursor.moveToFirst()) {
            do {
                esFavorito = cursor.getString(4);
            } while (cursor.moveToNext());
        }

            if(esFavorito.equals("0")){
                esFavorito = "1";
                manager.updateFavorito(getDataAdapter.getIdRadio(),esFavorito);
                Picasso.with(v.getContext()).load(R.drawable.no_favorito).into(imageButton2);
            }else{
                esFavorito = "0";
                manager.updateFavorito(getDataAdapter.getIdRadio(),esFavorito);
                Picasso.with(v.getContext()).load(R.drawable.es_favorito).into(imageButton2);
            }

        }

    }
}

我的CustomFilter

package com.rchile.piper.radioschile;

/**
 * Created by piper on 09/05/2018.
 */

import android.widget.Filter;
import java.util.ArrayList;

public class CustomFilter extends Filter{

    MyRecyclerViewAdapter adapter;
    public  ArrayList<MainActivity.Radios> filterList;


    private ArrayList<MainActivity.Radios> mDataset;


    public CustomFilter(ArrayList<MainActivity.Radios> mDataset,MyRecyclerViewAdapter adapter)
    {
        this.adapter=adapter;
        this.mDataset=mDataset;
        this.filterList = new ArrayList<>();

    }

    //FILTERING OCURS
    @Override
    protected FilterResults performFiltering(CharSequence charSequence) {
        FilterResults results=new FilterResults();

        //CHECK CONSTRAINT VALIDITY
        if(charSequence != null && charSequence.length() > 0)
        {
            //CHANGE TO UPPER
            charSequence=charSequence.toString().toUpperCase();
            //STORE OUR FILTERED PLAYERS
            ArrayList<MainActivity.Radios> filteredPlayers=new ArrayList<>();

            for (int i=0;i<mDataset.size();i++)
            {
                //CHECK
                if(mDataset.get(i).getNombre().toUpperCase().contains(charSequence))
                {
                    //ADD PLAYER TO FILTERED PLAYERS
                    filteredPlayers.add(mDataset.get(i));
                }
            }

            results.count=filteredPlayers.size();
            results.values=filteredPlayers;
        }else
        {
            results.count=mDataset.size();
            results.values=mDataset;

        }

        return results;

    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {

        adapter.getDataAdapter.clear();
        adapter.getDataAdapter.addAll((ArrayList<MainActivity.Radios>) results.values);
        adapter.notifyDataSetChanged();
    }
}

1 个答案:

答案 0 :(得分:0)

adapter.notifyDataSetChanged();之后您遗失了adapter.getDataAdapter.clear();,这也是数据发生变化后所需要的。添加如下:

adapter.getDataAdapter.clear();
adapter.notifyDataSetChanged();
adapter.getDataAdapter.addAll((ArrayList<MainActivity.Radios>) results.values);
adapter.notifyDataSetChanged();