I am new to android and trying to update the application gridlayoutin the popular films of the popular film project nodacity nanodegree.
I download data using AsyncTaskand analyzing data, updating recyclerviewand installation adapter in onPostExecute.
This is my AsyncTask class
private class getJson extends AsyncTask<String, Void, String>{
@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 < jsonArray.length() && i < 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();
}
}
}
This is my adapter class.
public class MainRecyclerViewAdapter extends RecyclerView.Adapter<MainRecyclerViewAdapter.ViewHolder> {
ArrayList<MovieDetails> data;
Context context;
public MainRecyclerViewAdapter(Context context, ArrayList<MovieDetails> 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);
}
@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);
}
}
}
This is how I handle sorting based on vote or popularity
@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);
}
}
onCreate
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);
}
I decided to use it notifydatasetchanged(), but I cannot figure out where to use it.
Also, please tell me how best to handle sorting.
This is what I did:
- I moved the adapter instance and adapter settings to onCreate
call notfiydatasetchanged()inonPostexeute()
, AsyncTask, arraylist , . list.clear() json onPostexecute()