【问题标题】:No adapter attached; skipping layout - beginner issue未连接适配器;跳过布局 - 初学者问题
【发布时间】:2018-05-17 08:33:48
【问题描述】:

我使用 volley 从服务器下载 JSON 数据,在读取和解析数据的函数中设置适配器。但是,无法识别适配器。问题是,我已经用相同的方法成功地在另一个活动中实现了适配器而没有错误。以下是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    if (extras!=null) {
        user_id = getIntent().getStringExtra("user_id");
    } else {
        Intent logIn = new Intent(JoinedActivity.this, LoginActivity.class);
        startActivity(logIn);
    }
    setContentView(R.layout.activity_joined);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);
    recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    GetJoinedEvents();
}

public void GetJoinedEvents(){

    String tag_string_req = "req_getJoinedEvents";

    StringRequest stringRequest = new StringRequest(Request.Method.GET, Config.URL_USER_lIST_EVENTS+user_id, new Response.Listener<String>(){
        @Override

        public void onResponse(String response) {
            // progressDialog.dismiss();
            Log.i("responseTest",response);

            try {

                JSONObject jsonObject = new JSONObject(response);
                JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
                int length = result.length();

                HashMap<String, String> events;
                for (int i = 0; i < length; i++) {
                    JSONObject jo = result.getJSONObject(i);
                    String event_id = jo.getString(Config.TAG_EVENT_ID);
                    String name = jo.getString(Config.TAG_EVENT_NAME);
                    String date = jo.getString(Config.TAG_EVENT_START_DT);
                    String weekday = jo.getString(Config.TAG_EVENT_WEEKDAY);
                    String event_type = jo.getString(Config.TAG_EVENT_TYPE);

                    events = new HashMap<>();
                    events.put(Config.TAG_EVENT_ID, event_id);
                    events.put(Config.TAG_EVENT_NAME, name);
                    events.put(Config.TAG_EVENT_START_DT, date);
                    events.put(Config.TAG_EVENT_WEEKDAY, weekday);
                    events.put(Config.TAG_EVENT_TYPE, event_type);
                    eventList.add(events);
                }

                listAdapter = new RecyclerViewAdapter(JoinedActivity.this,eventList, user_id);
                recyclerView.setAdapter(listAdapter);

            }
            catch (JSONException e) {
                e.printStackTrace();

            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Error", "Bad internet connection");

        }
    }) {};
    AppController.getInstance().addToRequestQueue(stringRequest, tag_string_req);
    Log.i("URL", stringRequest.toString());
}

}

【问题讨论】:

    标签: java android android-studio android-recyclerview android-volley


    【解决方案1】:

    更好的方法是在 onCreate 上初始化 Recycler 视图和适配器。稍后,当您拥有数据时,您可以创建一个 swapItems 方法,仅用于通过适配器更改 recyclerview 中的数据,但在 Activity 启动时,RecyclerView 及其适配器都会被实例化。

    在您的 swapItems 上,您可以执行以下操作:

    public void swapItems(List<Events> eventList) {
        if (eventList != null) {
            this.items = eventList;
            notifyDataSetChanged();// should call this to let the recyclerview know that our data set has changed
        }
    }
    

    然后你可以通过

    的活动调用它
    listAdapter.swapItems(eventList);// this is done the moment you already got your list of events 
    

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 2021-04-24
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多