gpt4 book ai didi

android - 如何删除 fragment ListView 中的项目?

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

我想从 fragment 中删除项目。当长按项目时,操作栏会发生变化并出现删除按钮。我单击删除按钮 该项目似乎已从 ListView 中删除。但是当我重新启动应用程序时,该项目再次出现。

public class ReadFragment extends Fragment {
ListView spinner;
TextView empty;
String selected;
List<String> list;
String[] filenames;

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

View rootView = inflater.inflate(R.layout.fragment_read, container,
false);
spinner = (ListView) rootView.findViewById(R.id.spinner2);

this.spinner.setEmptyView(rootView.findViewById(R.id.emptyElement));
getFilenames();

getactionbar();

return rootView;

}

private void getFilenames() {
// TODO Auto-generated method stub
filenames = getActivity().fileList();
list = new ArrayList<String>();
for (int i = 0; i < filenames.length; i++) {
// Log.d("Filename", filenames[i]);
list.add(filenames[i]);
}
ArrayAdapter<String> filenameAdapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1,
android.R.id.text1, list);

spinner.setAdapter(filenameAdapter);
spinner.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

selected = ((TextView) view).getText().toString();

Intent intent = new Intent(getActivity(), ReadData.class);
intent.putExtra("key", selected);

startActivity(intent);

}
});

}
private void getactionbar() {
// TODO Auto-generated method stub
spinner.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
spinner.setMultiChoiceModeListener(new MultiChoiceModeListener() {

@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.ic_action_discard:

deletefiles();


Toast.makeText(getActivity(), "Deleted", Toast.LENGTH_LONG).show();

mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}

private void deletefiles() {
// TODO Auto-generated method stub
SparseBooleanArray checkedItemPositions = spinner.getCheckedItemPositions();
int itemCount = spinner.getCount();

for(int i=itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
filenameAdapter.remove(list.get(i));
}
}
checkedItemPositions.clear();
filenameAdapter.notifyDataSetChanged();

}


@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}

@Override
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an invalidate() request
return false;
}
});

}
}

任何人都可以帮我永远删除该项目,我的意思是一旦它被删除,当应用程序重新启动时它不应该再次出现在 ListView 中。

最佳答案

试试吧,试试吧

final Button btnRemove = (Button) findViewById(R.id.button1);
btnRemove.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
lst.remove(adapter.getItem(2));
adapter.notifyDataSetChanged();
}
});;

关于android - 如何删除 fragment ListView 中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22148888/

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