我正在對gridview
工作,這裏是我的要求從gridview一次性刪除多個選定的圖像項目delete button
按。從GridView一次按鈕刪除多個圖像按
意味着我應該能夠按刪除按鈕刪除選定的圖像。
我試了很多,但沒有得到這個想法。
我可以一次刪除一個圖像。
這就是我所做的。
//活動
public class ImageActivity extends Activity {
ImageAdapter iAdapter;
ArrayList<Integer>myList;
int position;
GridView gView;
boolean selected = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
dataImage();
iAdapter = new ImageAdapter(this,myList);
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.setSelection(position);
gView.setSelected(true);
Toast.makeText(ImageActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
@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)
{
if(gView.isSelected())
{
iAdapter.list.remove(iAdapter.getPosition());
iAdapter.notifyDataSetChanged();
Toast.makeText(this, "Delete" ,Toast.LENGTH_SHORT).show();
}
}
gView.setSelected(false);
return super.onOptionsItemSelected(item);
}
public void dataImage()
{
myList = new ArrayList<Integer>();
myList.add(R.drawable.sample_1);
myList.add(R.drawable.sample_3);
myList.add(R.drawable.sample_2);
myList.add(R.drawable.sample_2);
myList.add(R.drawable.sample_1);
myList.add(R.drawable.sample_3);
myList.add(R.drawable.sample_1);
myList.add(R.drawable.sample_3);
myList.add(R.drawable.sample_2);
}
}
//適配器
public class ImageAdapter extends BaseAdapter{
Context context;
ArrayList<Integer> list;
int position;
ImageView imageView;
ImageActivity act;
public ImageAdapter(Context context,ArrayList<Integer>list)
{
this.list = list;
this.context = context;
act = (ImageActivity)context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
this.position = position;
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
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(list.get(position));
return imageView;
}
public int getPosition()
{
return position;
}
}
//
任何一個可以指導我,或者幫助我。
我的要求: - 如何從gridview
在同一時間按下按鈕刪除刪除多張圖像。
對於任何樣品... – Unknown 2013-05-07 11:36:55