2
我有一个recyclerView和customAdaprer。我传递一个对象(earthquakeList)的列表recycleradapter然后我就setAdapter:notifyDataSetChanged上RecyclerView
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
earthquakeList = new ArrayList<>();
adapter = new RecyclerViewAdapter(earthquakeList);
recyclerView.setAdapter(adapter);
}
我上的onResume方法创建的AsyncTask:
@Override
protected void onResume() {
super.onResume();
// Kick off an {@link AsyncTask} to perform the network request
new EarthQuakeAsyncTask().execute();
}
中的AsyncTask
我的课我从一个新的列表我反对说,我从互联网上获得,当我更换新表与旧名单,并呼吁notifyDataSetChanged,recyclerView仍然没有显示? 我调试我的应用程序,并从网上获取对象。 我做这样的列表视图但recylerview似乎传出。
我更换新的列表中选择一个喜欢的打击旧列表:
private class EarthQuakeAsyncTask extends AsyncTask<Void, Void, List<Earthquake>> {
@Override
protected List<Earthquake> doInBackground(Void... urls) {
// Create URL object
URL url = HttpRequest.createUrl(USGS_REQUEST_URL);
// perform HTTP request to the URL and receive a JSON response back
String jsonResponse = "";
try {
jsonResponse = HttpRequest.makeHttpRequest(url);
} catch (IOException e) {
e.printStackTrace();
}
List<Earthquake> earthquakes = HttpRequest.extractFeaturesFromJson(jsonResponse);
return earthquakes;
}
@Override
protected void onPostExecute(List<Earthquake> earthquakeList) {
super.onPostExecute(earthquakeList);
MainActivity.this.earthquakeList.clear();
MainActivity.this.earthquakeList.addAll(earthquakeList);
adapter.notifyDataSetChanged();
}
究竟是什么我错了吗?
************************编辑******************** 这是适配器:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyHolder> {
List<Earthquake> earthquakes;
public RecyclerViewAdapter(List<Earthquake> earthquakes) {
this.earthquakes = earthquakes;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.earthquak_list_item, parent, false);
MyHolder myHolder = new MyHolder(view);
return myHolder;
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
Earthquake earthquake = earthquakes.get(position);
holder.magnitude.setText(earthquake.getmMagnitude());
holder.location.setText(earthquake.getmLocation());
holder.time.setText(earthquake.getmDate());
}
@Override
public int getItemCount() {
return (null != earthquakes ? earthquakes.size() : 0);
}
public void setItems(List<Earthquake> earthquakeList) {
this.earthquakes = earthquakeList;
}
public class MyHolder extends RecyclerView.ViewHolder {
@BindView(R.id.magnitude)
TextView magnitude;
@BindView(R.id.location)
TextView location;
@BindView(R.id.date)
TextView time;
public MyHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}