1

我似乎无法弄清楚这段代码有什么问题。当我单击列表项打开删除对话框时,它不会做任何事情。虽然如果我重新开始活动,所有项目都将被删除。

 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;

}

}

我已经在这几个小时了,所以如果有人能帮助发现这个错误,我将非常感激。如果您需要我提供更多信息,请告诉我。

4

2 回答 2

0

Hai 在我的项目中我正在删除列表。我是这样使用的。当用户单击列表时。请获取列表的位置并通过对话函数 arg。

lv.remove(positiontoDelete);
于 2013-09-13T07:41:48.857 回答
0

这是答案

原因如下:根据API,列表在添加/删除方面是不可变的。但是在add操作时,您会创建一个新适配器,因此它运行良好。但是当你试图从“不可变”列表中删除一个对象时,你会得到一个错误。

并且请永远不要在catch没有明确解释为什么它是空的情况下使用空块。只是说ignored.printStackTrace();会为您节省大量时间!

于 2013-09-13T04:07:34.303 回答