yoyani
yoyani

Reputation: 85

Return List in asynctask Android

I Want to return my list jours using asynctask so it can add values in my list

See my Fragment :

ProgressDialog progressDialog = new ProgressDialog(getActivity());
        RecyclerView rv = (RecyclerView) root.findViewById(R.id.recyclerView);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        jours = new ArrayList<>();
        MaTask task = new MaTask(button,progressDialog,jours);
        task.execute();
        adapter = new MyAdapter(jours);
        rv.setAdapter(adapter);

And My Asynctask :

public class MaTask extends AsyncTask<Void, Void, List<Cours>> {
    ProgressDialog dialog ;
    Button button;
    List<Cours> jours ;
    public MaTask(Button b,ProgressDialog progressBar,List<Cours> laliste)
    {

        dialog=progressBar;
        button=b;
        jours=laliste;

    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog.setMessage("Chargement en Cours");
        dialog.show();

    }
    @Override
    protected List<Cours> doInBackground(Void... params) {
        try
        {
            Document doc = Jsoup.connect("http://terry.gonguet.com/cal/?g=tp11").get();
            Elements days = doc.select("div.day");
            Elements event = doc.select("div.event");
            for(Element day : days)
            {

                String jour = day.getElementsByClass("dayDate").first().html();
                System.out.println(" : " + jour);
                for(Element ev : event)
                {

                    Element title = ev.select("div[class=title]").first();
                    Element salle = ev.select("div[class=location]").first();
                    Element wat = ev.select("div[class=whoat]").first();
                    Element starthour = ev.select("div.bub.right.top").first();
                    Element endhour = ev.select("div.bub.right.bottom").first();
                    //System.out.println(" Titre :" + title.text() + " Debut heure : " + starthour.text() + " heure fin : " + endhour.text());
                    Cours lecours = new Cours(starthour.text(),title.text());

                    jours.add(lecours);




                }

                Collections.sort(jours);


            }
        }
        catch (IOException ex)
        {

        }
        return jours;


    }




    @Override
    protected void onPostExecute(List<Cours> laliste) {

        if(dialog.isShowing())
        {
            dialog.dismiss();
        }
        for (Cours c : laliste)
        {
            System.out.println(c.toString());
        }


    }
}

But when i do that nothing change in my list.

Upvotes: 0

Views: 596

Answers (2)

mariosmant
mariosmant

Reputation: 454

Change MaTask constructor to this:

 RecyclerView rv;
 public MaTask(Button b,ProgressDialog progressBar,List<Cours> laliste, RecyclerView rv)
  {
      dialog=progressBar; 
      button=b; 
      jours=laliste;
      this.rv = rv;
  }

And onPostExecute to this:

 @Override 
 protected void onPostExecute(List<Cours> laliste)
  {
      if(dialog.isShowing()) 
      { 
           dialog.dismiss(); 
       } 
      for (Cours c : laliste)
      {
        System.out.println(c.toString()); 
      } 

     MyAdapter adapter = new MyAdapter(laliste);
     rv.setAdapter(adapter);
     rv.notifyDataSetChanged();
   }

Upvotes: 0

Titus
Titus

Reputation: 22474

You need to notify the RecyclerView that the underlining list has changed. You can do that by calling the notifyDataSetChanged() method.

Here is an example:

@Override
protected void onPostExecute(List<Cours> laliste) {
   ....
   rv.notifyDataSetChanged();
}

Upvotes: 2

Related Questions