2

I have three fragment classes each with it's own custom adapter (I am using these three fragments with a ViewPager.)

//Fragment class 1
static MyListAdapterArtists myListAdapterArtists;

//Fragment class 2
static MyListAdapterTracks myListAdapterTracks;

//Fragment class 3
static MyGridViewAdapter myGridViewAdapter;

All three adapters are based on the same underlying data. I want to call notifyDataSetChanged() on any single one of them with the same class. So I pass them to UpdateAdapters class.

//Fragment class 1
UpdateAdapters.getInstance().setAdapterOne(adapter);

//Fragment class 2
UpdateAdapters.getInstance().setAdapterTwo(adapter);

//Fagment class 3
UpdateAdapters.getInstance().setAdapterThree(gridAdapter);

This is UpdateAdapters class

public class UpdateAdapters {

private static UpdateAdapters instance = null;

private MyListAdapterArtists myListAdapterArtists;
private MyListAdapterTracks myListAdapterTracks;
private MyGridViewAdapter myGridViewAdapter;

//Constructor
protected UpdateAdapters() { }

//Get the current instance
public static UpdateAdapters getInstance() {
    if (instance == null) {
        // create a new one if it doesn't exist
        instance = new UpdateAdapters();
    }
    return instance;
}

//Setters
public void setAdapterOne(MyListAdapterTracks myListAdapterTracks){
    this.myListAdapterTracks = myListAdapterTracks;
}

public void setAdapterTwo(MyListAdapterArtists myListAdapterArtists){
    this.myListAdapterArtists = myListAdapterArtists;
}

public void setAdapterThree(MyGridViewAdapter myGridViewAdapter){
    this.myGridViewAdapter = myGridViewAdapter;
}

//Public methods
public void update(){

        myListAdapterArtists.notifyDataSetChanged();
        myListAdapterTracks.notifyDataSetChanged();
        myGridViewAdapter.notifyDataSetChanged();

}

And I can update any of my adapters separately all like so

UpdateAdapters.getInstance().update();

The views update almost perfectly, except that the views in my list views only update after I scroll them off screen. The grid view's views work fine and update onscreen.

I tried passing the list-views into the update adapters class and invalidating the views like so

adapterTracks.notifyDataSetChanged();   
MyListViewTracks.invalidate();

But that didn't work either.

My adapter classes don't look any different from one another. They are practically the same.

I read here on SO that maybe it's a platform problem and I should create a custom list view. I'm not really sure how to incorporate that suggestion into my code. Does someone want to give it a shot?

Thanks

AdapterGridView class

public class AdapterGridView extends ArrayAdapter<AlbumObject> {


    Context context;
    int layoutResourceId;
    ArrayList<AlbumObject> albumObjectList;

    public AdapterGridView(Context context, int layoutResourceId, ArrayList<AlbumObject> albumObjectList) {

        super(context, layoutResourceId, albumObjectList);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.albumObjectList = albumObjectList;
    }

    static class ViewHolder {

        static ImageView albumArt;
        static TextView albumTitle;
        static TextView artist;
        static TextView title;
        //static TextView data;
        static TextView duration;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // Get the data item for this position
        AlbumObject albumObject = albumObjectList.get(position);

        ViewHolder viewHolder; // view lookup cache stored in tag

        // if an existing view is not being reused
        if (convertView == null) {

            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());

            convertView = inflater.inflate(R.layout.item_grid_view, parent, false);
            convertView.setTag(viewHolder);

            // if an existing view is being reused
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            //viewHolder.albumArt = null;
        }

        viewHolder.albumTitle = (TextView) convertView.findViewById(R.id.gridViewAlbumTitle);
        viewHolder.albumArt = (ImageView) convertView.findViewById(R.id.gridViewAlbumArt);
        viewHolder.albumTitle.setText(albumObject.albumTitle);

        if(albumObject.albumArtURI != null){

            File f = new File(albumObject.songObjectList.get(0).albumArtURI);

            Picasso.with(viewHolder.albumArt.getContext())
                    .load(f)
                    .transform(new CircleTransform())
                    .placeholder(R.drawable.blackcircle).fit().centerCrop()
                    .into(viewHolder.albumArt);
        } else {

            Picasso.with(viewHolder.albumArt.getContext())
                    .load(R.drawable.blackcircle)
                    //.transform(new CircleTransform())
                    .placeholder(R.drawable.blackcircle)
                    .into(viewHolder.albumArt);
        }

        return convertView;
    }
}

AdapterTracks class

public class AdapterTracks extends ArrayAdapter<SongObject>  {

    Context context;
    int layoutResourceId;
    ArrayList<SongObject> songObjectList;
    Activity activity;



    public static RequestQueue mRequestQueue;

    public static RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(MainActivity.getAppContext());
        }
        return mRequestQueue;
    }


    public AdapterTracks(Context ctx, int layoutResourceId, ArrayList<SongObject> songObjectList) {

        super(ctx, layoutResourceId, songObjectList);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.songObjectList = songObjectList;
        this.activity = (Activity) ctx;

    }

    static class ViewHolder {

        static ImageView albumArt;
        static TextView album;
        static TextView artist;
        static TextView title;
        //static TextView data;
        static TextView duration;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // Get the data item for this position
        SongObject songObject = songObjectList.get(position);

        ViewHolder viewHolder; // view lookup cache stored in tag

        // if an existing view is not being reused
        if (convertView == null) {

            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());

            convertView = inflater.inflate(R.layout.item_list_view, parent, false);
            convertView.setTag(viewHolder);

            // if an existing view is being reused
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            //viewHolder.albumArt = null; // Prevents recycling from overlapping list item contents
        }

        viewHolder.artist = (TextView) convertView.findViewById(R.id.artist);
        viewHolder.title = (TextView) convertView.findViewById(R.id.title);
        viewHolder.albumArt = (ImageView) convertView.findViewById(R.id.album_art);
        viewHolder.artist.setText(songObject.artist);
        viewHolder.title.setText(songObject.songTitle);


        if(songObject.albumArtURI != null){

            File f = new File(songObject.albumArtURI);

            Picasso.with(viewHolder.albumArt.getContext())
                    .load(f)
                    .transform(new CircleTransform())
                    .placeholder(R.drawable.blackcircle).fit().centerCrop()
                    .into(viewHolder.albumArt);
        } else {

            Picasso.with(viewHolder.albumArt.getContext())
                    .load(R.drawable.blackcircle)
                    .transform(new CircleTransform())
                    .placeholder(R.drawable.blackcircle)
                    .into(viewHolder.albumArt);
        }

        return convertView;
    }
}
Community
  • 1
  • 1
the_prole
  • 8,275
  • 16
  • 78
  • 163

0 Answers0