为什么我的数据没有显示在android的回收器视图中
我正在使用youtube数据api制作一个应用程序,我遇到了一个问题,无法找到解决方案,因此任何帮助都将不胜感激 下面是我的代码,我已经尝试了很多,但没有找到任何解决方案,我是初学者,所以请在此方面帮助我, 在onResponse方法中,toast messege正在工作,这意味着代码正在得到响应,但这些数据没有显示在recycler视图中为什么我的数据没有显示在android的回收器视图中,android,json,android-recyclerview,picasso,youtube-data-api,Android,Json,Android Recyclerview,Picasso,Youtube Data Api,我正在使用youtube数据api制作一个应用程序,我遇到了一个问题,无法找到解决方案,因此任何帮助都将不胜感激 下面是我的代码,我已经尝试了很多,但没有找到任何解决方案,我是初学者,所以请在此方面帮助我, 在onResponse方法中,toast messege正在工作,这意味着代码正在得到响应,但这些数据没有显示在recycler视图中 public class MainActivity extends AppCompatActivity { RecyclerView list; Line
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"
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");
与
“缩略图”在“snippet”JSONObject中替换此行
JSONObject jsonObjectthumb = jsonObject1.getJSONObject("thumbnails").getJSONObject("medium");
与
“缩略图”对象位于“snippet”对象内。您需要在
onResponse()
方法内设置适配器。谢谢您的评论,但它不起作用。谢谢您的回答!它起作用了,但我给第一个答案打了勾,因为它是第一个答案
<?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>
JSONObject jsonObjectthumb = jsonObject1.getJSONObject("thumbnails").getJSONObject("medium");
JSONObject jsonObjectthumb = jsonObject1.getJSONObject("snippet").getJSONObject("thumbnails").getJSONObject("medium");
JSONObject jsonObjectthumb = jsonObject1.getJSONObject("thumbnails").getJSONObject("medium");
JSONObject jsonObjectthumb = jsonObjectSnippet.getJSONObject("thumbnails").getJSONObject("medium");