gpt4 book ai didi

java - 我只想解析 json,其中部分是 2 两个。但响应所有 json 文件解析

转载 作者:行者123 更新时间:2023-12-02 03:12:47 25 4
gpt4 key购买 nike

this is my response i want to remove null cardviews我只想解析具有部分 == 2 的 json 对象,它可以工作,但在我的 recyclerview 中也打印数组长度的空值。

package com.example.movies.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.movies.R;
import com.example.movies.fragments.Movies;
import com.example.movies.model.Songsapis;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;




public class MoviesFragmentRVAdapter extends RecyclerView.Adapter<MoviesFragmentRVAdapter.RecyclerViewHolder> {

private Context context;
ArrayList<Songsapis> songsapis;
private int drawable;
private static ClickListener clickListener;

public MoviesFragmentRVAdapter(Movies context, int drawable) {
this.drawable = drawable;
songsapis = new ArrayList<>();
}






@Override
public MoviesFragmentRVAdapter.RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new RecyclerViewHolder(LayoutInflater.from(parent.getContext())
.inflate(drawable, parent, false));
}

@Override
public void onBindViewHolder(MoviesFragmentRVAdapter.RecyclerViewHolder holder, int position) {

Context context = holder.bookImage.getContext();
Songsapis songs = songsapis.get(position);
if (songs.getSection().equals("2")){
holder.bookTitle.setText(songs.getTitle());
Picasso.with(context)
.load(songs.getThumbnail())
.into(holder.bookImage);

}









}

@Override
public int getItemCount() {
return (songsapis.size() );
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {



private TextView bookTitle;
private ImageView bookImage;
CardView container;


public RecyclerViewHolder(View itemView) {
super(itemView);

itemView.setOnClickListener(this);

bookTitle = itemView.findViewById(R.id.category_title_id);
bookImage = itemView.findViewById(R.id.category_img_id);
container = itemView.findViewById(R.id.grid_card);

}

@Override
public void onClick(View v) {
clickListener.onItemClick(getAdapterPosition(), v);

}
}

public void addAll(List<Songsapis> list) {

for (int i = 0; i < list.size(); i++) {


songsapis.add(list.get(i));
notifyDataSetChanged();
}


}

public void setOnItemClickListener(ClickListener clickListener) {
MoviesFragmentRVAdapter.clickListener = clickListener;
}

public interface ClickListener {
void onItemClick(int position, View v);

}
}

这是我的 json

  {
package: {
id: "com.latesthindisongs.app",
offline: 0,
title: "Latest Songs",
appversion: "1.0"
},
sections: [
{
id: 2,
title: "80's Songs"
},
{
id: 3,
title: "90's Songs"
}
],
listings: [
{
section: 2,
title: "Ranveer Singh NEW Hindi Movie ",
url: "https://www.youtube.com/watch?v=iTqACYjHy5",
thumbnail: "https://img.youtube.com/vi/FOaXXdi-8TE/maxresdefault.jpg",
type: "video"
},
{
section: 2,
title: "Dekha Ek Khwab Song, Silsila | Amitabh | Rekha | Kishore Kumar | Lata Mangeshkar",
url: "https://www.youtube.com/watch?v=43wT0xhvfsA",
thumbnail: "https://img.youtube.com/vi/43wT0xhvfsA/maxresdefault.jpg",
type: "video"
},
{
section: 2,
title: "Pyar Ka Dard - Kishore Kumar & Asha Bhosle",
url: "https://www.youtube.com/watch?v=BYsNBclR4nQ",
thumbnail: "https://img.youtube.com/vi/BYsNBclR4nQ/maxresdefault.jpg",
type: "video"
},
{
section: 2,
title: "Ek Roz Main Tadapkar (HD) - Bemisal Songs - Amitabh Bachchan - Rakhee Gulzar - Kishore Kumar",
url: "https://www.youtube.com/watch?v=TvdYoPEDlKM",
thumbnail: "https://img.youtube.com/vi/TvdYoPEDlKM/maxresdefault.jpg",
type: "video"
},
{
section: 2,
title: "Kitni Khoobsoorat Yeh | Rakhee | Amitabh Bachchan | Bemisal | Vinod Mehra | Kishore Kumar ",
url: "https://www.youtube.com/watch?v=h3yQJZagDTw",
thumbnail: "https://img.youtube.com/vi/h3yQJZagDTw/maxresdefault.jpg",
type: "video"
},
{
section: 3,
title: "Ranveer Singh NEW Hindi Movie ",
url: "https://www.youtube.com/watch?v=iTqACYjHy5",
thumbnail: "https://img.youtube.com/vi/FOaXXdi-8TE/maxresdefault.jpg",
type: "video"
},
{
section: 3,
title: "Dekha Ek Khwab Song, Silsila | Amitabh | Rekha | Kishore Kumar | Lata Mangeshkar",
url: "https://www.youtube.com/watch?v=43wT0xhvfsA",
thumbnail: "https://img.youtube.com/vi/43wT0xhvfsA/maxresdefault.jpg",
type: "video"
},
{
section: 3,
title: "Tu Is Tarah male Aap To Aise Na",
url: "https://www.youtube.com/watch?v=OoZukqIx8q4",
thumbnail: "https://img.youtube.com/vi/Sa3u73QJ4bk/maxresdefault.jpg",
type: "video"
},
{
section: 3,
title: "Pyar Ka Dard - Kishore Kumar & Asha Bhosle",
url: "https://www.youtube.com/watch?v=BYsNBclR4nQ",
thumbnail: "https://img.youtube.com/vi/BYsNBclR4nQ/maxresdefault.jpg",
type: "video"
},
{
section: 3,
title: "Ek Roz Main Tadapkar (HD) - Bemisal Songs - Amitabh Bachchan - Rakhee Gulzar - Kishore Kumar",
url: "https://www.youtube.com/watch?v=TvdYoPEDlKM",
thumbnail: "https://img.youtube.com/vi/TvdYoPEDlKM/maxresdefault.jpg",
type: "video"
}
]
}

在我的 ui View 中,我获取了 json 的记录,其中section == 2。这是 5 条记录,但在这些记录之后,我还在我的回收 View 中得到了 5 个为空的卡片 View

最佳答案

你可以做这样的事情,这是非常基本的解决方案,

  ArrayList<Songsapis> list = new ArrayList<Songsapis>();
for(Songsapis s: YOUR_LIST){
if(s.getSection.equals("2")){
list.add(s);
}
}

这将创建一个新列表,其中仅包含部分为 2 的对象。将此列表传递给您的适配器。

注意 - 您可能需要稍微更改一下代码。

关于java - 我只想解析 json,其中部分是 2 两个。但响应所有 json 文件解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56983066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com