android - 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;
}
}
解决方案
好的,找到了问题,将您的 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();
基本上,当您进行网络调用时,它是异步发生的(在网络线程上)。因此,即使您已将数据添加到列表中,您的适配器也不知道它,因此通知它非常重要。
我希望这会有所帮助,如果它不能解决您的问题,请发表评论。
推荐阅读
- python - SpeechRecognition 在 Raspberry Pi 上引发 ALSA 错误
- android - 如何强制在外部浏览器中打开应用链接
- mongodb - 如何查找最近检查日期在 2020 年之后的文件?
- javascript - 从 Airports.JSON 查询 Airport Lat 和 long 反应原生
- html - 如何从输入中获取信息并将其放入href
- pine-script - 有没有办法在函数中使用 bgcolor ?
- javascript - Next.js 增量静态重新生成不反映更改而不重新加载页面
- flutter - 例外:坏状态:未来已经完成
- azure - 视频索引器 API INVALID_INPUT - 索引预设“DefaultWithNoiseReduction”无效
- scala - Sttp Uri 避免解码