我想尝试用一小组 TextViews 创建一个 TableRow,每个 TextViews 都保存一个原始字符串数据。我想,也许如果我存储这些 TableRows 的 ArrayList,并且如果成功地来回读/写,我会简化很多时间来在 Android 屏幕上创建数据表或图表。
从表面上看,我觉得我必须创建很多 View 的子类,并为所有这些实现 Serializable。这完全超出了我的能力。
我的主要问题是:是否可以将一组 TextView 存储到 TableRow 中,创建一个充满 TableRows 的数组,并将它们中的每一个单独添加到 TableLayout 中?
到目前为止,我无法超越使用 ObjectOutputStream 保存数据,因为它不断抛出 NotSerializableException、InvalidAccessException、IllegalAccessException、EOFException、IOException 等各种不同的异常。
如果我的概念不是很好,请问我应该在哪里重新考虑?我应该改变什么,我应该如何存储数据组?提前致谢。
事不宜迟,这是我用来尝试保存 TableRows 的 ArrayList 的代码,以及我在过去 12 小时内不停地工作的所有笨蛋。
package tt.tt;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class Test extends Activity implements OnClickListener, Serializable {
private static final long serialVersionUID = 2466231508464769438L;
public class Format implements Serializable {
private static final long serialVersionUID = 1L;
public String name;
public long points;
public int position;
}
List<Format> formats = new ArrayList<Format>();
TableRow row;
TableLayout layout;
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.table);
layout = (TableLayout) this.findViewById(R.id.table);
Button make = (Button) this.findViewById(R.id.make);
make.setOnClickListener(this);
Button retrieve = (Button) this.findViewById(R.id.retrieve);
retrieve.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.make:
make();
break;
case R.id.retrieve:
retrieve();
break;
}
}
public void make() {
Random r = new Random();
Format f = new Format();
f.name = r.toString();
f.points = r.nextLong();
f.position = r.nextInt(10);
formats.add(f);
FileOutputStream out;
try {
out = this.openFileOutput("format", Activity.MODE_PRIVATE);
ObjectOutputStream output = new ObjectOutputStream(new BufferedOutputStream(out));
output.writeObject(formats);
output.flush();
output.close();
}
catch (FileNotFoundException e) {
Log.d("Test", "Can't save data", e);
}
catch (IOException e) {
Log.d("Test", "Can't save data", e);
}
TextView text = new TextView(this);
text.setText(Integer.toString(f.position));
row = new TableRow(this);
row.addView(text, 0, TableRow.LayoutParams.WRAP_CONTENT);
text = new TextView(this);
text.setText(f.name);
row.addView(text, 1, TableRow.LayoutParams.WRAP_CONTENT);
text = new TextView(this);
text.setText(Long.toString(f.points));
row.addView(text, 2, TableRow.LayoutParams.WRAP_CONTENT);
layout.addView(row);
}
public void retrieve() {
layout.removeAllViews();
FileInputStream in;
try {
in = this.openFileInput("format");
ObjectInputStream input = new ObjectInputStream(new BufferedInputStream(in));
List<Format> temp = (List<Format>) input.readObject();
input.close();
}
catch (FileNotFoundException e) {
Log.d("Test", "Can't load data", e);
}
catch (StreamCorruptedException e) {
Log.d("Test", "Can't load data", e);
}
catch (IOException e) {
Log.d("Test", "Can't load data", e);
}
catch (ClassNotFoundException e) {
Log.d("Test", "Can't load data", e);
}
}
}
不幸的是,当我在这里到达这条线时:
output.writeObject(formats);
它抛出一个错误,从 Logcat 检索:
09-22 14:50:43.658: D/Test(29362): Can't save data
09-22 14:50:43.658: D/Test(29362): java.io.NotSerializableException: android.widget.TableLayout
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
09-22 14:50:43.658: D/Test(29362): at java.util.ArrayList.writeObject(ArrayList.java:644)
09-22 14:50:43.658: D/Test(29362): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 14:50:43.658: D/Test(29362): at java.lang.reflect.Method.invoke(Method.java:511)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
09-22 14:50:43.658: D/Test(29362): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
09-22 14:50:43.658: D/Test(29362): at tt.tt.Test.make(Test.java:75)
09-22 14:50:43.658: D/Test(29362): at tt.tt.Test.onClick(Test.java:55)
09-22 14:50:43.658: D/Test(29362): at android.view.View.performClick(View.java:3549)
09-22 14:50:43.658: D/Test(29362): at android.view.View$PerformClick.run(View.java:14400)
09-22 14:50:43.658: D/Test(29362): at android.os.Handler.handleCallback(Handler.java:605)
09-22 14:50:43.658: D/Test(29362): at android.os.Handler.dispatchMessage(Handler.java:92)
09-22 14:50:43.658: D/Test(29362): at android.os.Looper.loop(Looper.java:154)
09-22 14:50:43.658: D/Test(29362): at android.app.ActivityThread.main(ActivityThread.java:4945)
09-22 14:50:43.658: D/Test(29362): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 14:50:43.658: D/Test(29362): at java.lang.reflect.Method.invoke(Method.java:511)
09-22 14:50:43.658: D/Test(29362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-22 14:50:43.658: D/Test(29362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-22 14:50:43.658: D/Test(29362): at dalvik.system.NativeStart.main(Native Method)