0

有什么办法可以从gridview.

我想item从我的gridview.

我没有找到任何东西。我的代码是

public class ImageAdapter extends BaseAdapter{

    Context context;

    public ImageAdapter(Context context)
    {
        this.context = context;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return  mThumbIds.length;
    }

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

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

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

        final ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(0, 5, 0, 0);


        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    public Integer[] mThumbIds = {
            R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,
            R.drawable.sample_3,R.drawable.sample_1,R.drawable.sample_2,
            R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_1

    };

}

/////////////////

public class ImageActivity extends Activity {

    ImageAdapter iAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
         iAdapter = new ImageAdapter(this);
        final GridView gView = (GridView)findViewById(R.id.grid_view);
        gView.setAdapter(iAdapter);
        gView.setOnItemClickListener(new OnItemClickListener() {

             public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                 //gView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                // gView.setItemChecked(position, true);
                 Toast.makeText(ImageActivity.this, "" + position, Toast.LENGTH_SHORT).show();
                }

        });
        iAdapter.notifyDataSetChanged();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_image, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId() == R.id.menu_delete)
        {
            Toast.makeText(this, "Delete",Toast.LENGTH_SHORT ).show();
        }
        return super.onOptionsItemSelected(item);
    }

}

任何人都可以有想法。感谢

4

2 回答 2

1

你正在使用一张桌子:

public Integer[] mThumbIds = {
        R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,
        R.drawable.sample_3,R.drawable.sample_1,R.drawable.sample_2,
        R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_1}

表格不可修改。
将其替换为您将能够进行添加或删除操作的列表。发生更改时只需调用 notifyDataSetChanged 即可让适配器知道其列表已被修改。

于 2013-05-06T11:38:39.683 回答
0

正如所Teovald建议pskink的那样,您不能拥有固定的图像列表,然后实现您正在寻找的删除功能。如果要添加删除,请更改您的设计并确保您的数据集也是可移动的。到目前为止,您尝试的似乎是使用一些非常基本的代码,这些代码很好地显示了基本的 GridView 功能。

试试这个。创建一个返回实际图像的图像数据集类。将图像存储在可以修改的列表中。向此数据集添加 setter/getter,并添加删除特定项目的方法。更改您的图像适配器以使用此新数据集中的图像。每当删除图像时,请在适配器上调用notifyDataSetChanged

祝你好运

于 2014-05-08T19:18:08.427 回答