过去几天我一直在尝试保存并加载ArrayList
个字符串,而我似乎无法让它工作。没有错误,但它甚至不再向ListView添加项目。我也不认为它也在拯救。请帮忙?
package com.evolvr.productivityapp;
import android.app.ListActivity;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.io.*;
import java.util.ArrayList;
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();
}
}
}
答案 0 :(得分:0)
您可以在传递
之前检查列表项是否为空或为空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();
}
答案 1 :(得分:-1)
尝试从此示例中采用相同的概念:
private ArrayList<String> listOfList = new ArrayList<String>();
private ListView LView;
String someText = "This is some text that will be added";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do_list);
listOfList.add(someText);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listOfList);
LView.setAdapter(adapter); //Adds to the List View
}