最好的方式保存的ArrayList到机器人记忆 [英] Best way to save arraylist to androids memory

查看:102
本文介绍了最好的方式保存的ArrayList到机器人记忆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个活动传递到另一个包。包含字符串n(长度为最多30),字符串ID和串色。我需要将这些值保存到一个ArrayList作为数组(N,标识,颜色),然后保存的ArrayList到机器人的记忆。我一直在寻找这样做。我已经试过数据库,但其复杂,我目前的一个最好的方式,我不认为我需要这样一个复杂的东西。我试过的FileOutputStream(如在这里解释: HTTP ://developer.android.com/guide/topics/data/data-storage.html#$p$pf 的),但它不工作对我来说,可能是因为我做错了什么?。我是不是真的需要创建阵列的ArrayList或者可能是我可以使用ArrayList中束,或任何其他方式..?请告诉我最好的方式...?请帮助..

由于每个人......是想这么长的时间,但没有运气..我张贴了code希望有人可以给我一个手就认为:

 公共类MainActivity扩展活动
{
字符串gotNotes;
字符串n;
字符串gotDOW;
字符串gotID;
公共字符串CLRS;
公共字符串ID;
公共字符串NTS;
字符串gotHour;
字符串gotColor;
TextView的说明;
公共静态字符串文件名=allevents;
的String [] newevent;
的String []的活动;
共享preferences共享preF;


  @覆盖
  公共无效的onCreate(包savedInstanceState)
   {
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);



    按键设置=(按钮)findViewById(R.id.settings);

    捆绑gotPackage = getIntent()getExtras()。
    如果(gotPackage!= NULL){
    gotNotes = gotPackage.getString(AddedNote);
    如果(gotNotes.equals())
      {
        N =空;
      }
    其他
        {
        N = gotNotes;
        }
    // gotDOW = gotPackage.getString(日);
    // gotHour = gotPackage.getInt(一小时);
    gotID = gotPackage.getString(ID);
    gotColor = gotPackage.getString(颜色);

    初始化();

    }
    其他{}



 settings.setOnClickListener(新OnClickListener()
    {
       公共无效的onClick(视图v)
         {
              意图I =新的意图(v.getContext(),Settings.class);
              startActivityForResult(ⅰ,0);
         }

    });

 }

私人无效初始化()
 {
    // TODO自动生成方法存根
的String [] newevent = {N,gotID,gotColor};

ArrayList的<的String []>事件=新的ArrayList<的String []>();
events.add(newevent);

共享preferences共享preF = this.get preferences(Activity.MODE_PRIVATE);
共享preferences.Editor编辑= this.get preferences(Activity.MODE_PRIVATE).edit();
editor.putString(yourKey,events.toString());
editor.commit();

字符串ALLDATA =共享pref.getString(yourKey,NULL);
串[]播放列表= allData.split(,);

  / *为(INT数= 0;数< events.lastIndexOf(共享preF);数量++)
   {

           注释=(TextView中)findViewById(getResources()则getIdentifier(播放列表[数字],的getString(0),ALLDATA)。);
           notes.setText(数+ 1);


   } * /



    注释=(TextView中)findViewById(getResources()则getIdentifier(gotID,ID,getPackageName()));
    notes.setText(N);
    notes.setGravity(Gravity.CENTER_HORIZONTAL);
    notes.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
    如果(gotColor.equals(蓝)){
    notes.setBackgroundColor(Color.rgb(99,184,255));}否则
    如果(gotColor.equals(绿色)){
    notes.setBackgroundColor(Color.rgb(189,252,201));}其他
    如果(gotColor.equals(黄)){
    notes.setBackgroundColor(Color.rgb(238,233,​​191));}其他
    如果(gotColor.equals(灰色)){
    notes.setBackgroundColor(Color.LTGRAY);}其他
    如果(gotColor.equals(水族)){
    notes.setBackgroundColor(Color.rgb(151,255,255));}其他
    如果(gotColor.equals(白)){}

}



    @覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
    。getMenuInflater()膨胀(R.menu.activity_main,菜单);
    返回true;

}

}
 

解决方案

只需使用共享preferences保存您的应用程序的数据。

 共享preferences共享preF = activity.get preferences(Activity.MODE_PRIVATE);
