[英]Android, Save ArrayList for when reopening app
I'm creating an app wich is supposed to do the following: 我正在创建一个应该执行以下操作的应用程序:
- When started shows a splash/info-activity. -启动时显示启动/信息活动。
- In next activity shows a list of names as checkboxes -在下一个活动中,将名称列表显示为复选框
- user can add new names via EditText & Add-button (list updated dynamically) -用户可以通过EditText和“添加”按钮添加新名称(列表动态更新)
- When closing app, and reopening, names previously added should be saved and displayed in list. -关闭应用程序并重新打开时,以前添加的名称应保存并显示在列表中。
I've tried to setup my list as ArrayList in my startingactivity just to see if I can save and load my information correctly: 我试图在我的startupactivity中将列表设置为ArrayList,只是为了查看我是否可以正确保存和加载信息:
public class StartActivity extends Activity implements OnClickListener {
Typeface face;
TextView tvStartIntrotext;
Button bStartStart;
ArrayList<String> names = new ArrayList<String>();
String NAMESFILE = "names_file";
FileOutputStream fos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startactivity);
face = Typeface.createFromAsset(getAssets(), "andalemono.ttf");
bStartStart = (Button) findViewById(R.id.bStartStart);
bStartStart.setTypeface(face);
tvStartIntrotext = (TextView) findViewById(R.id.tvStartIntrotext);
tvStartIntrotext.setTypeface(face);
bStartStart.setOnClickListener(this);
try {
fos = openFileOutput(NAMESFILE, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
names.add("Name Name");
oos.writeObject(names);
oos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.startactivity, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent startIntent = new Intent(StartActivity.this, Choose.class);
startActivity(startIntent);
}
}
To read and display I have this in my other activity so far: 要阅读和显示,到目前为止,我在其他活动中也有此内容:
public class Choose extends Activity implements OnClickListener {
String NAMESFILE = "names_file";
FileInputStream fis;
Button bChooseChoose, bChooseAdd;
Typeface face;
TextView tvChoosePick;
EditText etChooseAddnew;
LinearLayout llMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.slideinright, R.anim.slideoutleft);
setContentView(R.layout.choose);
face = Typeface.createFromAsset(getAssets(), "andalemono.ttf");
bChooseChoose = (Button) findViewById(R.id.bChooseChoose);
bChooseChoose.setTypeface(face);
bChooseChoose.setOnClickListener(this);
bChooseAdd = (Button) findViewById(R.id.bChooseAdd);
bChooseAdd.setTypeface(face);
bChooseAdd.setOnClickListener(this);
tvChoosePick = (TextView) findViewById(R.id.tvChoosePick);
tvChoosePick.setTypeface(face);
etChooseAddnew = (EditText) findViewById(R.id.etChooseAddnew);
etChooseAddnew.setTypeface(face);
etChooseAddnew.setBackgroundResource(R.color.white1);
etChooseAddnew.setHintTextColor(color.greytext);
llMain = (LinearLayout) findViewById(R.id.llMain);
try {
fis = openFileInput(NAMESFILE);
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<Object> names = (ArrayList<Object>) ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < names.size(); i++) {
CheckBox cbb = new CheckBox(this);
cbb.setText(names.get(i));
cbb.setTypeface(face);
cbb.setTextSize(16);
llMain.addView(cbb);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.bChooseChoose:
break;
case R.id.bChooseAdd:
/*This button for adding new list name*/
break;
}
}
}
One error I'm getting at this point is "setText" in my for-loop. 我现在遇到的一个错误是for循环中的“ setText”。 Don't yet know how to get the information from the file opened to display in a list correctly.
尚不知道如何从打开的文件中获取信息以正确显示在列表中。 Although the for-loop works when no fileInput/Output is used.
尽管在不使用fileInput / Output的情况下for循环有效。
Any pointers to what I can try would be really helpful, since I'm new to Android programming. 因为我是Android编程的新手,所以我可以尝试的任何指针都将非常有帮助。 :)
:)
Thx! 谢谢!
The problem is that names
variable declared inside try-catch
block and cannot be accessed from outside. 问题在于,
names
变量在try-catch
块内声明,无法从外部访问。 Just move declaration out of the block: 只需将声明移出块即可:
List<Object> names;
try {
fis = openFileInput(NAMESFILE);
ObjectInputStream ois = new ObjectInputStream(fis);
names = (ArrayList<Object>) ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
names = Collections.emptyList();
}
UPDATE And you can't use cbb.setText(names.get(i))
because names.get(i)
is not a String. 更新而且您不能使用
cbb.setText(names.get(i))
因为names.get(i)
不是字符串。 It's an object (that actually should be String if you saved strings in NAMESFILE
). 它是一个对象(如果将字符串保存在
NAMESFILE
,则实际上应该是String)。
You can either case it to String: 您可以将其大小写为String:
cbb.setText((String)names.get(i))
or use toString
method: 或使用
toString
方法:
cbb.setText(names.get(i).toString())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.