0
過去幾天我一直試圖保存和加載字符串ArrayList
,我似乎無法讓它工作。沒有錯誤,但它甚至不會將項目添加到ListView中。我也不認爲它可以挽救。請幫助?保存並檢索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();
}
}
}
的Android版本您使用的? –
我正在使用API 16. – Alex
您正在爲您的列表提供一個空適配器嘗試在讀取對象後移動創建適配器的行。 – Mohamed