如何永久删除列表视图项?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何永久删除列表视图项?相关的知识,希望对你有一定的参考价值。
我有这个代码,listview项目从列表中删除,但在我重新加载listview后,该项目再次出现。如何永久删除该项目?请帮我。
删除listview项的代码:
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkedItemPositions = listView.getCheckedItemPositions();
int itemCount = listView.getCount();
for(int i=itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
str=list.get(i);
adapter.remove(str);
}
}
checkedItemPositions.clear();
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
}
});
编辑:正如@Nirup Iyer所说,这是我的完整代码:
package com.example.myapplication;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import static com.example.myapplication.Pop_up.s2;
import static com.example.myapplication.Pop_up.text1;
import static com.example.myapplication.Pop_up.thedate;
import static com.example.myapplication.Pop_up_2.aManager;
import static com.example.myapplication.Pop_up_2.dat;
import static com.example.myapplication.Pop_up_2.dite;
import static com.example.myapplication.Pop_up_2.pIntent;
import static com.example.myapplication.Pop_up_2.pqr;
import static com.example.myapplication.Pop_up_2.s;
import static com.example.myapplication.modify_events.st;
import static com.example.myapplication.modify_events_1.st1;
import static com.example.myapplication.modify_events_2.st2;
public class modify_events_3 extends nav_draw {
ListView listView;
TextView textView;
ArrayAdapter<String> adapter;
String day,month,year;
Button b1,b2;
String str=null;
List<String> list = new ArrayList<String>();
SparseBooleanArray checkedItemPositions;
int i,j;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.events_week);
listView = (ListView) findViewById(R.id.listView);
textView = (TextView) findViewById(R.id.textView);
b1=(Button)findViewById(R.id.button10);
b2=(Button)findViewById(R.id.button11);
final String arr[]=new String[10];
if(dite==null)
{
startActivity(new Intent(modify_events_3.this,nav_draw.class));
Toast.makeText(getApplicationContext(),"Please create an event first", Toast.LENGTH_SHORT).show();
}else {
DateFormat format2 = new SimpleDateFormat("EEE");
DateFormat format3 = new SimpleDateFormat("MMM");
DateFormat format4 = new SimpleDateFormat("yyyy");
day = format2.format(dite);
month = format3.format(dite);
year = format4.format(dite);
list.add(pqr);
adapter = new ArrayAdapter<String>(modify_events_3.this, android.R.layout.simple_list_item_multiple_choice, list);
//Toast.makeText(getApplicationContext(), day + "" + month + "" + year, Toast.LENGTH_LONG).show();
}
if (((st2.equals(day)) && (st1.equals(month)) && (st.equals(year)))) {
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SparseBooleanArray checked = listView.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<String>();
for(int i=0;i<checked.size();i++) {
int position;
position = checked.keyAt(i);
if (checked.valueAt(i))
selectedItems.add(adapter.getItem(position));
for (int j = 0; j < arr.length; j++) {
str = listView.getItemAtPosition(i).toString();
str = arr[j];
}
}
startActivity(new Intent(modify_events_3.this,Pop_up.class));
Intent incoming = getIntent();
String dat = incoming.getStringExtra("date");
thedate.setText(dat);
text1.setText(str);
Toast.makeText(getApplicationContext(), "Have anything else in mind? ;)", Toast.LENGTH_SHORT).show();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkedItemPositions = listView.getCheckedItemPositions();
int itemCount = listView.getCount();
for(int i=itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
str=list.get(i);
adapter.remove(str);
}
}
checkedItemPositions.clear();
adapter.notifyDataSetChanged();
adapter=null;
listView.setAdapter(adapter);
aManager.cancel(pIntent);
Toast.makeText(getApplicationContext(), "The alarm has been deleted successfully", Toast.LENGTH_SHORT).show();
startActivity(new Intent(modify_events_3.this,nav_draw.class));
}
});
} else {
adapter=null;
listView.setAdapter(adapter);
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "No event was created", Snackbar.LENGTH_LONG);
snackbar.show();
}
}
}
我是否必须在文件中写入该项目?
答案
根据您的说法和查看代码,该项目将重新出现在列表中。这是因为,当您重新加载活动时,将再次调用onCreate()方法,并执行onCreate()中的整个代码。这意味着再次创建列表,再次初始化适配器并再次填充列表视图。您对列表所做的任何更改都将持续到活动保持活动状态。
如果您希望在活动消失后删除已删除的项目,则可以将列表项写入持久存储(数据库,SharedPreferences或内部/外部存储)(更多详细信息here)。这样,您所做的任何修改都可以保存在存储中,并在onCreate方法中从那里读回。
以上是关于如何永久删除列表视图项?的主要内容,如果未能解决你的问题,请参考以下文章
片段 A 的列表视图中的片段 B 中的新列表视图,单击 A 的列表项