问题
I created an image gallery app which displays images as a gridview. I can select multiple photos at a time but I am not able to delete all of them at once by clicking the delete button. I am not using checkboxes because it doesn't go well with the design of my app. I tried finding a way but almost all of them use checkboxes.Can anyone suggest me how to delete multiple photos at once without using checkboxes?
PhotosActivity.java:
public class PhotosActivity extends AppCompatActivity {
int int_position;
private GridView gridView;
GridViewAdapter adapter;
ArrayList<Model_images> al_menu = new ArrayList<>();
private ArrayList<Object> mSelected = new ArrayList<>();
boolean boolean_folder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_gallery);
final ImageButton button1 = (ImageButton) findViewById(R.id.button1);
final ImageButton button2 = (ImageButton) findViewById(R.id.button2);
final ImageButton button3 = (ImageButton) findViewById(R.id.button3);
final ImageButton button4 = (ImageButton) findViewById(R.id.button4);
final ImageButton button5 = (ImageButton) findViewById(R.id.button5);
button1.setVisibility(View.GONE);
button2.setVisibility(View.GONE);
button3.setVisibility(View.GONE);
button4.setVisibility(View.GONE);
button5.setVisibility(View.GONE);
gridView = (GridView) findViewById(android.R.id.list);
int_position = getIntent().getIntExtra("value", 0);
adapter = new GridViewAdapter(this, al_images, int_position);
gridView.setAdapter(adapter);
gridView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String abc = "file://" + al_images.get(int_position).getAl_imagepath().get(position);
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
i.putExtra("id", position);
i.putExtra("folderPosition", int_position);
i.putExtra("abc", abc);
startActivity(i);
}
});
gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
String position1 = new String(al_images.get(int_position).getAl_imagepath().get(position));
if(mSelected.contains(position1)) {
mSelected.remove(position1);
view.setBackgroundColor(Color.TRANSPARENT);// remove item from list
// update view (v) state here
// eg: remove highlight
}
else {
mSelected.add(position1);
view.setBackgroundColor(Color.LTGRAY);// add item to list
// update view (v) state here
// eg: add highlight
}
button1.setVisibility(View.VISIBLE);
button2.setVisibility(View.VISIBLE);
button3.setVisibility(View.VISIBLE);
button4.setVisibility(View.VISIBLE);
button5.setVisibility(View.VISIBLE);
button3.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(PhotosActivity.this);
builder1.setMessage("Are you sure you want to delete it ?");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
deleteSelectedItems();
al_images.remove(position);
adapter.notifyDataSetChanged();
finish();
}
});
builder1.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
});
return true;
}
});
}
private void deleteSelectedItems() {
SparseBooleanArray checked = gridView.getCheckedItemPositions();
if (checked != null) {
List<Object> list = mSelected;
for (int i = 0; i < checked.size(); i++) {
if (checked.get(i)) {
Object item = list.get(i);
mSelected.remove(item);
checked.delete(i);
}
}
}
}
GridviewAdapter.java
public class GridViewAdapter extends ArrayAdapter<Model_images> {
Context context;
ViewHolder viewHolder;
ArrayList<Model_images> al_menu = new ArrayList<>();
int int_position;
public GridViewAdapter(Context context, ArrayList<Model_images> al_menu,int position) {
super(context, R.layout.activity_adapter__photos_folder, al_menu);
this.al_menu = al_menu;
this.context = context;
this.int_position = position;
}
@Override
public int getCount() {
Log.e("ADAPTER LIST SIZE", al_menu.size() + "");
Log.e("ADAPTER LIST SIZE", al_menu.get(int_position).getAl_imagepath().size() + "");
return al_menu.get(int_position).getAl_imagepath().size();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getViewTypeCount() {
if (al_menu.size() > 0) {
return al_menu.get(int_position).getAl_imagepath().size();
} else {
return 1;
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_adapter__photos_folder, parent, false);
viewHolder.tv_foldern = (TextView) convertView.findViewById(R.id.tv_folder);
viewHolder.tv_foldersize = (TextView) convertView.findViewById(R.id.tv_folder2);
viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tv_foldern.setVisibility(View.GONE);
viewHolder.tv_foldersize.setVisibility(View.GONE);
Glide.with(context).load("file://" + al_menu.get(int_position).getAl_imagepath().get(position))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(viewHolder.iv_image);
return convertView;
}
private static class ViewHolder {
TextView tv_foldern, tv_foldersize;
ImageView iv_image;
}
}
回答1:
You can do like that
In Activity -
public class PhotosActivity extends AppCompatActivity {
...
private ArrayList<Integer> mSelected = new ArrayList<>();
...
gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
if(mSelected.contains(position)) {
mSelected.remove(position);
view.setBackgroundColor(Color.TRANSPARENT);// remove item from list
// update view (v) state here
// eg: remove highlight
}
else {
mSelected.add(position);
view.setBackgroundColor(Color.LTGRAY);// add item to list
// update view (v) state here
// eg: add highlight
}
.....
.....
.....
}
now call method of adapter as on delete btn click -
adapter.updateUpdapter(mSelecected);
In Adapter -
public GridViewAdapter(Context context, ArrayList<Model_images> al_menu,int position) {
super(context, R.layout.activity_adapter__photos_folder, al_menu);
this.al_menu.addAll(al_menu);
....
}
private void updateUpdapter(ArrayList<Integer> mSelected){
for(int selected: mSelected){
al_menu.get(int_position).getAl_imagepath().remove(selected);
}
notifiDataSetChanged();
}
I hope it will help .
回答2:
I think it is because you only remove one position from the adapter.
...
public void onClick(DialogInterface dialog, int id) {
deleteSelectedItems();
al_images.remove(position);
adapter.notifyDataSetChanged();
...
I can't test the code at the moment,but try:
private void deleteSelectedItems() {
SparseBooleanArray checked = gridView.getCheckedItemPositions();
if (checked != null) {
List<Object> list = mSelected;
for (int i = 0; i < checked.size(); i++) {
if (checked.valueAt(i)) {
mSelected.remove(checked.keyAt(i));
al_images.remove(checked.keyAt(i));
}
}
}
}
回答3:
Select and delete multiple items :
The moment you select the item store the position or the unique value of that item in an array or list.
After selecting the item/s, when user presses the
Delete
button loop through your main items and remove the selected item from it.Refresh the Adapter by calling
notifyDataSetChanged();
//based on item position List < Integer > selectedItems = new ArrayList < > ();
public void onSelected(int pos) { selectedItems.add(pos); }
public deleteItems() { for (Integer pos = selectedItems) { mainItems.remove((int)pos); } adapter.notifyDataSetChanged(); }
来源:https://stackoverflow.com/questions/48075320/how-to-delete-multiple-gridview-items-at-once