序列化一个arrayList并保存并读取它

时间:2011-04-01 11:09:54

标签: android

我试图永久保存一个arrayList,每次当我点击按钮时,该函数将在内存中添加新的arrayList,所以当在另一个活动中读取它时,我将读取所有已保存的arrayList,就像我一样即使我关闭应用程序也能保存我的数据,我将保存所有数据的历史记录,只需点击一下即可。

此活动是我希望每次通过点击在列表中选择项目时保存数据

并将其保存在内存中,并能够在另一个活动中读取此数据,以便在列表中显示

public class ResultView extends Activity 
{
protected static final String LOG_TAG = null;
protected static final String EXTRA_BUTTONTEXT = null;
protected static final int RECEIVE_MESSAGE = 0;

String[] lv_arr = {};
TextView title,sum;
ListAdapter adapter;
TextView t;
private ListView lvUsers;
private ArrayList<Medicaments> mListUsers;
String responce=null;
protected int count;

private int FROMcountryChooseId,TOcountryChooseId;
CharSequence meToConvert;
private CharSequence nameOfFromcountry,nameOfTocountry;


ArrayList<History> results = new ArrayList<History>();

public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.result); 


  //set the List of the product to the ListView
  mListUsers = getMedicamentsView();
  lvUsers = (ListView) findViewById(R.id.resultListView);
  lvUsers.setAdapter(new ListAdapter(this, R.id.resultListView, mListUsers)); 

  lvUsers.setOnItemClickListener(new OnItemClickListener()
  {

      private Object myObject;

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




          Intent pingIntent = new Intent(getApplicationContext(),ConvertedView.class);

          //get the data from the listView in the right position
          int DCIN=mListUsers.get(position).DCIN;
          String molecule=mListUsers.get(position).DCI;
          String pathologie=mListUsers.get(position).Pathologie;
          String medicaments=mListUsers.get(position).Medicaments;

          //use pingIntent to share the data for the next Acitity
          pingIntent.putExtra("DCINChoosenMedicaments",DCIN);
          pingIntent.putExtra("moleculeChoosenMedicaments",molecule);
          pingIntent.putExtra("pathologieChoosenMedicaments",pathologie);
          pingIntent.putExtra("TOcountryChooseId",TOcountryChooseId);

          pingIntent.putExtra("nOfTocountry",nameOfTocountry);
           Log.i("Name of medicaments from ResultView **********************     "," "+nameOfTocountry);

           myObject=medicaments;

           //TODO:Save Data in the memory or or file 



           setResult(Activity.RESULT_OK,pingIntent);


          startActivity(pingIntent);
          //finish();




      }
   });



}

我试着在网上查看一个很好的解释,但没有什么可以帮助我,我真的迷失在这个主题上我会感谢帮助

非常感谢

3 个答案:

答案 0 :(得分:1)

Android serializable与Java serializable没有什么不同。您可以通过阅读Java docs http://java.sun.com/developer/technicalArticles/Programming/serialization/ http://docs.oracle.com/javase/6/docs/technotes/guides/serialization/index.html

来解决所有问题。

希望这有帮助。

答案 1 :(得分:0)

那么你需要持久存储吗?如果是这样,请查看SharedPreferences或考虑创建自己的SQLite数据库。

答案 2 :(得分:0)

您可以使用序列化将ArrayList对象作为字节序列存储到文件中。然后将文件存储在SD卡上