为什么我的数据没有显示在 android 的回收站视图中

why my data is not showing in recycler view in android

我正在使用 youtube 数据制作一个应用程序 api 我遇到了一个问题,无法找到解决方案,因此我们将不胜感激

下面是我的代码我已经尝试了很多但没有找到任何解决方案而且我是初学者所以请帮助我, 在 onResponse 方法中,吐司消息正在工作,这意味着代码正在获得响应,但这些数据未显示在回收站视图中

public class MainActivity extends AppCompatActivity {

RecyclerView list;
LinearLayoutManager linearLayoutManager;

ArrayList<VideosList> videosLists;
CustomRecyclerAdapter listViewAdapter;
String url = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=myId&maxResults=3&key=myApiKey";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list = findViewById(R.id.rv);

    videosLists = new ArrayList<>();

    linearLayoutManager = new LinearLayoutManager(this);

    RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
    StringRequest  stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Toast.makeText(MainActivity.this,"Success",Toast.LENGTH_LONG).show();


            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("items");

                for (int i = 0; i < jsonArray.length(); i++ ) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                    JSONObject jsonObjectVideoId = jsonObject1.getJSONObject("id");
                    JSONObject jsonObjectSnippet = jsonObject1.getJSONObject("snippet");
                    JSONObject jsonObjectthumb = jsonObject1.getJSONObject("thumbnails").getJSONObject("medium");

                    String videoId = jsonObjectVideoId.getString("videoId");
                    String thumb = jsonObjectthumb.getString("url");

                    VideosList videosList = new VideosList();
                    videosList.setVideoId(videoId);
                    videosList.setTitle(jsonObjectSnippet.getString("title"));
                    videosList.setThumbnail(thumb);

                    videosLists.add(videosList);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(MainActivity.this,"failed",Toast.LENGTH_LONG).show();
        }
    });
    requestQueue.add(stringRequest);

    list.setHasFixedSize(true);
    list.setLayoutManager(linearLayoutManager);
    listViewAdapter = new CustomRecyclerAdapter(this, videosLists);
    list.setAdapter(listViewAdapter);

}

}

下面是上面代码的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
tools:context="com.creativeapps.entertainmentkidsyouth.MainActivity">


<android.support.v7.widget.RecyclerView
    android:id="@+id/rv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     />
</RelativeLayout>

下面是适配器的代码

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

private ArrayList<VideosList> videosList;

private Context context;


public CustomRecyclerAdapter(Context context,ArrayList<VideosList> videoList) {
    this.videosList = videoList;
    this.context = context;

}



@Override
public CustomRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview, parent, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(final CustomRecyclerAdapter.ViewHolder holder, int position) {

    holder.name.setText(videosList.get(position).getTitle());
    Picasso.get().load(videosList.get(position).getThumbnail()).into(holder.imageView);



}



@Override
public int getItemCount() {

    return videosList != null ?videosList.size():0;
}


public class ViewHolder extends RecyclerView.ViewHolder{

    public TextView name;
    ImageView imageView;


    private ViewHolder(View itemView) {
        super(itemView);
        imageView = itemView.findViewById(R.id.thumbnail);
        name = itemView.findViewById(R.id.title);



    }
}

}

下面是 class 视频列表

public class VideosList {
public String thumbnail, videoId, title;

public VideosList(String thumbnail, String videoId, String title) {
    this.thumbnail = thumbnail;
    this.videoId = videoId;
    this.title = title;
}

public VideosList() {
}

public String getThumbnail() {

    return thumbnail;
}

public void setThumbnail(String thumbnail) {
    this.thumbnail = thumbnail;
}

public String getVideoId() {
    return videoId;
}

public void setVideoId(String videoId) {
    this.videoId = videoId;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}
}

自定义回收器视图的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
    android:id="@+id/root"
    android:orientation="horizontal"
    android:gravity="center"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/thumbnail"
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:textSize="16sp"
        android:layout_marginStart="10dp"
        android:padding="10dp"
        android:textColor="#111"
        android:id="@+id/title"
        android:text="Video"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>

您错误地从 API

解析 JSON

替换

JSONObject jsonObjectthumb = jsonObject1.getJSONObject("thumbnails").getJSONObject("medium");

JSONObject jsonObjectthumb = jsonObject1.getJSONObject("snippet").getJSONObject("thumbnails").getJSONObject("medium");

"thumbnails" 在 "snippet" 里面 JSONObject

替换此行

JSONObject jsonObjectthumb = jsonObject1.getJSONObject("thumbnails").getJSONObject("medium");

JSONObject jsonObjectthumb = jsonObjectSnippet.getJSONObject("thumbnails").getJSONObject("medium");

'thumbnails' 对象在 'snippet' 对象内部。