Android从gridview中删除项目

时间:2015-03-24 13:20:49

标签: java android

我正在开发包含gridview的应用程序。我使用包含图像和文本的自定义适配器填充此网格。

adapter = new CustomGrid(this, titlearray, imageId);
         grid=(GridView)findViewById(R.id.gridView1);
           grid.setAdapter(adapter);

当我点击任何项目时,会打开一个包含按钮的对话框,我想要的是当我点击此按钮时该项目将被删除。我检查了很多解决方案,他们说我应该更换适配器或使用新的适配器。

我尝试了以下代码,我将数据放入新数组并将其加载到适配器中,然后在网格中使用它们:

                    String[] ntarray=new String[wcount];    
                    Drawable[] ndarray=new Drawable[wcount];





                if(position==0)
                {
                    for(int i=0;i<wcount;i++)
                    {
                        ntarray[i]=titlearray[i+1];
                        ndarray[i]=imageId[i+1];
                    }
                }


                if(position==wcount-1)
                {
                    for(int i=0;i<wcount-1;i++)
                    {
                        ntarray[i]=titlearray[i];
                        ndarray[i]=imageId[i];
                    }
                }


                 adapter = new CustomGrid(Wishlist.this, ntarray, ndarray);
                   grid.setAdapter(adapter);
                   adapter.notifyDataSetChanged();
                   dialog.hide();

我使用自定义网格适配器:

package com.erc.test;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
public class CustomGrid extends BaseAdapter{
    private Context mContext;
    private final String[] web;
    private final Drawable[] Imageid;
      public CustomGrid(Context c,String[] web,Drawable[] imageId ) {
          mContext = c;
          this.Imageid = imageId;
          this.web = web;
      }
    @Override
    public int getCount() {
      // TODO Auto-generated method stub
      return web.length;
    }
    @Override
    public Object getItem(int position) {
      // TODO Auto-generated method stub
      return null;
    }
    @Override
    public long getItemId(int position) {
      // TODO Auto-generated method stub
      return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      View grid;
      LayoutInflater inflater = (LayoutInflater) mContext
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          if (convertView == null) {
            grid = new View(mContext);
        grid = inflater.inflate(R.layout.grid_single, null);
            Button textView = (Button) grid.findViewById(R.id.grid_text);
            ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
            textView.setText(web[position]);
           // textView.setShadowLayer(2, 1, 1, Color.BLACK);
            imageView.setImageDrawable(Imageid[position]);
          } else {
            grid = (View) convertView;
          }
      return grid;
    }
}

是否有任何函数或任何方法可以帮助我删除此项而不创建新的适配器,如果是,我如何在我的customgrid类中添加它?

1 个答案:

答案 0 :(得分:5)

进行更改:

    ArrayList<String> ntarray= new ArrayList<String>();    
    ArrayList<Drawable> ndarray= new ArrayList<Drawable>();

    if(position==0){
      for(int i=0;i<wcount;i++){
          ntarray.set(i, titlearray[i+1]);
          ndarray.set(i, imageId[i+1]);
      }
    }

   if(position==wcount-1){
      for(int i=0;i<wcount-1;i++){
         ntarray.set(i, titlearray[i]);
         ndarray.set(i, imageId[i]);
      }
   }

   adapter = new CustomGrid(Wishlist.this, ntarray, ndarray);
   grid.setAdapter(adapter);
   adapter.notifyDataSetChanged();
   dialog.hide();

并在您的适配器中:

    public class CustomGrid extends BaseAdapter{
        private Context mContext;
        private final ArrayList<String> web;
        private final ArrayList<Drawable> Imageid;

        public CustomGrid(Context c, ArrayList<String> web, ArrayList<Drawable> imageId ) {
              mContext = c;
              this.Imageid = imageId;
              this.web = web;
          }

        public void removeItem(int position){
             web.remove(position);
             Imageid.remove(position);
             notifyDataSetChanged();
        }

    ....
    }

如果您要删除gridview中的项目,只需致电:adapter.removeItem(TheItemPosition);

<强>更新

您可以在适配器中添加此方法以清除所有项目:

public void clearData(){
      web.clear();
      Imageid.clear();
      notifyDataSetChanged();
}