使用cardview在recyclerview中显示json数据

时间:2019-01-05 02:33:39

标签: json android-studio android-recyclerview

我正在尝试使用recyclerview和cardview在listview上显示json文件中的数据。 当我启动应用程序时,它只会显示一个空白屏幕。我不确定自己做错了什么,因为android studio上没有错误。

请您仔细检查我的代码并帮助我纠正错误。我尝试在论坛和youtube上搜索没有运气的解决方案。

下面是我的json文件和mainactivity.java。

我的json文件

{
"movie": [
{
"title": "movie1",
"genre": "action",
"imgURL": "https://demonuts.com/Demonuts/SampleImages/roger.jpg"
},
{
"title": "movie2",
"genre": "romance",
"imgURL": "https://demonuts.com/Demonuts/SampleImages/murray.jpg"
},
{
"title": "movie3",
"genre": "drama",
"imgURL": "https://demonuts.com/Demonuts/SampleImages/djoko.jpg"
},
{
"title": "movie4",
"genre": "comedie",
"imgURL": "https://demonuts.com/Demonuts/SampleImages/nadal.jpg"
}
]
}

mainactivity.java

public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ExampleAdapter mExampleAdapter;
private ArrayList<ExampleItem> mExampleList;
private RequestQueue mRequestQueue;

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

    mRecyclerView = findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    mExampleList = new ArrayList<>();

    mRequestQueue = Volley.newRequestQueue(this);
    parseJSON();
}

private void parseJSON() {
    String url = "https://api.jsonbin.io/b/5c2fc20381fe89272a845c69/1";

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("movie");

                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject movie = jsonArray.getJSONObject(i);

                            String title = movie.getString("title");
                            String imageUrl = movie.getString("imageURL");
                            String genre = movie.getString("genre");

                            mExampleList.add(new ExampleItem(imageUrl, title, genre));
                        }

                        mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
                        mRecyclerView.setAdapter(mExampleAdapter);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

    mRequestQueue.add(request);
}
}

1 个答案:

答案 0 :(得分:0)

更改movie.getString(“ imageURL”);到movie.getString(“ imgURL”)

                            for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject movie = jsonArray.getJSONObject(i);
                            String title = movie.getString("title");
                            String imageUrl = movie.getString("imgURL");
                            String genre = movie.getString("genre");

                            mExampleList.add(new ExampleItem(imageUrl, title, genre));
                        }