我似乎无法弄清楚这段代码有什么问题。当我单击列表项以打开删除对话框时,它将不会执行任何操作。虽然如果我重新开始活动,所有项目都将被删除。
public class ContextsList extends Activity {
EditText display;
ListView lv;
public ArrayAdapter<String> adapter;
Button addButton;
String temp_appender;
String appender = "";
List<String> dataset;
String[] splitup;
public String items;
@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);
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) {
items = LoadPreferences();
try {
adapter.remove(((TextView) arg1).getText().toString());
adapter.notifyDataSetChanged();
SavePreferences("LISTS", items);
} catch (Exception ignored) {
}
SavePreferences("LISTS", items);
}
});
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) {
// TODO Auto-generated method stub
SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
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(","));
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
return dataSet;
}
}
我已经在这里工作了几个小时,所以如果有人能帮助发现这个错误,我真的很感激。如果您需要我的更多信息,请告诉我。
答案 0 :(得分:0)
原因如下:根据API,列表在添加/删除方面是不可变的。但是在add
操作中,您创建了一个新的适配器,因此运行良好。但是当您尝试从“不可变”列表中删除对象时,会出现错误。
并且请永远不要使用空的catch
块而没有明确的解释为什么它是空的。只要把ignored.printStackTrace();
放在一边就可以节省你的时间!
答案 1 :(得分:0)
Hai在我的项目中我正在删除列表。我这样用。当用户单击列表时。请获取列表的位置并通过对话框功能arg。
lv.remove(positiontoDelete);