共享preferences.Editor编辑= activity.get preferences(Activity.MODE_PRIVATE).edit();
editor.putString(yourKey,yourArray.toString());
editor.commit();
 

为了让您的数组作为字符串执行以下操作:

 字符串arrayString =共享pref.getString(yourKey,NULL);
 

I have a bundle passed from one activity to another. That contains String n (the length is max 30), String ID and String color. I need to save these values to an ArrayList as an array (n, ID, color) and then to save ArrayList to androids memory. I was looking for a best way of doing that.. I've tried database but its to complicated for me at the moment and I don't think I need such a complex thing. I've tried FileOutputStream (as it explained here: http://developer.android.com/guide/topics/data/data-storage.html#pref?) but it's not working for me, probably because I'm doing something wrong. Do I actually need to create an arraylist of arrays or may be i could use arraylist of bundles, or any other way..? Whats the best way...? Please help..

Thanks every one...Was trying all this time but no luck.. I'm posting the code hoping that someone could give me a hand on that:

public class MainActivity extends Activity
{
String gotNotes;
String n;
String gotDOW;
String gotID;
public String clrs;
public String id;
public String nts;
String gotHour;
String gotColor;
TextView notes;
public static String FILENAME = "allevents";
String[] newevent;
String[] events;
SharedPreferences sharedPref;


  @Override
  public void onCreate(Bundle savedInstanceState) 
   {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    Button settings = (Button)findViewById(R.id.settings);

    Bundle gotPackage = getIntent().getExtras();
    if (gotPackage != null){
    gotNotes = gotPackage.getString("AddedNote");
    if (gotNotes.equals(" "))
      {
        n = "Empty";
      }
    else 
        {
        n = gotNotes;
        }
    //gotDOW = gotPackage.getString("Day");
    //gotHour = gotPackage.getInt("Hour");
    gotID = gotPackage.getString("ID");
    gotColor = gotPackage.getString("color");

    initialize();

    }
    else{}



 settings.setOnClickListener(new OnClickListener()
    {
       public void onClick(View v)
         {
              Intent i = new Intent(v.getContext(),Settings.class);
              startActivityForResult(i,0);
         }

    });

 }

private void initialize() 
 {
    // TODO Auto-generated method stub
String[] newevent = {n, gotID, gotColor};

ArrayList<String[]> events = new ArrayList<String[]>();
events.add(newevent);

SharedPreferences sharedPref = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor =     this.getPreferences(Activity.MODE_PRIVATE).edit();
editor.putString("yourKey", events.toString());
editor.commit();

String allData = sharedPref.getString("yourKey", null);
String[] playlists = allData.split(",");

  /* for (int number=0;number<events.lastIndexOf(sharedPref);number++)
   {

           notes = (TextView)findViewById(getResources().getIdentifier(playlists[number], getString(0), allData));
           notes.setText(number+1);


   }*/



    notes = (TextView)findViewById(getResources().getIdentifier(gotID,     "id",getPackageName()));
    notes.setText(n);
    notes.setGravity(Gravity.CENTER_HORIZONTAL);
    notes.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
    if (gotColor.equals("Blue")){
    notes.setBackgroundColor(Color.rgb(99, 184, 255));}else
    if(gotColor.equals("Green")){
    notes.setBackgroundColor(Color.rgb(189, 252, 201));}else
    if(gotColor.equals("Yellow")){
    notes.setBackgroundColor(Color.rgb(238, 233, 191));}else
    if(gotColor.equals("Grey")){
    notes.setBackgroundColor(Color.LTGRAY);}else    
    if(gotColor.equals("Aqua")){
    notes.setBackgroundColor(Color.rgb(151, 255, 255));}else
    if(gotColor.equals("White")){}

}



    @Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;

}

}

解决方案

Simply use SharedPreferences to save your application's data.

SharedPreferences sharedPref = activity.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = activity.getPreferences(Activity.MODE_PRIVATE).edit();
editor.putString("yourKey", yourArray.toString());
editor.commit();

To get your array as String do the following:

String arrayString = sharedPref.getString("yourKey", null);

这篇关于最好的方式保存的ArrayList到机器人记忆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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