A
A
arlanchik52018-03-05 20:25:41
Android
arlanchik5, 2018-03-05 20:25:41

How to remove item from GridView array?

Hello everyone, in my fragment, the photos are loaded using picasso and placed in the GridView . When you long press on one of the photos, a menu appears with the inscription delete, what method do I need to enter for such an element removal?

public class FragmentMems extends android.support.v4.app.Fragment {
    //константа для меню и удленения фоток
    final int MENU_DELETE_ID=1;
    private GridView gridView;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_user_mems, container, false);
        gridView = (GridView) rootView.findViewById(R.id.gridview);
        registerForContextMenu(gridView);
        gridView.setAdapter(new GridViewAdapter(getContext()));
        //при нажатии открывается новая активити с fullimage
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent i =new Intent(getContext(),ZoomActivity.class);
                i.putExtra("id",position);
                startActivity(i);

            }
        });
        return rootView;   }
    //меню при долгом нажатии
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    switch (v.getId()){
        case R.id.gridview:
        menu.add(0,MENU_DELETE_ID,0,"Удалить");
        break;
    }
    super.onCreateContextMenu(menu,v,menuInfo);
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case MENU_DELETE_ID:
                //?????
                break;
        }
        return super.onContextItemSelected(item);
    }
}

public class GridViewAdapter extends BaseAdapter {
    private Context context;
    public String[] items = {
            "http://topmemas.top/img/img/1517414160.jpg",
            "http://topmemas.top/img/img/1518633309.jpg",
            "http://topmemas.top/img/img/1517859960.jpg",
            

    };

    public GridViewAdapter(Context context){
        this.context=context;

    }
    @Override
    public int getCount() {
        return items.length;
    }
    @Override
    public Object getItem(int position) {
        return items[position]; }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertview, ViewGroup parent) {
        ImageView img;
        if (convertview == null){
            img = new ImageView(context);
            convertview = img;
            img.setPadding(10,10,10,10);

        }else {
            img =(ImageView)convertview;
        }

        Picasso.with(context).load(items[position]).placeholder(R.drawable.image)
                .resize(320,320).noFade()
                .into(img, new Callback() {
                    @Override
                    public void onSuccess() {
                    }
                    @Override
                    public void onError() {
                        Toast.makeText(context, "Ошибка загрузки", Toast.LENGTH_SHORT).show();

                    }
                });
        return convertview;
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nekrasov, 2018-03-05
@LemonRX

Since the dimension of an array in java is determined when an array is created, you cannot simply remove an array element, and therefore remove an element from the GridView, you should use ArrayList instead of a regular array
After you remove the element (remove (position)) ArrayList you need to call the method notifyDataSetChanged()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question