3

I'm removing images from a gridview when a button is pressed.

The images does remove fine and the gridview also updates with the call "adapter.notifyDataSetChanged();".

When an imageview is removed another image next to this position should take its position. This happens, but the image here won't reload so there's just a blank space? How can I get this imageview to reload its image?

The problem:enter image description here

Here is my gridadapter:

public class FavoriteMovieGridAdapter extends BaseAdapter {
Context context;
ArrayList<DataFavorites> List;
FavoritesMovie fragment;
private static LayoutInflater inflater = null;
FavoriteMovieGridAdapter adapter = this;


public FavoriteMovieGridAdapter(Context context, ArrayList<DataFavorites> List, FavoritesMovie fragment) {
    this.context = context;
    this.List = List;
    this.fragment = fragment;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}



@Override
public int getCount() {
    // TODO Auto-generated method stub
    return List.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

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

    // Avoid unneccessary calls to findViewById() on each row
    final ViewHolder holder;

    /*
     * If convertView is not null, reuse it directly, no inflation
     * Only inflate a new View when the convertView is null.
     */

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.favorite_grid_item, null);

        holder = new ViewHolder();
        holder.poster = (ImageView) convertView.findViewById(R.id.upcoming_image);

        holder.editbutton = (ImageView) convertView.findViewById(R.id.delete_item);
        // The tag can be any Object, this just happens to be the ViewHolder
        convertView.setTag(holder);
    }
    else{

        // Get the ViewHolder back to get fast access to the TextView
        // and the ImageView.
        holder = (ViewHolder) convertView.getTag();

    }


    final View finalConvertView = convertView;

    final DataFavorites e;
    new DataFavorites();
    e = List.get(position);

    String url = String.valueOf(e.getUrl());

    // load image url into poster

// Seems as if this doesn't run for the imageview next to this when this view is removed?              
  Picasso.with(context).load(url).fit().placeholder(R.drawable.movie_back).into(holder.poster);

    // Create onclick and show edit button

    convertView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {


     // show edit button
     holder.editbutton.setVisibility(View.VISIBLE);

     YoYo.with(Techniques.FadeIn).duration(700).playOn(finalConvertView.findViewById(R.id.delete_item));



            // onclick edit button remove item and update gridview
     holder.editbutton.setOnClickListener(new View.OnClickListener() {

     @Override
     public void onClick(View v) {

      YoYo.with(Techniques.ZoomOut).duration(700).playOn(finalConvertView.findViewById(R.id.favorite_relative));


      Handler handler = new Handler();
      handler.postDelayed(new Runnable() {
                        public void run() {


       adapter.List.remove(e);
       adapter.notifyDataSetChanged();


        // Delete specific movie from data base
        DatabaseHandlerMovie db = new DatabaseHandlerMovie(context);
        // Reading all movies
        db.deleteMovie(e);

            }
                    }, 1000);




                }
            });

            return false;
        }
    });


    return convertView;
}

static class ViewHolder {
    ImageView poster;
    ImageView editbutton;
}



}

My grid item layout:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/favorite_relative"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal">



<ImageView android:layout_width="123.3dp"
    android:layout_height="185.3dp"
    android:id="@+id/upcoming_image"
    android:scaleType="fitXY" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/delete_item"
    android:src="@drawable/ic_remove_circle_outline_white_24dp"
    android:tint="@color/colorPrimaryDark"
    android:clickable="true"
    android:visibility="gone"
    android:layout_alignRight="@+id/upcoming_image"
    android:layout_alignEnd="@+id/upcoming_image"/>

    </RelativeLayout>

My grid layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".Favorites"
android:background="#FFFFFF"
android:focusableInTouchMode="true">



<GridView

    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:id="@+id/movies_gridlayout"
    android:paddingRight="-1dp"
    android:paddingEnd="-1dp"
    android:numColumns="3"
    android:background="#ffffff"
    android:visibility="invisible"/>



<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:id="@+id/favorite_none"
    android:text="No favorite movies found"
    android:textSize="15sp"
    android:visibility="gone"
    android:textColor="@color/SecondaryText"
    />


<ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone"
        >
    </ProgressBar>
    </FrameLayout>
Mat0
  • 1,165
  • 2
  • 11
  • 27
  • it depends on how you are storing your images.. if it is a sql database you would delete it from the sql database like this: http://stackoverflow.com/questions/7510219/deleting-row-in-sqlite-in-android#7510399 and in your gridview you would call notifyDataChanged() like shown here: http://stackoverflow.com/questions/4438128/how-to-refresh-a-gridview31162450 – edwinj Oct 06 '15 at 13:47
  • oh wait.. sorry i didnt fully read your question.. in your case, its something to do with your gridview layout.. can you please also show your gridview layout.. – edwinj Oct 06 '15 at 13:48
  • Okay so I've added my grid layouts to the question. Is anything related to this? – Mat0 Oct 06 '15 at 13:52
  • Try to use non static ViewHolder class class ViewHolder { ImageView poster; ImageView editbutton; } – once2go Oct 06 '15 at 14:44
  • This had no effect for the problem – Mat0 Oct 06 '15 at 15:35

2 Answers2

0

Turns out a 3rd-party lib was causing this:

YoYo.with(Techniques.ZoomOut).duration(700).playOn(finalConvertView.findViewById(R.id.favorite_relative));
Mat0
  • 1,165
  • 2
  • 11
  • 27
0

you will have adapter and mainactivity for gridview

So u will have to implement public interface in both of them to communicate

following code may help you :)

In Adapter class add following code

private OnItemClickListner onItemClickListner;
public MyAdapter(Context c, List<PDFDoc> pdfDocs,OnItemClickListner onItemClickListner) {
          this.context = c;
          this.pdfDocs = pdfDocs;
          this.onItemClickListner=onItemClickListner;
        }
     
public interface OnItemClickListner {
         void removefromadapter(int position);
        }
    
ImageButton imageButton = view.findViewById(R.id.cancelid);
imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // write your stuff here
            onItemClickListner.removefromadapter(position);
        }
    });

In MainActivity of gridview add following code

implements OnItemClickListner required**

public class addclient_fragment extends Fragment implements MyAdapter.OnItemClickListner {

you will required to add this method in mainactivity

@Override
    public void removefromadapter(int position) {
        // write your stuff here
    }
Vaibhav Pallod
  • 466
  • 7
  • 21