gpt4 book ai didi

java - 在android中删除ListView(cab)中的多个选定项目

转载 作者:行者123 更新时间:2023-12-02 05:45:35 24 4
gpt4 key购买 nike

我想删除 ListView (Gmail 样式)中的多个选定项目(行)。如果我选择(使用长单击)一行,则不会发生任何情况。我在互联网上找到了这段代码,并尝试将其插入到我的项目中。

主要 Activity

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_lista, container,
false);

Lista = (ListView) rootView.findViewById(R.id.Lista);
items = new ArrayList<ListViewItem>();
items = GetLists.GetRecordList(rootView.getContext());
adapter = new ListaAdapter(rootView.getContext(),
R.layout.list_view_item, items);
Lista.setAdapter(adapter);
Lista.setMultiChoiceModeListener(this);
Lista.setChoiceMode(Lista.CHOICE_MODE_MULTIPLE_MODAL);

return rootView;
}

@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menu) {
switch (menu.getItemId()) {
case R.id.menu_delete:
SparseBooleanArray selected = adapter.getSelectedIds();
for (int i = (selected.size() - 1); i >= 0; i--) {
if (selected.valueAt(i)) {
ListViewItem selectedItem = adapter.getItem(selected
.keyAt(i));
adapter.remove(selectedItem);
}
}
actionMode.finish();
return true;

default:
return false;
}
}

@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
actionMode.getMenuInflater().inflate(R.menu.delete_menu, menu);
return false;
}

@Override
public void onDestroyActionMode(ActionMode arg0) {
adapter.removeSelection();

}

@Override
public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
return false;
}

@Override
public void onItemCheckedStateChanged(ActionMode actionMode, int position,
long arg2, boolean arg3) {
final int checkedOut = Lista.getCheckedItemCount();
actionMode.setTitle(checkedOut + " selezionato");
adapter.toggleSelection(position);

}

这是适配器

public class ListaAdapter extends ArrayAdapter<ListViewItem> {

private Context context;
private ArrayList<ListViewItem> items;
private SparseBooleanArray mSelectedItemsIds;

public ListaAdapter(Context context, int resourceId,
ArrayList<ListViewItem> items) {
super(context, resourceId, items);
mSelectedItemsIds = new SparseBooleanArray();
this.context = context;
this.items = items;
}

private class ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView txtSubTitle;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_view_item, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView
.findViewById(R.id.TitoloPrincipale);
holder.txtSubTitle = (TextView) convertView
.findViewById(R.id.Sottotitolo);
holder.imageView = (ImageView) convertView.findViewById(R.id.Image);
convertView.setTag(holder);

} else {
holder = (ViewHolder) convertView.getTag();
}
ListViewItem list = getItem(position);
holder.txtTitle.setText(list.getNomeFarmaco());
holder.txtSubTitle.setText(list.getFasceOrarie());
holder.imageView.setImageResource(list.getIcon());
// convertView
// .setBackgroundColor(mSelectedItemsIds.get(position)
// : Color.TRANSPARENT);
return convertView;

}

public void remove(ListViewItem item) {
items.remove(item);
notifyDataSetChanged();
}

public void toggleSelection(int position) {
selectView(position, !mSelectedItemsIds.get(position));
}

public void removeSelection() {
mSelectedItemsIds = new SparseBooleanArray();
notifyDataSetChanged();
}

public void selectView(int position, boolean value) {
if (value)
mSelectedItemsIds.put(position, value);
else
mSelectedItemsIds.delete(position);

notifyDataSetChanged();
}

public int getSelectedCount() {
return mSelectedItemsIds.size();
}

public SparseBooleanArray getSelectedIds() {
return mSelectedItemsIds;
}

你有什么想法吗?谢了

最佳答案

该代码与长按无关。唯一出现的“long”是方法的long 参数。 :-)

This sample project演示基于长按列表行启动的操作模式。关键是onItemLongClick():

  @Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setItemChecked(position, true);

return(true);
}

在这里(由于将 Activity 注册为 ListViewOnItemLongClickListener),我们打开 CHOICE_MODE_MULTIPLE_MODAL 并检查较长的项目- 单击,从而激 Activity 作模式。

关于java - 在android中删除ListView(cab)中的多个选定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24099454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com