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

问题描述 投票:0回答:2

我正在使用youtube数据api创建一个应用程序,我遇到了问题,无法找到解决方案,所以任何帮助将不胜感激

下面是我的代码,我已经尝试了很多,但没有找到任何解决方案,我是初学者,所以亲切地帮助我,在onResponse方法toast messege工作,这意味着代码得到响应,但这些数据没有显示在回收者的观点

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>
android json android-recyclerview picasso youtube-data-api
2个回答
1
投票

您错误地从API解析JSON

更换

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

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

“缩略图”位于“片段”JSONObject中


0
投票

替换此行

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

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

“缩略图”对象位于“代码段”对象内。

© www.soinside.com 2019 - 2024. All rights reserved.