我想尝试使用一小组TextView创建一个TableRow,每个TextView都包含一个原始的String数据。我想,也许如果我存储这些TableRows的ArrayList,并且如果成功地来回读/写,我会在Android屏幕上简化大量时间来创建数据表或图表。
从表面上看,我觉得我必须创建很多视图的子类,并为所有这些子类实现Serializable。这完全超出了我的能力。
我的主要问题是:是否可以将一组TextView存储到TableRow中,创建一个完整的TableRows数组,并将它们中的每一个单独添加到TableLayout中?
到目前为止,我不能超越使用ObjectOutputStream保存数据,因为它不断抛出NotSerializableException,InvalidAccessException,IllegalAccessException,EOFException,IOException等各种异常。
如果我的概念不是很好,请问我应该在哪里重新考虑这一点?我应该更改什么,以及如何存储数据组?提前谢谢。
不用多说,这是我用来尝试保存TableRows的ArrayList的代码,以及我过去12小时不间断工作的所有mumbo-jumbo。
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)
答案 0 :(得分:0)
经过漫长的星际争霸2会议后想出来了。所有这个问题的真正解决方案是不将接口Serializable实现为嵌套类,无论是public,protected还是private。
我发誓,如果你将这个接口实现到你的嵌套类中,它还会尝试序列化外部类中的其他元素,在本例中,它们是Activity类。当然,它会失败。你不能序列化一个Activity类,有这么多非Serializable成员和实例,如果你这样做,就不可能让它工作。
就是这样。
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.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 {
List<Format> formats = new ArrayList<Format>();
TableRow row;
TableLayout layout;
public void onClick(View v) {
switch (v.getId()) {
case R.id.make:
make();
break;
case R.id.retrieve:
retrieve();
break;
}
}
@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 make() {
Random r = new Random();
Format f = new Format();
f.position = r.nextInt(10);
f.name = r.toString();
f.points = r.nextLong();
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("Format", "Can't save data", e);
}
catch (IOException e) {
Log.d("Format", "Can't save data", e);
}
}
public void retrieve() {
formats.clear();
FileInputStream in;
try {
in = this.openFileInput("format");
ObjectInputStream input = new ObjectInputStream(new BufferedInputStream(in));
formats = (ArrayList<Format>) input.readObject();
input.close();
}
catch (FileNotFoundException e) {
Log.d("Format", "Can't load data", e);
}
catch (StreamCorruptedException e) {
Log.d("Format", "Can't load data", e);
}
catch (IOException e) {
Log.d("Format", "Can't load data", e);
}
catch (ClassNotFoundException e) {
Log.d("Format", "Can't load data", e);
}
int count = 0;
while (count < formats.size()) {
Format f = formats.get(count);
row = new TableRow(this);
TextView temp = new TextView(this);
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);
temp.setText(Integer.toString(f.position));
row.addView(temp, 0, params);
temp = new TextView(this);
temp.setText(f.name);
row.addView(temp, 1, params);
temp = new TextView(this);
temp.setText(Long.toString(f.points));
row.addView(temp, 2, params);
layout.addView(row);
count++;
}
}
}
格式类:
package tt.tt;
import java.io.Serializable;
public class Format implements Serializable {
private static final long serialVersionUID = 1L;
public String name;
public long points;
public int position;
@Override
public String toString() {
return new StringBuilder().append(Integer.toString(position)).append(" ").append(name).append(" ").append(points).toString();
}
}