Android 未在RecyclerView中显示的项目

Android 未在RecyclerView中显示的项目,android,json,data-binding,android-recyclerview,Android,Json,Data Binding,Android Recyclerview,这是我现在面临的一个奇怪的问题。 在我的应用程序中,我从一个链接中获取JSON文件中的数据,然后用截取法解析它。现在我想在一个RecyclerView上显示数据,它似乎不起作用,我很高兴我的代码很好,但有些东西似乎不起作用,我找不到它 activity_home.xml 这似乎是在我测试背景色时呈现的: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintL

这是我现在面临的一个奇怪的问题。 在我的应用程序中,我从一个链接中获取JSON文件中的数据,然后用截取法解析它。现在我想在一个RecyclerView上显示数据,它似乎不起作用,我很高兴我的代码很好,但有些东西似乎不起作用,我找不到它

activity_home.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>
Glide.with(mContext.getApplicationContext()).load(currentNews.getImage())到(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);
        }
    });
持证人类别:

 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);


        }
    }
不知道我做错了什么。谢谢你抽出时间

编辑:

getData函数:

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);

    }
private void getData(){
final ProgressDialog ProgressDialog=新建ProgressDialog(此);
progressDialog.setMessage(“加载…”);
progressDialog.show();
JsonArrayRequest JsonArrayRequest=newjsonarrayrequest(url,new Response.Listener()){
@凌驾
公共void onResponse(JSONArray响应){
ArrayList newsList=新建ArrayList();
对于(int i=0;i
更新2:

流程:com.agencegg.apps.implementecd,PID:15002 java.lang.NullPointerException:尝试调用虚拟方法“android.content.Context” 空对象上的android.content.Context.getApplicationContext() 参考 在com.agencegg.apps.implementecd.adapters.NewsAdapter.onBindViewHolder(NewsAdapter.java:73)上 在com.agencegg.apps.implementecd.adapters.NewsAdapter.onBindViewHolder(NewsAdapter.java:31)/


在适配器上添加如下方法:

public void update(ArrayList<News> newsList){
            this.newsList = newsList;
        }
公共作废更新(ArrayList新闻列表){
this.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();
        }
    }
@覆盖
公共void onResponse(JSONArray响应){
ArrayList newsList=新建ArrayList();
对于(int i=0;i
在适配器上添加如下方法:

public void update(ArrayList<News> newsList){
            this.newsList = newsList;
        }
公共作废更新(ArrayList新闻列表){
this.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();
        }
    }
@覆盖
公共void onResponse(JSONArray响应){
ArrayList newsList=新建ArrayList();
对于(int i=0;i
从api获取数据后,您是否更新了适配器?最初列表为空。通过调用
adapter.notifyDataSetChanged()?使用新闻列表更新适配器。在适配器类上创建一个方法。api调用后,然后调用此方法,然后调用adapter.notifyDataSetChanged()。我没有使用api,它是到JSON文件的直接链接。请共享getData()方法代码以更好地理解。解析json文件中的数据后,从适配器调用更新方法。从api中获取数据后,是否更新了适配器?最初列表为空。通过调用
adapter.notifyDataSetChanged()?使用新闻列表更新适配器。在适配器类上创建一个方法。api调用后,然后调用此方法,然后调用adapter.notifyDataSetChanged()。我没有使用api,它是到JSON文件的直接链接。请共享getData()方法代码以更好地理解。解析json文件中的数据后,从适配器调用update方法。注释不用于扩展讨论;这段对话已经结束。我还有另一个请求:评论不是为了进一步讨论;这段对话已经结束了。我还有一个要求
@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();
        }
    }