简体   繁体   中英

I'm making a simple to do list application.I am able to save the data to file but when I close the application and reopen i'm loosing the data

I'm making a simple to do list application.I am able to save the data to file but when I close the application and reopen i'm loosing the data.I'm storing the data in the file and I cannot retrieve the data when app is closed and reopn

public class MainActivity extends AppCompatActivity {
    EditText toDo;
    ListView listView;
    Button addButton;
    ArrayList<String> item;
    ArrayAdapter<String> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        readItems();

        toDo = (EditText) findViewById(R.id.editText);
        addButton = (Button) findViewById(R.id.addButton);
        listView = (ListView) findViewById(R.id.listView);
        item = new ArrayList<String>();
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, item);
        listView.setAdapter(arrayAdapter);
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String result = toDo.getText().toString();
                arrayAdapter.add(result);
                toDo.setText(" ");
                writeItems();
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                item.remove(position);
                arrayAdapter.notifyDataSetChanged();
                writeItems();
                return true;
            }
        });
    }

    private void readItems() {
        File filesDir = getFilesDir();
        File todoFile = new File(filesDir, "todo.txt");
        int read = -1;

        try {

            FileInputStream inputStream = new FileInputStream(todoFile);
            while ((read = inputStream.read()) != -1) {
                //arrayAdapter.add(String.valueOf((char)read));
                Log.d("TAG", " " + read);
            }
            item = new ArrayList<String>(FileUtils.readLines(todoFile));
        } catch (IOException e) {
            item = new ArrayList<String>();
        }
    }

    private void writeItems() {
        File filesDir = getFilesDir();
        File todoFile = new File(filesDir, "todo.txt");
        try {`enter code here`
            FileUtils.writeLines(todoFile, item);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

in your writeItems method

File todoFile = new File(filesDir, "todo.txt");
FileUtils.writeLines(todoFile, item);

creates new file or override the existing file by deleting all the content

you should use FileWriter class for that which provides you an option to append data in the existing file

Try the following code, it uses FileWriter class.

try{
        String line = "your string content";

        File file =new File("todo-list.txt");

        //if file doesnt exists, then create it
        if(!file.exists()){
            file.createNewFile();
        }

        //true = append file
        FileWriter fileWritter = new FileWriter(file.getName(),true);
            BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
            bufferWritter.write(line);
            bufferWritter.close();

        System.out.println("Done");

    }catch(IOException e){
        e.printStackTrace();
    }

The code is from http://www.mkyong.com/java/how-to-append-content-to-file-in-java/ this website.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM