我环顾四周,有一些类似的问题,但没有一个可行的解决方案。
我创建了一个简单的 ListView 以从 EditText 获取输入并在单击按钮时将其显示在列表中。如果单击列表项,我会弹出一个对话框并要求确认删除。单击“是”时,该项目似乎要删除。虽然如果我然后尝试删除另一个项目,该项目将删除,但前一个项目将重新出现在其位置。我不知道如何解决这个问题。根据我有限的理解,似乎该项目从未从 SharedPreferences 中删除,并且当我在下一个“onClick”上再次加载它们时,旧项目再次出现。虽然我尝试清除 SharedPreferences 并在删除后保存它们,但这没有用。
package com.example.send2omni;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.AndroidCharacter;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ContextsList extends Activity {
public static final String CONTEXTS = "contexts_list";
EditText display;
ListView lv;
public ArrayAdapter<String> adapter;
Button addButton;
String temp_appender;
String appender = "";
List<String> dataset;
String[] splitup;
public String items;
ArrayList<String> itemsarraylist;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contexts_list);
display = (EditText) findViewById(R.id.editText);
lv = (ListView) findViewById(R.id.listView);
addButton = (Button) findViewById(R.id.bAdd);
LoadPreferences();
lv.setAdapter(adapter);
LoadPreferences();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appender = LoadPreferences();
if(display.getText().toString() != null){
temp_appender = display.getText().toString();
String string_to_split = appender + "," + temp_appender;
List<String> items = Arrays.asList(string_to_split.split(","));
adapter = new ArrayAdapter<String>
(getApplicationContext(), android.R.layout.simple_list_item_1, items);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
SavePreferences("LISTS", string_to_split, null);
LoadPreferences();
}
display.setText("");
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
deleteItem(arg1);
}
});
}
protected void deleteItem(final View arg1)
{
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("Delete");
dialogBuilder.setMessage
("Do you want to delete \"" + ((TextView) arg1).getText() + "\"?");
dialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String splitit = LoadPreferences();
List<String> listit = Arrays.asList(splitit.split(","));
ArrayList<String> itemsarraylist = new ArrayList<String>();
for(int i = 0; i <listit.size(); i++){
itemsarraylist.add(i, listit.get(i));
}
try {
adapter.remove(((TextView) arg1).getText().toString());
adapter.notifyDataSetChanged();
itemsarraylist.remove(((TextView) arg1).getText().toString());
try{
SavePreferences("LISTS", null, itemsarraylist);
}catch(Exception e){
}
} catch (Exception e) {
Log.e("remove", "failed");
}
}
});
dialogBuilder.setNeutralButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog deleteDialog = dialogBuilder.create();
deleteDialog.show();
}
protected void SavePreferences(String key, String value, ArrayList<String> opt) {
// TODO Auto-generated method stub
SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
if (opt != null){
for (int i = 0 ; i <= opt.size(); i++)
value += opt.get(i) + ",";
}
SharedPreferences.Editor editor = data.edit();
editor.putString(key, value);
editor.commit();
}
protected String LoadPreferences(){
SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
String dataSet = data.getString("LISTS", "");
dataset = Arrays.asList(dataSet.split(","));
splitup = dataSet.split(",");
List<String> items = Arrays.asList(dataSet.split(","));
ArrayList<String> itemsarraylist = new ArrayList<String>();
for(int i = 0; i <items.size(); i++){
itemsarraylist.add(i, items.get(i));
}
adapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, itemsarraylist);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
return dataSet;
}
}
我已经包含了课程中的所有代码,以防万一你有时间在你的 IDE 中试用它,并且因为工作部分可能对其他初学者有用。
如果有人能帮我解决这个问题,我无法表达我会多么感激。这几乎是我在完成我的第一个应用程序之前必须采取的最后一步,而且我真的不期待为了保存字符串列表而必须学习 SQLite。
谢谢大家的时间。。