关于" Recycler View的错误。没有连接适配器,跳过布局"

时间:2017-11-26 10:10:18

标签: android android-recyclerview android-volley

我对适配器有一些问题,有loadData()方法:

MainActivity.java

public class MainActivity extends AppCompatActivity {
private static final String URL_DATA = "http://api.openweathermap.org/data/2.5/weather?q=Malang&appid=2c869002c83226532f2ac719d5c80c49";

private RecyclerView mRecyclerView;
private RecyclerView.Adapter adapter;
ArrayList<List> listItem;

LoadData()

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

    StringRequest stringRequest = new StringRequest(Request.Method.POST,
            URL_DATA,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    progressDialog.dismiss();
                    try {
                        JSONObject jobj = new JSONObject(s);
                        JSONArray array = jobj.getJSONArray("weather");

                        for (int i=0; i<array.length(); i++){
                            JSONObject obj = array.getJSONObject(i);
                            List item = new List(
                                    obj.getString("description"),
                                    obj.getString("main"),
                                    obj.getString("base")
                            );
                            listItem.add(item);
                        }
                        adapter = new CustomAdapter(getApplicationContext(), listItem);
                        mRecyclerView.setAdapter(adapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
  }
}

onCreate():

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listItem = new ArrayList<>();
    adapter = new CustomAdapter(MainActivity.this, listItem);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(adapter);
    loadData();
}

有人可以帮我解决这个问题吗?我真的是新手尝试这个凌空,谢谢你。

另一个java: 1.清单。的java

public class List {
//public int gambar;
public String nama;
public String suhu;
public String kelembapan;
public List (String nama, String suhu, String kelembapan) {

    this.nama = nama;
    this.suhu = suhu;
    this.kelembapan = kelembapan;
}
public String getNama(){
    return nama;
}
public String getSuhu(){
    return suhu;
}
public String getkelembapan(){
    return kelembapan;
}

}

  1. CustomAdapter.java - &gt; screenshot code of CustomAdapter.java

2 个答案:

答案 0 :(得分:0)

尝试使用MainActivity.this代替getApplicationContext()

在设置Adater

之前,还要将布局管理器设置为您的代码
mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));

答案 1 :(得分:0)

  

在你的onCreate方法

        listitem = new ArrayList<>();
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mAdapter = new CustomAdapter(listitem);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(mAdapter);
        loadData();
  

现在在LoadData()方法中替换这2行

adapter = new CustomAdapter(getApplicationContext(), listItem);
mRecyclerView.setAdapter(adapter);
  

以下行

mAdapter.notifyDataSetChanged();