Android - ArrayAdapter,ArrayList和文件编写器未正确更新

时间:2016-08-02 02:51:49

标签: java android arraylist android-arrayadapter android-file

以下是我尝试创建的Android应用中的活动代码。它是一个"待办事项列表"可以通过动态调整(添加/删除)并保存到文件中。在测试时,我发现以下错误(1)我似乎无法纠正,我有一个问题(2):

  1. 前几个"添加"列表显示为重复。删除adaptor.add vs list.add没有修复它。如果我离开视图并返回,则在您添加"时复制。停止。但是,列表存储的文件具有重复项。当我尝试"删除"列表中的项目,但我认为重复导致问题。如何停止复制?
  2. 如何使用视图中的当前列表完全覆盖文件中的数据?
  3. public class ToDo extends ListActivity {
    
    /** Items entered by the user is stored in this ArrayList variable */
    ArrayList list = new ArrayList();
    
    /** Declaring an ArrayAdapter to set items to ListView */
    ArrayAdapter adapter;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        /** Setting a custom layout for the list activity */
        setContentView(R.layout.content_to_do);
    
        /** Reference to the add button of the layout main.xml */
        Button btn = (Button) findViewById(R.id.btnAdd);
    
        /** Reference to the delete button of the layout main.xml */
        Button btnDel = (Button) findViewById(R.id.btnDel);
    
        Button btnClr = (Button) findViewById(R.id.btnClr);
    
        /** Defining the ArrayAdapter to set items to ListView */
    
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, list);
    
        File file = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS), "TODOList.ser");
        if(file.exists()) {
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                try {
                    // create a file in downloads directory
                    FileInputStream fis = new FileInputStream(
                            new File(Environment.getExternalStoragePublicDirectory(
                                    Environment.DIRECTORY_DOWNLOADS), "TODOList.ser")
                    );
                    ObjectInputStream is = new ObjectInputStream(fis);
                    list = (ArrayList<String>) is.readObject();
                    System.out.println(list);
    
                    Iterator<String> it = list.iterator();
                    while (it.hasNext()) {
                        System.out.println("Data is "+ it.next());
                        adapter.add(it.next());
                    }
    
                    is.close();
                    Log.v("MyApp","File has been read");
                } catch(Exception ex) {
                    ex.printStackTrace();
                    Log.v("MyApp","File wasn't read");
                }
            }
        }
        /** Defining a click event listener for the button "Add" */
        OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText edit = (EditText) findViewById(R.id.txtItem);
                list.add(edit.getText().toString());
                adapter.add(edit.getText().toString());
                edit.setText("");
                adapter.notifyDataSetChanged();
    
                if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                    try {
                        // create a file in downloads directory
                        FileOutputStream fos =
                                new FileOutputStream(
                                        new File(Environment.getExternalStoragePublicDirectory(
                                                Environment.DIRECTORY_DOWNLOADS), "TODOList.ser")
                                );
                        ObjectOutputStream os = new ObjectOutputStream(fos);
                        os.writeObject(list);
                        os.close();
                        Log.v("MyApp","File has been written");
                    } catch(Exception ex) {
                        ex.printStackTrace();
                        Log.v("MyApp","File didn't write");
                    }
                }
    
            }
        };
    
        /** Defining a click event listener for the button "Delete" */
        OnClickListener listenerDel = new OnClickListener() {
            @Override
            public void onClick(View v) {
                /** Getting the checked items from the listview */
                SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
                int itemCount = getListView().getCount();
    
                for(int i=itemCount-1; i >= 0; i--){
                    if(checkedItemPositions.get(i)){
                        adapter.remove(list.get(i));
                        list.remove(i);   //try to adjust array list before write
                    }
                }
                checkedItemPositions.clear();
                adapter.notifyDataSetChanged();
                if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                    try {
                        // create a file in downloads directory
                        FileOutputStream fos =
                                new FileOutputStream(
                                        new File(Environment.getExternalStoragePublicDirectory(
                                                Environment.DIRECTORY_DOWNLOADS), "TODOList.ser")
                                );
                        ObjectOutputStream os = new ObjectOutputStream(fos);
                        os.writeObject(list);
                        os.close();
                        Log.v("MyApp","File has been written");
                    } catch(Exception ex) {
                        ex.printStackTrace();
                        Log.v("MyApp","File didn't write");
                    }
                }
            }
        };
    
    
        /** Defining a click event listener for the button "Delete" */
        OnClickListener listenerClr = new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                    try {
                        File clearit = new File(Environment.getExternalStoragePublicDirectory(
                                                Environment.DIRECTORY_DOWNLOADS), "TODOList.ser");
                        clearit.delete();
                        adapter.clear();
                        Log.v("MyApp","File has been deleted");
                    } catch(Exception ex) {
                        ex.printStackTrace();
                        Log.v("MyApp","File didn't write");
                    }
                }
            }
        };
    
    
        /** Setting the event listener for the add button */
        btn.setOnClickListener(listener);
    
        /** Setting the event listener for the delete button */
        btnDel.setOnClickListener(listenerDel);
    
        /** Setting the event listener for the clear button */
        btnClr.setOnClickListener(listenerClr);
    
        /** Setting the adapter to the ListView */
        setListAdapter(adapter);
    
        }
    
    }
    

0 个答案:

没有答案