android - RecyclerView 中未显示的项目
问题描述
这是我现在面临的一个奇怪的问题。在我的应用程序中,我从链接中的 JSON 文件中获取数据,然后使用 Volley 解析它。现在我想在似乎不起作用的 RecyclerView 上显示数据,我很确定我的代码很好,但有些东西似乎不起作用,我找不到它。
活动主页.xml
这似乎在我用背景颜色测试时呈现:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.HomeActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_main_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
这是活动代码
HomeActivity.java:
public class HomeActivity extends AppCompatActivity {
private String url = "url/to/json.file";
private RecyclerView mList;
private LinearLayoutManager linearLayoutManager;
private List<News> newsList;
private RecyclerView.Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
newsList = new ArrayList<>();
mList = findViewById(R.id.rv_main_list);
adapter = new NewsAdapter(newsList);
mList.setHasFixedSize(true);
mList.setLayoutManager(new LinearLayoutManager(this));
mList.setAdapter(adapter);
getData();
}
这是我从适配器膨胀并从 JSON 文件设置数据的布局
layout_single_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#673AB7"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
和适配器类相关的代码,我猜:
@Override public NewsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); 查看视图 = layoutInflater.inflate(R.layout.layout_single_item, parent, false); ViewHolder viewHolder = new ViewHolder(view); 返回视图持有者;}
@Override
public void onBindViewHolder(@NonNull NewsAdapter.ViewHolder holder, int position) {
final News currentNews = newsList.get(position);
holder.tvTitle.setText(String.valueOf(currentNews.getTitle()));
Glide.with(mContext.getApplicationContext()).load(currentNews.getImage()).into(holder.ivArticleImage); }
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri newsUri = Uri.parse(currentNews.getNewsLink());
Intent toWebSiteIntent = new Intent(Intent.ACTION_VIEW, newsUri);
mContext.startActivity(toWebSiteIntent);
}
});
ViewHolder 类:
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvTitle;
public ImageView ivArticleImage;
public LinearLayout linearLayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
this.tvTitle = itemView.findViewById(R.id.tv_title);
this.ivArticleImage = itemView.findViewById(R.id.iv_image);
linearLayout = itemView.findViewById(R.id.linearlayout);
}
}
不知道我做错了什么。谢谢你的时间。
编辑:
获取数据函数:
private void getData() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
ArrayList<News> newsList = new ArrayList<>();
for(int i = 0; i < response.length(); i++){
try{
JSONObject jsonObject = response.getJSONObject(i);
News article = new News();
article.setTitle(jsonObject.getString("title"));
article.setImage(jsonObject.getString("image"));
}catch (JSONException e){
e.printStackTrace();
progressDialog.dismiss();
}
}
adapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
progressDialog.dismiss();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
更新 2:
进程:com.agencegg.apps.actualitecd,PID:15002 java.lang.NullPointerException:尝试在 com.agencegg 的空对象引用上调用虚拟方法 'android.content.Context android.content.Context.getApplicationContext()'。 com.agencegg.apps.actualitecd.adapters.NewsAdapter.onBindViewHolder(NewsAdapter.java:31) //...
解决方案
在适配器上添加一个方法,如下所示:
public void update(ArrayList<News> newsList){
this.newsList = newsList;
}
像这样更新 onResponse() 方法的列表:
@Override
public void onResponse(JSONArray response) {
ArrayList<News> newsList = new ArrayList<>();
for(int i = 0; i < response.length(); i++){
try{
JSONObject jsonObject = response.getJSONObject(i);
News article = new News();
article.setTitle(jsonObject.getString("title"));
article.setImage(jsonObject.getString("image"));
newsList.add(news).
}catch (JSONException e){
e.printStackTrace();
progressDialog.dismiss();
}
}
adapter.update(newsList)
adapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}
推荐阅读
- r - 如何使用 ggplot2 更改堆叠条形图的顺序和配色方案?
- c# - 在 ApplicationUser 中保持用户锁定状态的最佳方法是什么?
- karate - 在空手道框架中执行排序时出现 def 变量问题
- c++ - 如何让编译器读取下载的库?
- typescript - TypeScript - 如何设计一个动态呈现另一个类特征的类型类?
- excel - 点击用户表单中的提交以在下一行填写数据
- android - 尝试在谷歌地图上显示自定义标记时,BitmapDescriptor.fromBytes() 不起作用
- node.js - 用对象填充 SharedArrayBuffer
- ant - Ant 构建成功但不编译 CSS,可能是什么问题?
- javascript - 在nodejs中创建注册系统时收到未定义错误的isEmail