将ArrayList保存到内存并读取它

时间:2013-12-01 20:05:46

标签: java android arraylist

我把这个代码放到我的应用程序中,我需要当你在弹出对话框中点击“确定”或“Borrar”保存ArrayList“al”所以当我关闭应用程序并再次打开它时,列表中有以前的项目,我努力但我无法弄明白,我从6天前开始使用java,所以我没有太多的想法,谢谢!

public class Iniciado extends Activity {

    TextView tv3;
    ListView listView;
    final Context context = this;
    final ArrayList<String> al = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.iniciado);
        conseguirdatos();
        tv3=(TextView)findViewById(R.id.tv3);
        Bundle bolsar = getIntent().getExtras();
        tv3.setText("Bienvenido "+ bolsar.getString("NombreKey"));
        listView = (ListView)findViewById(R.id.list);

        // Defined Array values to show in ListView
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, al);

        // Assign adapter to ListView
              listView.setAdapter(adapter); 

              // ListView Item Click Listener
              listView.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                     // ListView Clicked item index
                     final int itemPosition     = position;

                     // ListView Clicked item value
                     String  itemValue    = (String) listView.getItemAtPosition(position);

                      // Show Alert 
                      Toast.makeText(getApplicationContext(),
                        "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
                        .show();

                      // Make Dialog                
                        final Dialog dialog = new Dialog(context);
                        dialog.setContentView(R.layout.cambiar);
                        dialog.setTitle("Editar evento");

                        // set the custom dialog components - text, image and button

                        final EditText editar = (EditText)dialog.findViewById(R.id.eteditar);
                        editar.setText(itemValue);
                        Button okBtn = (Button) dialog.findViewById(R.id.okbtn);
                        Button borrarBtn = (Button) dialog.findViewById(R.id.borrarbtn);
                        // if button is clicked, close the custom dialog
                        okBtn.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                String recuperado = editar.getText().toString();
                                al.set(itemPosition, recuperado);
                                ArrayAdapter<String> adapter = new ArrayAdapter<String>(Iniciado.this, android.R.layout.simple_list_item_1, android.R.id.text1, al);
                                adapter.notifyDataSetChanged();
                                dialog.dismiss();
                                }
                        });
                        borrarBtn.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                al.remove(itemPosition);
                                adapter.notifyDataSetChanged();
                                dialog.dismiss();
                            }
                        });
                        dialog.show();
                   }
           });
    }

    private void conseguirdatos() {

        if(al.size() == 0){
            al.add("C"); 
            al.add("A"); 
            al.add("E"); 
            al.add("B"); 
            al.add("D"); 
            al.add("F");
            al.add("C"); 
            al.add("A"); 
            al.add("E"); 
            al.add("B"); 
            al.add("D"); 
            al.add("F");
            al.add("C"); 
            al.add("A"); 
            al.add("E"); 
            al.add("B"); 
            al.add("D"); 
            al.add("F");
            al.add("C"); 
            al.add("A"); 
            al.add("E"); 
            al.add("B"); 
            al.add("D"); 
            al.add("F");   
            }

    }
}

2 个答案:

答案 0 :(得分:1)

这就是你想要的:

public static ArrayList<String> readList(Context c){//
    try{
        FileInputStream fis = c.openFileInput("NAME");
        ObjectInputStream is = new ObjectInputStream(fis);
        ArrayList<String> list = (ArrayList<String>)is.readObject();
        is.close();
        return list;
    }catch(Exception ex){
        ex.printStackTrace();
        return null;
    }               
}

public static void writeList(Context c, ArrayList<String> list){
    try{
        FileOutputStream fos = c.openFileOutput("NAME", Context.MODE_PRIVATE);
        ObjectOutputStream os = new ObjectOutputStream(fos);
        os.writeObject(list);
        os.close();
    }catch(Exception ex){
        ex.printStackTrace();
    }
}

答案 1 :(得分:0)

请勿使用此功能 final ArrayList<String> al = new ArrayList<String>(); 每次启动应用程序时,它都会将al重新初始化为全新的数组。

其次,Android中有几种内置方式可以保存程序化数据和信息。点击此处:http://developer.android.com/guide/topics/data/data-storage.html

希望这就是你要找的东西:)