RecyclerView dosent显示以下任何元素是我的代码

时间:2016-12-07 06:52:08

标签: android android-recyclerview

Fragment_bookmovie2.java

public class Fragment_bookmovie2 extends android.support.v4.app.Fragment implements View.OnClickListener {
private static final String TAG ="Fragment_bookmovie2" ;
ArrayList<String> moviename= new ArrayList<>();
ArrayList<String> movieyear= new ArrayList<>();
ArrayList<String> moviebanner= new ArrayList<>();

private RecyclerView.LayoutManager layoutManager;
private static RecyclerView recyclerView;
Movie_listAdapter2 adapter ;


LinearLayout progress;
RelativeLayout ll;
public Fragment_bookmovie2() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Log.e(TAG,"reched on create");
    // first step - retrieve object
    Backendless.Persistence.of(Booking.class).find(new AsyncCallback<BackendlessCollection<Booking>>() {
        @Override
        public void handleResponse(BackendlessCollection<Booking> response) {
            for(Booking booking : response.getData()) {
                if(!moviename.contains(booking.getMovie().get(0).getMovie_Name())) {
                    moviename.add(booking.getMovie().get(0).getMovie_Name());
                    movieyear.add(booking.getMovie().get(0).getYear());
                    moviebanner.add("https://api.backendless.com/" + BackendSetting.APPLICATION_ID + "/" + BackendSetting.VERSION + "/files/mypics/" + booking.getMovie().get(0).getImage());
                }
            }
            adapter.notifyDataSetChanged();
            ll.removeView(progress);
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Toast.makeText(getActivity(), fault.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    moviename.add("haha");
    movieyear.add("hello");
    moviebanner.add("http://www.w3schools.com/css/trolltunga.jpg");

    // Inflate the layout for this fragment
    ll = (RelativeLayout) inflater.inflate(R.layout.fragment_bookmovie2, container, false);
    progress = (LinearLayout) ll.findViewById(R.id.progress_bookingFrag);


    recyclerView = (RecyclerView) ll.findViewById(R.id.recycle_movielist);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    adapter = new Movie_listAdapter2(getActivity(),moviename,movieyear,moviebanner);
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();

    return ll;
}

@Override
public void onClick(View view) {

}

@Override
public void onPause() {
    super.onPause();
}

@Override
public void onResume() {
    super.onResume();
}

/*@Override
public void onButtonClickListnerProceed(Integer pos)  {
    Intent booking_screen_redirect = new Intent(getActivity(), BookingScreen2.class);
    booking_screen_redirect.putExtra("movie_name",moviename.get(pos));
    this.onPause();
    startActivity(booking_screen_redirect);
    getActivity().overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);

}*/

}

Movie_listAdapter2.java

  public class Movie_listAdapter2 extends  RecyclerView.Adapter<Movie_listAdapter2.Movie_cards>{

final static String TAG="Movie_listAdapter2";
Context context_external;
ArrayList<String> moviename=new ArrayList<>();
ArrayList<String> movieyear=new ArrayList<>();
ArrayList<String> banner=new ArrayList<>();
public Movie_listAdapter2(Context context,ArrayList<String> resource,    ArrayList<String> resource1, ArrayList<String> resource2) {
    //set the context and the format of the layout inside the list item
    //i.e R.layout.movie_list_adapter
    //also the resource i.e the item names in an ArrayList
    this.moviename = resource;
    this.movieyear = resource1;
    this.banner = resource2;
    this.context_external=context;
    Log.e(TAG,"size = "+movieyear.size()+" "+moviename.size());


}
public static class Movie_cards extends RecyclerView.ViewHolder {

    CardView cv;
    TextView movie_name;
    TextView movie_year;
    Button proceed;
    ImageView movie_banner;

    public Movie_cards(View itemView) {
        super(itemView);
        cv = (CardView)itemView.findViewById(R.id.movie_list_card);
        movie_name = (TextView)itemView.findViewById(R.id.tv_card_moviename);
        movie_year = (TextView)itemView.findViewById(R.id.tv_card_movieyear);
        movie_banner = (ImageView)itemView.findViewById(R.id.card_moviebanner);
        proceed = (Button) itemView.findViewById(R.id.b_card_proceed);
    }
}
@Override
public Movie_cards onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_list_card, parent, false);
    Movie_cards pvh = new Movie_cards(v);
    return pvh;
}

public void updateData(ArrayList<String> resource, ArrayList<String> resource1, ArrayList<String> resource2) {
    this.moviename = resource;
    this.movieyear = resource1;
    this.banner = resource2;
    notifyDataSetChanged();
}

@Override
public void onBindViewHolder(final Movie_cards holder, final int position) {
    holder.movie_name.setText(moviename.get(position));
    holder.movie_year.setText(movieyear.get(position));
    Picasso.with(context_external)
            .load(banner.get(position))
            .placeholder(R.drawable.placeholder)
            .networkPolicy(NetworkPolicy.OFFLINE)
            .error(R.drawable.placeholder)
            .fit()
            .into(holder.movie_banner, new Callback() {
                @Override
                public void onSuccess() {
                    //i.e image was loaded from cache
                }

                @Override
                public void onError() {
                    //image was downloaded as it wasn't available in cache
                    Picasso.with(context_external)
                            .load(banner.get(position))
                            .placeholder(R.drawable.placeholder)
                            .error(R.drawable.warning)
                            .fit()
                            .into(holder.movie_banner);
                }
            });
    holder.proceed.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent booking_screen_redirect = new Intent(context_external, BookingScreen2.class);
            booking_screen_redirect.putExtra("movie_name",moviename.get(position));
            context_external.startActivity(booking_screen_redirect);
            //overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);

        }
    });
}



@Override
public int getItemCount() {
    return 0;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}



 }

enter image description here

片段显示为空,即使我已将数据添加到arraylists并传递给适配器,也不会显示任何项目。请帮忙 。提前谢谢

1 个答案:

答案 0 :(得分:2)

问题在于您返回的以下代码0,这就是为什么它没有显示任何项目

@Override
  public int getItemCount() {
  return 0;
}

你应该在这里返回列表的大小moviename.size();或movieyear.size()