没有连接适配器,在RecyclerView中跳过布局

时间:2017-08-07 07:33:42

标签: android json android-recyclerview recycler-adapter recyclerview-layout

当我尝试从JSON加载数据时,我的recyclerView变为空白。 在logcat中,它说:“没有连接适配器;跳过布局”

这是我的活动

public class MainActivity extends AppCompatActivity {

private RecyclerView news;
private RecyclerView.Adapter news_adapter;
private List<News_Item> listItems;
private static final String news_data = "Url to Json file";

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    news = (RecyclerView) findViewById(R.id.news);
    news.setHasFixedSize(true);
    news.setLayoutManager(new LinearLayoutManager(this));

    listItems = new ArrayList<>();
    loadRecyclerViewData();

}

private void loadRecyclerViewData(){
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Loading News...");
    progressDialog.show();

    StringRequest stringRequest = new StringRequest(Request.Method.GET,
            news_data,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    progressDialog.dismiss();
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray array = jsonObject.getJSONArray("articles");

                        for(int i = 0; i < array.length(); i++){
                            JSONObject o = array.getJSONObject(i);
                            News_Item item = new News_Item(
                                    o.getString("title"),
                                    o.getString("description"),
                                    o.getString("author"),
                                    o.getString("urlToImage"),
                                    o.getString("URL")
                            );

                            listItems.add(item);
                        }

                        news_adapter = new News_Adapter(listItems,getApplicationContext());
                        news.setAdapter(news_adapter);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            progressDialog.dismiss();
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}
}

但是当我在其中加载虚拟数据时,它可以正常工作

 listItems = new ArrayList<>();

    for(int i= 0; i<=10 ; i++){
        News_Item news_item = new News_Item(
                "Heading" + (i+1),
                "Lorem Ipsum",
                "Lorem Ipusm",
                "Lorem Ipsum",
                "Lorem Ipsum"
        );
        listItems.add(news_item);
    }
    news_adapter = new News_Adapter(listItems,this);
    news.setAdapter(news_adapter);

我试着在stackoverflow上阅读其他一些问题,但是大部分都没有帮助question 1
question 2question 3

这是我的News_adapter

public class News_Adapter extends RecyclerView.Adapter<News_Adapter.ViewHolder> {

private List<News_Item> listItems;
private Context context;

public News_Adapter(List<News_Item> listItems, Context context) {
    this.listItems = listItems;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card,parent,false);

    return new ViewHolder(v);

}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final News_Item item = listItems.get(position);

    holder.news_title.setText(item.getTitle());
    holder.news_desc.setText(item.getSubtitle());
    holder.news_author.setText(item.getAuthor());

    Picasso.with(context)
    .load(item.getImg_url())
    .into(holder.news_img);

    holder.card_body.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context, "item clicked "+item.getTitle() , Toast.LENGTH_SHORT).show();
        }
    });

}

@Override
public int getItemCount() {
    return listItems.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{

    public TextView news_title,news_desc,news_author;
    public ImageView news_img;
    public RelativeLayout card_body;

    public ViewHolder(View itemView) {
        super(itemView);

        news_author = (TextView) itemView.findViewById(R.id.news_author);
        news_desc = (TextView) itemView.findViewById(R.id.news_desc);
        news_title = (TextView) itemView.findViewById(R.id.news_title);
        news_img = (ImageView) itemView.findViewById(R.id.news_img);
        card_body = (RelativeLayout) itemView.findViewById(R.id.card_body);

    }
}

}

1 个答案:

答案 0 :(得分:0)

放线 -

news_adapter = new News_Adapter(listItems,getApplicationContext());
news.setAdapter(news_adapter);
onCreate()电话

之前的loadRecyclerViewData()方法中

所以onCreate()就像 -

listItems = new ArrayList<>();
news_adapter = new News_Adapter(listItems,getApplicationContext());
news.setAdapter(news_adapter);
loadRecyclerViewData();

然后在onResponse()方法中执行news_adapter.notifyDataSetChanged();而不是上述2行。

更新1 将您的行o.getString("URL")替换为o.getString("url")。另外,不要使用getString总是使用optString,这样如果某些信息不是来自JSON,那么您的代码就不会中断。