首页 > 解决方案 > JSON 数据只显示一次

问题描述

我正在尝试从 JSON 文件中获取数据。但它只显示一次。当我关闭应用程序并再次打开时,它不会显示结果。当我清除应用程序的缓存时,它会再次显示我的结果。logcat 中没有错误,应用程序也没有崩溃。我不知道该怎么办。这个问题有一个通用的解决方案还是我做了什么?

这是我的 Games.class;

    public class Games extends AppCompatActivity {

    RecyclerView recyclerView;
    ArrayList<ModelGames> modelGames;
    private RequestQueue mQueue;

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

        modelGames = new ArrayList<>();

        recyclerView = findViewById(R.id.gamesPageRecyclerView);

        mQueue = Volley.newRequestQueue(this);
        jsonParse();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        RecyclerView.LayoutManager gamesLiLayoutManager = linearLayoutManager;
        recyclerView.setLayoutManager(gamesLiLayoutManager);

        GamesAdapter adapter = new GamesAdapter(this, modelGames);
        recyclerView.setAdapter(adapter);
    }

    private void jsonParse() {
        String popularIn2019 = "https://api.rawg.io/api/games?dates=2019-01-01,2019-12-31&ordering=-added";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, popularIn2019, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("results");

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

                                JSONObject result = jsonArray.getJSONObject(i);
                                String gameName = result.getString("name");
                                String gameImage = result.getString("background_image");
                                modelGames.add(new ModelGames(gameName, gameImage));
                            }

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

这是我的 GamesAdapter;

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

    private Context mContext;
    private ArrayList<ModelGames> mList;

    GamesAdapter(Context context, ArrayList<ModelGames> list){
        mContext = context;
        mList = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View view = layoutInflater.inflate(R.layout.rv_game_items, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ModelGames getItem = mList.get(position);
        TextView gameName = holder.game_title;
        ImageView gameImage = holder.game_image;
        Glide.with(mContext).asBitmap().load(getItem.getImage()).into(gameImage);

        gameName.setText(mList.get(position).getText());

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView game_image;
        TextView game_title;


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

            game_image = itemView.findViewById(R.id.cardviewImage);
            game_title = itemView.findViewById(R.id.cardviewText);


        }
    }
}

这是我的模型游戏;

public class ModelGames {

    private String text, image;

    public ModelGames(String text, String image) {
        this.text = text;
        this.image = image;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }
}

标签: androidjson

解决方案


好的,找到了问题,将您的 JSON 数据添加到 ModelGames 的数组列表后,您没有调用

adapter.notifyDataSetChanged();

因此,首先,在 onCreate 之前将您的适配器设为全局变量:

private GamesAdapter adapter;

然后在你的 onCreate 里面这样做:

 adapter = new GamesAdapter(this, modelGames);
 recyclerView.setAdapter(adapter);

在你的 jsonParse() 方法内部(在 onResponse 内部)执行如下操作:

JSONArray jsonArray = response.getJSONArray("results");

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

    JSONObject result = jsonArray.getJSONObject(i);
    String gameName = result.getString("name");
    String gameImage = result.getString("background_image");
    modelGames.add(new ModelGames(gameName, gameImage));
}

adapter.notifyDataSetChanged();

基本上,当您进行网络调用时,它是异步发生的(在网络线程上)。因此,即使您已将数据添加到列表中,您的适配器也不知道它,因此通知它非常重要。

我希望这会有所帮助,如果它不能解决您的问题,请发表评论。


推荐阅读