android - Custom Adapter not populating GridView -


i have made custom adapter populate gridview. doing trying populate gridview 2 different data(that custom) are: movies , favouritemovies. when dridview updated using movies updates fine when favouritemovies not. storing favouritemovies in sqlite db , querying return cursor.the data returns , there no error. main problem moviesadapter. inside when add movies adapter adds list inside moviesadapter , works when add favouritemovies adapter not add inside favouritelist inside moviesadapter , size zero. please help!! here moviesadapter: package com.akshitjain.popularmovies;

import android.content.context; import android.graphics.bitmapfactory; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview;  import com.squareup.picasso.picasso;  import java.util.arraylist; import java.util.list;   public class moviesadapter extends baseadapter {   private context mcontext; list<movies> list = new arraylist<>(); list<favouritemovies> favouritelist = new arraylist<>();  public moviesadapter(context c) {     this.mcontext = c; }  @override public int getcount() {     if (list != null)         return list.size();     else         return favouritelist.size(); }  @override public object getitem(int position) {     if (list != null)         return list.get(position);     else         return favouritelist.get(position); }  @override public long getitemid(int position) {     return 0; }  @override public view getview(int position, view convertview, viewgroup parent) {     if (convertview == null) {         convertview =     layoutinflater.from(mcontext).inflate(r.layout.grid_item_movies, parent, false);     }      imageview movieposter = (imageview) convertview.findviewbyid(r.id.grid_item_movie_image);     if (list != null) {         movies movies = list.get(position);         string posterpath = movies.posterpath;         final string poster_final_url = constants.image_base_url +     constants.poster_size_large + posterpath;              picasso.with(mcontext).load(poster_final_url.trim()).into(movieposter);     } else {         favouritemovies favouritemovies = favouritelist.get(position);         byte[] bb = favouritemovies.posterimage;         movieposter.setimagebitmap(bitmapfactory.decodebytearray(bb, 0,       bb.length));     }      return convertview; }  public void add(movies movies) {     list.add(movies); }  public void offlineadd(favouritemovies favouritemovies) {     favouritelist.add(favouritemovies); }  public void clear() {     list.clear(); }  } 

and here adding adapter:

          @override     protected void     onpostexecute(movies[] strings) {     if (strings != null) {         mmoviesadapter.clear();         (movies moviesstr : strings) {             mmoviesadapter.add(moviesstr);         }     }else if(mfavouritemovies != null){         mmoviesadapter.clear();         for(favouritemovies     favouritemoviesstr : mfavouritemovies){               mmoviesadapter.offlineadd(favouritemoviesstr);         }     }     log.v("fetchmoviestask","count :" +     mmoviesadapter.getitem(0));         mmoviesadapter.notifydatasetchanged(); } 

so sorry crude language.

you have count == 0 if set favorite movies.

list<movies> list = new arraylist<>();  @override public int getcount() {     if (list != null)         return list.size();     else         return favouritelist.size(); } 

this return count of 0 since list never null. favorite movies ignored.

solutions

  • since displaying either movies or favorites should use 2 adapters. if don't want this,
  • use boolean. set showfavorites true, , display those. or if keep null checks,
  • set list<movie> = null; when displaying favorited, work.

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -