将列表项保存/加载到文本文件android的有效途径 [英] Efficientway to Save/load List items to text file android

查看:101
本文介绍了将列表项保存/加载到文本文件android的有效途径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,有人能有效地将列表视图数组项保存和加载到文本文件中吗? 您如何将保存和加载集成到此代码中?

Hi All does anyone have a Efficient way to Save and load list view array items to a text file how would you integrate save and load into this code ?

我有一些指导,例如保存到文本.

I have some direction for example for save to text.

         File f = File.createTempFile("file", ".txt",Environment.getExternalStorageDirectory ());
        FileWriter fw = new FileWriter(f);
        fw.write(m_listItems);
        fw.close();

我要做的就是具有保存和加载我添加到listview的列表项的功能.非常感谢您的帮助.我迷路了.

All i want to do is have the ability save and load the list items that i add to the listview Any help would be much appreciated . I'm lost.

提前谢谢你们.

public class ListtestActivity extends Activity {
ArrayAdapter<String> m_adapter;
ArrayList<String> m_listItems = new ArrayList<String>();


Button bt;
Button save;
Button Loadtxt;
EditText et;
TextView tv;
ListView lv;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 save = (Button) findViewById(R.id.button2);
Loadtxt = (Button) findViewById(R.id.button3);
bt = (Button) findViewById(R.id.button1);
et = (EditText) findViewById(R.id.editText1);
tv = (TextView) findViewById(R.id.textView1);
lv = (ListView) findViewById(R.id.listView1);
m_adapter = new ArrayAdapter<String>   (this,android.R.layout.simple_list_item_1, m_listItems);

 lv.setAdapter(m_adapter);
final String input = et.getText().toString();

bt.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

    String input = et.getText().toString();
    if(null!=input&&input.length()>0){     

    m_listItems.add(input);

    m_adapter.notifyDataSetChanged();

  }

save.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {



  }

 Loadtxt.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {



 }
}
});

推荐答案

protected Object bytesToObject(byte[] bytes) throws IOException, ClassNotFoundException {
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream(bais);
    Object ret= ois.readObject();
    ois.close();
    return ret;
}

protected byte[] objectToBytes(Serializable object) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(object);
    oos.close();
    return baos.toByteArray();
}

这篇关于将列表项保存/加载到文本文件android的有效途径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