2017-06-09 41 views
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); 
    } 
} 

回答

0

行動,我忘了把佈局管理爲recyclerView。

這是正確的代碼:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ButterKnife.bind(this); 
    earthquakeList = new ArrayList<>(); 
    layoutManager = new LinearLayoutManager(this); 
    recyclerView.setLayoutManager(layoutManager); 
    adapter = new RecyclerViewAdapter(earthquakeList); 
    recyclerView.setAdapter(adapter); 

} 
相關問題