我只想解析节是2 2的json。但作为响应,所有json文件都会解析

时间:2019-07-11 06:41:33

标签: java android json

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

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视图中,我得到json的记录,其中section == 2。这是5条记录,但在这些记录之后,我在recyclview中也获得了5张cardview,这是空的

1 个答案:

答案 0 :(得分:0)

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

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

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

注意-您可能需要稍作更改。