简体   繁体   中英

Recycler view not showing after parsing json data using Volley

Inside my tab bar i want to show my data in Recycler view by using json parsing using volley library. I parsed the data and it showing in toast perfectly.But in Recycler view its not showing Here is my codes.

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.xml.transform.ErrorListener;

import info.tatwa.adupter.AdupterBoxOffice;
import info.tatwa.extras.Keys;
import info.tatwa.extras.UrlEndPoint;
import info.tatwa.login.L;
import info.tatwa.model.Movies;
import info.tatwa.network.MyApplication;
import info.tatwa.network.VolleySingleton;
import info.tatwa.newnav.R;
import info.tatwa.extras.UrlEndPoint.*;
import info.tatwa.extras.Keys.EndpointBoxOffice.*;

public class FragmentBoxOffice extends Fragment {
private VolleySingleton volleySingleton;
private RequestQueue requestQueue;
private AdupterBoxOffice adupterBoxOffice;
private ArrayList<Movies>listMovie = new ArrayList<>();
private ImageLoader imageLoader;
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
public static final String ROTET_TOMATO_URL_BOX_OFFICE="My url is here"
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private RecyclerView listMovieHits;

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;


/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment FragmentBoxOffice.
 */

 public static FragmentBoxOffice newInstance(String param1, String param2) {
    FragmentBoxOffice fragment = new FragmentBoxOffice();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
 }
public static String getRequestUrl(int limit){
    //ROTET_TOMATO_URL_BOX_OFFICE+"?apikey="+          MyApplication.ROTET_TOMATO_API_KEY+"&limit="+limit;
    return UrlEndPoint.URL_BOX_OFFICE +
            UrlEndPoint.URL_CHAR_QUASTIAN+
            UrlEndPoint.URL_CHAR_PAREM_APIKEY +
            MyApplication.ROTET_TOMATO_API_KEY +
            UrlEndPoint.URL_CHAR_APPEND+
            UrlEndPoint.URL_CHAR_PAREM_LIMIT + limit;
}

public FragmentBoxOffice() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
    volleySingleton = VolleySingleton.getsInstance();
    requestQueue = volleySingleton.getmRequestQueue();
    senJsonRequest();
}
public void senJsonRequest(){
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, getRequestUrl(10), (String)null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            paresJSONResponse(response);
            listMovie =paresJSONResponse(response);
            adupterBoxOffice.setMovieList(listMovie);
            L.t(getActivity(), response.toString());
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity(),"Error"+error.getMessage(),Toast.LENGTH_SHORT).show();
        }
    });
    requestQueue.add(request);
}
public ArrayList<Movies> paresJSONResponse(JSONObject response){
    ArrayList<Movies> listMovie = new ArrayList<>();
    if(response ==null|| response.length()==0 ){
        return null;
    }
    try {
        StringBuilder data = new StringBuilder();
        JSONArray arrayMovie = response.getJSONArray(Keys.EndpointBoxOffice.KEY_MOVIES);
        Log.v("BIKASH", "JSON Object id" + arrayMovie);
        for(int i = 0; i<arrayMovie.length();i++){
           JSONObject currentMovies= arrayMovie.getJSONObject(i);
            Long id =currentMovies.getLong(Keys.EndpointBoxOffice.KEY_ID);
            String title=currentMovies.getString(Keys.EndpointBoxOffice.KEY_TITLE);
            JSONObject objectReleaseDates=currentMovies.getJSONObject(Keys.EndpointBoxOffice.KEY_RELEASE_DATES);
            String releaseDate=null;
            if(objectReleaseDates.has(Keys.EndpointBoxOffice.KEY_THEATER)){
                releaseDate = objectReleaseDates.getString(Keys.EndpointBoxOffice.KEY_THEATER);
            }
            else {
                releaseDate="NA";
            }
            int audianceRatting=-1;
            JSONObject objectRatting = currentMovies.getJSONObject(Keys.EndpointBoxOffice.KEY_RATINGS);
            {
                if(objectRatting.has(Keys.EndpointBoxOffice.KEY_AUDIENCE_SCORE)){
                    audianceRatting=objectRatting.getInt(Keys.EndpointBoxOffice.KEY_AUDIENCE_SCORE);
                }

            }
            String synuypsis = currentMovies.getString(Keys.EndpointBoxOffice.KEY_SYNOPSIS);
            String urlThumbnel = null;
            JSONObject objPoster = currentMovies.getJSONObject(Keys.EndpointBoxOffice.KEY_THUMBNAIL);
            if(objPoster.has(Keys.EndpointBoxOffice.KEY_THUMBNAIL)){
                urlThumbnel = objPoster.getString(Keys.EndpointBoxOffice.KEY_THUMBNAIL);
            }

            Movies movie =new Movies();
            movie.setId(id);
            movie.setTitle(title);
           Date date = dateFormat.parse(releaseDate);
            movie.setReleaseDateTheater(date);
            movie.setAudienceScore(audianceRatting);
            movie.setSynopsis(synuypsis);
            movie.setUrlThumbnail(urlThumbnel);

            listMovie.add(movie);
          //  data.append(id + "\n");
        }
        L.t(getActivity(),listMovie.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return listMovie;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =inflater.inflate(R.layout.fragment_fragment_box_office, container, false);
    listMovieHits =(RecyclerView)view.findViewById(R.id.listMovieHits);
    listMovieHits.setLayoutManager(new LinearLayoutManager(getActivity()));
    adupterBoxOffice = new AdupterBoxOffice(getActivity());
    listMovieHits.setAdapter(adupterBoxOffice);
    senJsonRequest();
    return view;
}

}

My Adupter

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;

import java.util.ArrayList;

import info.tatwa.model.Movies;
import info.tatwa.myfragment.FragmentBoxOffice;
import info.tatwa.network.VolleySingleton;
import info.tatwa.newnav.R;


public class AdupterBoxOffice extends         RecyclerView.Adapter<AdupterBoxOffice.ViewHolderBoxOffice> {
private LayoutInflater layoutInflater;
FragmentBoxOffice activity;

private ArrayList<Movies> listMovie = new ArrayList<>();
private VolleySingleton volleySingleton;
private ImageLoader imageLoader;

public AdupterBoxOffice(Context context){
    layoutInflater=LayoutInflater.from(context);
    volleySingleton=VolleySingleton.getsInstance();
    imageLoader = volleySingleton.getImageLoader();

}
public void setMovieList(ArrayList<Movies>listMovie){
 this.listMovie = listMovie;
    notifyItemRangeChanged(0,listMovie.size());
}
@Override
public ViewHolderBoxOffice onCreateViewHolder(ViewGroup parent, int viewType) {
    layoutInflater = LayoutInflater.from(parent.getContext());
    View view = layoutInflater.inflate(R.layout.movie_list_itom,parent,false);
    ViewHolderBoxOffice viewHolder =new ViewHolderBoxOffice(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolderBoxOffice holder, int position) {
    Movies currentMovie =listMovie.get(position);
    holder.movieTitle.setText(currentMovie.getTitle());
    holder.movieReleaseDate.setText(currentMovie.getReleaseDateTheater().toString());
    holder.movieRatting.setRating(currentMovie.getAudienceScore()/20.0f);
   String urlThumbnel = currentMovie.getUrlThumbnail();
    if(urlThumbnel!=null){
        imageLoader.get(urlThumbnel, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                holder.imagePoster.setImageBitmap(response.getBitmap());
            }

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }
}

@Override
public int getItemCount() {
    return listMovie.size();
}

static class ViewHolderBoxOffice extends RecyclerView.ViewHolder {
    private ImageView imagePoster;
    private TextView movieTitle;
    private TextView movieReleaseDate;
    private RatingBar movieRatting;
    public ViewHolderBoxOffice(View itemView) {
        super(itemView);
        imagePoster=(ImageView)itemView.findViewById(R.id.movieThumbnail);
        movieTitle =(TextView)itemView.findViewById(R.id.movieTitle);
        movieReleaseDate=(TextView)itemView.findViewById(R.id.movieReleaseDate);
        movieRatting=(RatingBar)itemView.findViewById(R.id.movieAudienceScore);
    }
}

}

And My Model class

import android.os.Parcel;
import android.os.Parcelable;

import java.util.Date;




public class Movies{ 
private long id;
private String title;
private Date releaseDateTheater;
private int audienceScore;
private String synopsis;
private String urlThumbnail;
private String urlSelf;
private String urlCast;
private String urlReviews;
private String urlSimilar;

public Movies() {

}



public Movies(long id,
             String title,
             Date releaseDateTheater,
             int audienceScore,
             String synopsis,
             String urlThumbnail,
             String urlSelf,
             String urlCast,
             String urlReviews,
             String urlSimilar) {
    this.id = id;
    this.title = title;
    this.releaseDateTheater = releaseDateTheater;
    this.audienceScore = audienceScore;
    this.synopsis = synopsis;
    this.urlThumbnail = urlThumbnail;
    this.urlSelf = urlSelf;
    this.urlCast = urlCast;
    this.urlReviews = urlReviews;
    this.urlSimilar = urlSimilar;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public Date getReleaseDateTheater() {
    return releaseDateTheater;
}

public void setReleaseDateTheater(Date releaseDateTheater) {
    this.releaseDateTheater = releaseDateTheater;
}

public int getAudienceScore() {
    return audienceScore;
}

public void setAudienceScore(int audienceScore) {
    this.audienceScore = audienceScore;
}

public String getSynopsis() {
    return synopsis;
}

public void setSynopsis(String synopsis) {
    this.synopsis = synopsis;
}

public String getUrlThumbnail() {
    return urlThumbnail;
}

public void setUrlThumbnail(String urlThumbnail) {
    this.urlThumbnail = urlThumbnail;
}

public String getUrlSelf() {
    return urlSelf;
}

public void setUrlSelf(String urlSelf) {
    this.urlSelf = urlSelf;
}

public String getUrlCast() {
    return urlCast;
}

public void setUrlCast(String urlCast) {
    this.urlCast = urlCast;
}

public String getUrlReviews() {
    return urlReviews;
}

public void setUrlReviews(String urlReviews) {
    this.urlReviews = urlReviews;
}

public String getUrlSimilar() {
    return urlSimilar;
}

public void setUrlSimilar(String urlSimilar) {
    this.urlSimilar = urlSimilar;
}

@Override
public String toString() {
    return "\nID: " + id +
            "\nTitle " + title +
            "\nDate " + releaseDateTheater +
            "\nSynopsis " + synopsis +
            "\nScore " + audienceScore +
            "\nurlThumbnail " + urlThumbnail +
            "\nurlSelf " + urlSelf +
            "\nurlCast " + urlCast +
            "\nurlReviews " + urlReviews +
            "\nurlSimilar " + urlSimilar +
            "\n";
}

}

Please help me out .. Thanks in advance ..

After adupterBoxOffice.setMovieList(listMovie);
try add adupterBoxOffice.notifyDataSetChanged();

I found my solution.I put some wrong key during pars the Json. That's why my ArrayList returning zero.

Many many thanks to all for helping me out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM