How to use notifydatasetchanged in recyclerview gridlayout
<p>I am new to android and trying to update the <code>gridlayout</code> in popular movies popular movies app of udacity nanodegree project. </p>

<p>I am downloading the data using <code>AsyncTask</code> and parsing the data, updating the <code>recyclerview</code> and setting adapter in <code>onPostExecute</code>.</p>

<p><strong>This is my AsyncTask class</strong></p>

<pre><code>private class getJson extends AsyncTask&lt;String, Void, String&gt;{

    @Override
    protected String doInBackground(String... strings) {
        String result = "";
        URL url;
        HttpURLConnection httpURLConnection = null;


        try {
            url = new URL(strings[0]);

            httpURLConnection = (HttpURLConnection) url.openConnection();

            InputStream in = httpURLConnection.getInputStream();

            InputStreamReader reader = new InputStreamReader(in);

            int data = reader.read();

            while ( data != -1 ){
                  char current  = (char) data;

                  result += current;

                  data = reader.read();
            }

        } catch (java.io.IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        jsonString = s;

        Log.i("result", jsonString);

        JSONObject jsonObject = null;

        if(!list.isEmpty())
          list.clear();
        try {

            jsonObject = new JSONObject(jsonString);

            JSONArray jsonArray = jsonObject.getJSONArray("results");

            for( int i = 0; i &lt; jsonArray.length() &amp;&amp; i &lt; 10; i++ ){

                JSONObject currObject = jsonArray.getJSONObject(i);

                String posterurl = preUrl + currObject.getString("backdrop_path");

                Log.i("poster", posterurl);

                String url =  preUrl + currObject.getString("poster_path");

                String overview = currObject.getString("overview");

                String title = currObject.getString("original_title");

                String release_date = (currObject.getString("release_date"));

                String votes = (currObject.getString("vote_average"));

                list.add(new MovieDetails(url,overview, title, release_date, votes, posterurl));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }



    }
}
</code></pre>

<p><strong>This is my adapter class</strong> </p>

<pre><code>    public class MainRecyclerViewAdapter extends RecyclerView.Adapter&lt;MainRecyclerViewAdapter.ViewHolder&gt; {

    ArrayList&lt;MovieDetails&gt; data;
    Context context;

    public MainRecyclerViewAdapter(Context context, ArrayList&lt;MovieDetails&gt; data ){
         this.data = data;
         this.context = context;
    }
    @Override
    public MainRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

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

        MainRecyclerViewAdapter.ViewHolder viewHolder = new MainRecyclerViewAdapter.ViewHolder(v);

        return viewHolder;
    }

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

        Picasso.with(context).load(data.get(position).getImageUrl()).into(holder.movie_thumbnail);
        //holder.movie_thumbnail.setImageResource(R.mipmap.ic_launcher);


    }

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

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        ImageView movie_thumbnail;

        private Context context;

        @Override
        public void onClick(View view) {
            Toast.makeText(context,data.get(getPosition()).getTitle(),Toast.LENGTH_LONG).show();
            Intent intent = new Intent(context, DetailsActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("list", (Serializable) data);
            intent.putExtras(bundle);
            intent.putExtra("index", getAdapterPosition());

            context.startActivity(intent);
        }

        public ViewHolder(View itemView) {
            super(itemView);
            context = itemView.getContext();
            itemView.setOnClickListener(this);
            movie_thumbnail = (ImageView) itemView.findViewById(R.id.movies_item_thumbnail);
        }
    }
}
</code></pre>

<p>This is how I am handling <em>sorting</em> based on voting or popularity </p>

<pre><code>@Override
public boolean onOptionsItemSelected(MenuItem item) {

    getJson gJson = new getJson();
    switch (item.getItemId()){
        case R.id.rating :
            gJson.execute(top_rated);
            return true;
        case R.id.popularity : gJson.execute(popular);
            return true;
        default: return super.onOptionsItemSelected(item);

    }

}
</code></pre>

<p>onCreate </p>

<pre><code>protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar myToolbar = (Toolbar) findViewById(R.id.main_toolbar);
    setSupportActionBar(myToolbar);

    getJson getJson = new getJson();
    getJson.execute(popular);

    recyclerView = (RecyclerView)findViewById(R.id.main_recyclerview);

    recyclerView.setHasFixedSize(true);
    layoutManager = new GridLayoutManager(MainActivity.this,2);
    recyclerView.setLayoutManager(layoutManager);

    adapter = new MainRecyclerViewAdapter(MainActivity.this, list);
    recyclerView.setAdapter(adapter);

}
</code></pre>

<p>I figured out to use <code>notifydatasetchanged()</code>, but I am not able to figure <em>where</em> to use that.</p>

<p>Also please tell me <em>better</em> way to handle the sorting.</p>

<p>This is what I did :</p>

<ol>
<li>I moved the instantiating of adapter and setting of adapter of to onCreate</li>
<li><p>calling <code>notfiydatasetchanged()</code> in <code>onPostexeute()</code></p></li>
<li><p>Everytime I call the <code>AsyncTask</code>, my arraylist is being populated with new elements and so the adapter is filling all images. I am <strong><code>list.clear()</code></strong> before parsing the json in <code>onPostexecute()</code></p></li>
</ol>