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

时间:2019-03-14 10:00:44

标签: android json android-recyclerview picasso youtube-data-api

我正在使用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);



    }
}

}

以下是视频列表的课程

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>

2 个答案:

答案 0 :(得分:1)

您错误地从API解析JSON

替换

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

使用

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

“缩略图”位于“代码段” JSONObject中

答案 1 :(得分:0)

替换此行

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

使用

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

“缩略图”对象位于“片段”对象内部。