Saving of ArrayList to present [duplicate]

1

I've been trying to save and load an ArrayList of strings. I do not work.Even I do not see any errors too. Please.

public class toDoList extends ListActivity {
private Button done;
private ListView theList;
private EditText listItem;
private ArrayList<String> listOfList = new ArrayList<String>();
private ArrayAdapter<String> adapter;


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

    done = (Button) findViewById(R.id.doneBtn);
    theList = (ListView) findViewById(android.R.id.list);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listOfList);

    FileInputStream fis;
    ObjectInputStream ois;

    done.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listItem = (EditText) findViewById(R.id.taskTextView);
            if (listItem.getText().toString() == "") {

            } else { //adding items to the listview
                listOfList.add(listItem.getText().toString());
                listItem.setText("");
                adapter.notifyDataSetChanged();
            }
        }
    });
    setListAdapter(adapter);
    try{
        fis = openFileInput("ToDo");
        ois = new ObjectInputStream(fis);
        listOfList = (ArrayList<String>) ois.readObject();
    }catch(IOException e){
        Log.e("ToDoInput", "Can't import todo");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }


}

@Override
protected void onPause(){
    super.onPause();
    FileOutputStream fos;
    ObjectOutputStream ous = null;

    try{
        fos = openFileOutput("ToDo", Activity.MODE_PRIVATE);
        ous = new ObjectOutputStream(fos);
        ous.writeObject(listOfList);
    }catch(IOException e){
        Log.e("ToDo", "Something wrong with output?");
    }finally {
        try{
            if(ous != null) {
                ous.close();
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }


}
    
asked by 19.04.2016 в 15:40
source

2 answers

1

This line is what you really want if (listItem.getText().toString() == "") { maybe look for something like this if(listItem.getText().toString().equals("")){

This == compares "reference" not the value. (but I do not know if this is what you are looking for or the change solves your problem but you can test if the error is there).

    
answered by 19.04.2016 в 15:56
1

Try this, check that listItem is not null and also not empty.

done.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        listItem = (EditText) findViewById(R.id.taskTextView);
        if (isEditTextNotNull(listItem)) {
            //adding items to the listview
            listOfList.add(listItem.getText().toString());
            listItem.setText("");
            adapter.notifyDataSetChanged();
        }
    }
});

private boolean isEditTextNotNull(EditText listItem) {
  return listItem.getText().toString()!=null && !listItem.getText().toString().isEmpty();
}
    
answered by 19.04.2016 в 16:17