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