gpt4 book ai didi

Android使用上下文操作栏删除选定的项目

转载 作者:太空狗 更新时间:2023-10-29 14:16:23 24 4
gpt4 key购买 nike

到目前为止,我已经成功地关注了this教程并成功运行。但我现在想做的是实际删除 ListView 中的选定项目。这是我正在使用的代码:

private String[] data = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine","Ten"};

private SelectionAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mAdapter = new SelectionAdapter(this,
R.layout.row_list_item, R.id.textView1, data);
setListAdapter(mAdapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {

private int nr = 0;

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}

@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
mAdapter.clearSelection();
}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub

nr = 0;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextual_menu, menu);
return true;
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {

case R.id.item_delete:
nr = 0;
mAdapter.clearSelection();
mode.finish();
}
return true;
}

@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// TODO Auto-generated method stub
if (checked) {
nr++;
mAdapter.setNewSelection(position, checked);
} else {
nr--;
mAdapter.removeSelection(position);
}
mode.setTitle(nr + " selected");

}
});

getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub

getListView().setItemChecked(position, !mAdapter.isPositionChecked(position));
return false;
}
});
}

private class SelectionAdapter extends ArrayAdapter<String> {

private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();

public SelectionAdapter(Context context, int resource,
int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}

public void setNewSelection(int position, boolean value) {
mSelection.put(position, value);
notifyDataSetChanged();
}

public boolean isPositionChecked(int position) {
Boolean result = mSelection.get(position);
return result == null ? false : result;
}

public Set<Integer> getCurrentCheckedPosition() {
return mSelection.keySet();
}

public void removeSelection(int position) {
mSelection.remove(position);
notifyDataSetChanged();
}

public void clearSelection() {
mSelection = new HashMap<Integer, Boolean>();
notifyDataSetChanged();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);//let the adapter handle setting up the row views
v.setBackgroundColor(getResources().getColor(android.R.color.background_light)); //default color

if (mSelection.get(position) != null) {
v.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));// this is a selected position so make it red
}
return v;
}
}

问题是如何删除所选项目?我想不通,如果你们中的任何人知道这一点并且可以提供帮助,我将不胜感激。谢谢。

最佳答案

在您的代码中而不是使用 string [] data你可以使用 ArrayList<String> data因为它是动态大小的数组,其中实际删除了对象并相应地调整了列表(数组)的大小。通过此更改,您可以使用以下方法。

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
List<Integer> keyList = new ArrayList<Integer>(noteAdptr.getCurrentCheckedPosition());

Collections.sort(keyList, new Comparator<Integer>() {

@Override
public int compare(Integer lhs, Integer rhs) {

return lhs.compareTo(rhs);

}
});
Collections.reverse(keyList);

switch (item.getItemId()) {

case R.id.item_delete:
nr = 0;
for (Integer i:keyList){
Log.e("", "items selected is : "+i);
data.remove((int)i);
}
mAdapter .notifyDataSetChanged();
mAdapter.clearSelection();
mode.finish();
}
return true;
}

希望对你有帮助

关于Android使用上下文操作栏删除选定的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21595560/

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