首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >notifyDataSetChanged on RecyclerView

notifyDataSetChanged on RecyclerView
EN

Stack Overflow用户
提问于 2017-06-09 13:22:07
回答 1查看 140关注 0票数 2

我有recyclerView和customAdaprer。我将一个对象(EarthquakeList)的列表传递给回收适配器,然后执行setAdapter:

代码语言:javascript
复制
    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);
}

我在AsyncTask方法上创建了onResume:

代码语言:javascript
复制
    @Override
protected void onResume() {
    super.onResume();
    // Kick off an {@link AsyncTask} to perform the network request
    new EarthQuakeAsyncTask().execute();
}

在AsyncTask我的类中,我从我的对象那里得到了一个新的列表,当我用旧的列表替换这个新的列表并调用notifyDataSetChanged时,recyclerView仍然没有显示什么?我调试我的应用程序,并从网络中获取对象。我在列表视图上这样做,但是recylerview看起来很出众。

我用新的单子代替旧的单子,一个像blow一样:

代码语言:javascript
复制
  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();

    }

我到底是怎么了?

*编辑*这是适配器:

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-09 14:17:39

我忘了把LayoutManager放在recyclerView上了。

这是正确的代码:

代码语言:javascript
复制
    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);

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44458971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档