the notifyDataSetChanged does not work with AsyncTask

1

I'm trying to load a list but every time I try to update the view of the recyclerview, I do not load the data, just load the first record, here I leave the code:

public class LoadInfo extends AsyncTask<Void, People, Void> {

    private RecyclerView recyclerView;
    private ProgressBar progressBar;
    private Context context;
    private PeopleAdapter peopleAdapter;

    public LoadInfo(RecyclerView recyclerView,ProgressBar progressBar, Context context){
        this.recyclerView = recyclerView;
        this.context = context;
        this.progressBar = progressBar;
    }

    @Override
    protected void onPreExecute() {
        peopleAdapter = new PeopleAdapter();
        recyclerView.setAdapter(peopleAdapter);
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected Void doInBackground(Void... voids) {

        InputStream flujo = null;

        try
        {
            flujo = context.getResources().openRawResource(R.raw.nombres);
            BufferedReader lector = new BufferedReader(new InputStreamReader(flujo));

            String texto = lector.readLine();
            while(texto != null)
            {
                publishProgress(new People(texto));

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                texto = lector.readLine();
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                if (flujo != null)
                {
                    flujo.close();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(People... values) {
        peopleAdapter.updateList(values[0]);
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        progressBar.setVisibility(View.GONE);
    }

}

public class PeopleAdapter extends RecyclerView.Adapter<PeopleViewHolder> {

    private ArrayList<People> peopleList;

    public PeopleAdapter() {
        this.peopleList = new ArrayList<>();
    }

    @Override
    public PeopleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_list, parent, false);
        return new PeopleViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(PeopleViewHolder holder, int position) {
        holder.getName().setText(peopleList.get(position).getName());
        holder.getCount().setText(String.valueOf(peopleList.get(position).getCount()));
    }

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

    public void updateList(People person){
        peopleList.add(person);
        notifyDataSetChanged();
    }
}

Thanks in advance!

    
asked by Isaac Alvarez Gil 11.09.2017 в 12:20
source

2 answers

0

Try the following in your updateList() method:

public void updateList(People person){
    peopleList.add(person);

    ArrayList<People> updatePeopleList = peopleList;
    peopleList.clear();
    peopleList.addAll(updatePeopleList)

    notifyDataSetChanged();
}
    
answered by 11.09.2017 в 14:56
0

It is not common to update the data in the Adapter using onProgressUpdate() , but you can do it this way, after updating the data call notifyDataSetChanged() :

@Override
protected void onProgressUpdate(People... values) {
    peopleAdapter.updateList(values[0]);
    // Actualiza!
    peopleAdapter.notifyDataSetChanged();
}

Another option is to finish the execution of your AsyncTask ( onPostExecute() ), you can directly update the Adapter data:

@Override
protected void onPostExecute(Void aVoid) {
    progressBar.setVisibility(View.GONE);
    //Actualiza!  
     peopleAdapter.notifyDataSetChanged();
}
    
answered by 11.09.2017 в 18:00