Android,在重新打开应用时保存ArrayList

时间:2013-02-15 14:44:42

标签: android

我正在创建一个应该执行以下操作的应用程序:
- 启动时显示启动/信息活动 - 在下一个活动中显示名称列表作为复选框
- 用户可以通过EditText&添加新名称。添加按钮(动态更新列表)
- 关闭应用程序并重新打开时,应保存先前添加的名称并将其显示在列表中 我试图在我的起始活动中将我的列表设置为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);
}
}

要阅读和显示我到目前为止在其他活动中有这个:

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;
    }

    }
}

我在这一点上遇到的一个错误是我的for循环中的“setText”。还不知道如何从打开的文件中获取信息以正确显示在列表中。虽然在没有使用fileInput / Output时for循环工作 任何指向我可以尝试的内容都会非常有用,因为我是Android编程的新手。 :)
THX!

1 个答案:

答案 0 :(得分:0)

问题是在names块内声明了try-catch变量,无法从外部访问。只需将声明移出块:

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();
}

<强>更新 并且您不能使用cbb.setText(names.get(i)),因为names.get(i)不是字符串。它是一个对象(如果你在NAMESFILE中保存了字符串,它实际上应该是String) 您可以将其设置为String:

cbb.setText((String)names.get(i))

或使用toString方法:

cbb.setText(names.get(i).toString())