我正在使用SharedPreferences
存储列表视图项,如下所示。但是,即使我从SharedPreferences
中删除了listview项,也不会删除它。即使我删除了该项目,它仍会继续出现。我该如何解决?
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);
modify=(Button)findViewById(R.id.button10);
delete=(Button)findViewById(R.id.button11);
final String arr[]=new String[10];
sp = getSharedPreferences(USER_PREF, Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = sp.edit();
list.add(s3);
editor.putString(KEY_NAME, s3);
editor.apply();
editor.commit();
adapter = new ArrayAdapter<String>(modify_events_3.this,
android.R.layout.simple_list_item_multiple_choice, list);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);
modify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clicked=true;
SparseBooleanArray checked =
listView.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<String>();
for( i=0;i<checked.size();i++) {
int position;
position = checked.keyAt(i);
if (checked.valueAt(i))
selectedItems.add(adapter.getItem(position));
for ( j = 0; j < arr.length; j++) {
str = listView.getItemAtPosition(i).toString();
str = arr[j];
}
}
startActivity(new
Intent(modify_events_3.this,Pop_up.class));
Toast.makeText(getApplicationContext(), "Have anything
else in mind? ;)", Toast.LENGTH_SHORT).show();
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkedItemPositions = listView.getCheckedItemPositions();
int itemCount = listView.getCount();
for( i=itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
str=list.get(i);
adapter.remove(str);
}
}
checkedItemPositions.clear();
adapter.notifyDataSetChanged();
editor.remove(KEY_NAME);
listView.setAdapter(adapter);
}
});
editor.apply();
editor.commit();
}
答案 0 :(得分:1)
尝试从notifiyDatasetChanged()
中删除项目,然后执行ListView
ArrayList
显示一些存储在ArrayList
中的数据。
在notifyDatasetChanged()
中删除或添加元素时,必须告诉列表数据源已更改以显示新数据。
因此,这就是ListView
的来源。它告诉{{1}}数据已被修改,因此请反映已更改的数据。