我是 Android 新手,正在尝试更新 Udacity 纳米学位项目中的热门电影应用程序中的 gridlayout
。
我正在使用 AsyncTask
下载数据并解析数据,在 onPostExecute
中更新 recyclerview
并设置适配器。
这是我的 AsyncTask 类
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();
}
}
}
这是我的适配器类
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);
//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);
}
}
}
这是我处理基于投票或热度的排序的方式。
@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);
}
我已经想出了使用notifydatasetchanged()
的方法,但是我不知道在哪里使用它。
请告诉我更好的处理排序的方法。
这是我做的:
- 我将适配器的实例化和设置移动到了onCreate中
在
onPostExecute()
中调用notfiydatasetchanged()
每次调用
AsyncTask
时,我的arraylist都会被新元素填充,因此适配器会填充所有图像。我在onPostExecute()
中解析json之前list.clear()
adapter.notifyDataSetChanged();
,它在onPostExecute
中。请问你能让我理解一下吗? - X09