Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 序列化arrayList并保存和读取它_Android - Fatal编程技术网

Android 序列化arrayList并保存和读取它

Android 序列化arrayList并保存和读取它,android,Android,每次单击按钮时,我都试图永久保存arrayList。该函数将在内存中添加新的arrayList,以便在另一个活动中读取该arrayList时,我将读取已保存的所有arrayList,这样,即使在关闭应用程序时,我也可以保留数据,并保存数据的所有历史记录已通过单击选择 此活动是我希望通过单击在列表中选择项目时保存数据的活动 并将其保存在内存中,并能够在另一个活动中读取这些数据,以在列表中显示这些数据 public class ResultView extends Activity { prote

每次单击按钮时,我都试图永久保存arrayList。该函数将在内存中添加新的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();




      }
   });



}
public类ResultView扩展活动
{
受保护的静态最终字符串LOG_TAG=null;
受保护的静态最终字符串EXTRA_BUTTONTEXT=null;
受保护的静态最终int接收消息=0;
字符串[]lv_arr={};
文本视图标题,总和;
列表适配器;
文本视图t;
私有ListView用户;
私人ArrayList公司;
字符串response=null;
保护整数计数;
从CountryChooseId到CountryChooseId的私有int;
字符序列转换;
私有字符序列名称来自国家/地区,国家/地区名称;
ArrayList结果=新建ArrayList();
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
//将产品列表设置为ListView
mListUsers=GetMedicalmentsView();
lvUsers=(ListView)findViewById(R.id.resultListView);
setAdapter(新的ListAdapter(this,R.id.resultListView,mListUsers));
lvUsers.setOnItemClickListener(新的OnItemClickListener()
{
私有对象myObject;
public void onItemClick(AdapterView父对象、视图、整型位置、长id)
{
Intent pingIntent=newintent(getApplicationContext(),ConvertedView.class);
//从正确位置的listView获取数据
int DCIN=mListUsers.get(position).DCIN;
字符串分子=mListUsers.get(position).DCI;
字符串pathologie=mListUsers.get(position).pathologie;
字符串medicaments=mListUsers.get(position.medicaments);
//使用pingIntent为下一个城市共享数据
pingIntent.putExtra(“DCInchoosenMedicals”,DCIN);
pingIntent.putExtra(“分子选择药物”,分子);
pingIntent.putExtra(“病理学治疗”,病理学);
pingIntent.putExtra(“TOcountryChooseId”,TOcountryChooseId);
pingIntent.putExtra(“noftocontry”,tocountry名称);
Log.i(“结果视图中的药物名称*******************”,“+国家名称”);
myObject=药物;
//TODO:将数据保存在内存或文件中
setResult(Activity.RESULT\u OK,pingIntent);
星触觉(pingIntent);
//完成();
}
});
}
我试图在网上找到一个很好的解释,但没有什么可以帮助我。我真的对这个问题迷茫了。我会感谢你的帮助


非常感谢

你需要一个持久的存储吗?如果是这样的话,看看或者考虑创建自己的SQLite数据库。

< p>你可以使用序列化来将ARAYLIST对象存储为文件的字节序列。然后在SD卡上存储文件java /P > < PoxSerialActuab化与Java可串行化没有什么不同。通过阅读这里的Java文档了解到


希望这能有所帮助。

请分享您的代码,保存ArrayListits绝对有可能。如果要保存ArrayListits,请使用“serializable”来保存它,您能否提供一个示例ObjectOutputStream=new ObjectOutputStream(out);stream.writeObject(object);谢谢Ryan,添加了新的Oracle链接:)