【问题标题】:Passing large amount of data from class to class android将大量数据从类传递到类android
【发布时间】:2014-06-16 08:41:09
【问题描述】:

我正在开发一个 Android 应用程序,其中使用了许多 StringString[]intint[]。我必须使用intent.putExtra() 将此变量从一个活动传递到另一个活动。但在我看来有点恶心。我喜欢优化我的代码。 我会将这些变量用作static。但是使用过多的static 数据会导致内存泄漏。 那么在android的不同活动中管理随机使用的变量的正确方法是什么? 提前致谢。

【问题讨论】:

  • 使用 SharedPreferences 存储检索值
  • 我已经实现了单例模型。它是动态的和轻量级的编码来实现。谢谢楼主!

标签: java android android-intent android-activity static-variables


【解决方案1】:

确实,静态变量会导致内存泄漏和NullPointerExceptions

您可以将字符串和字符串数组保存到SharedPreferences,所以这里有一个适合您的解决方案(永远记得对您想要从 SharedPreferences 获取的数据进行空检查):

用于设置和获取对象的 SharedPreferences 类:

    public class SharedPrefManager {

            public static boolean saveArray(String[] array, String arrayName, Context mContext) {   
                SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
                SharedPreferences.Editor editor = prefs.edit();  
                editor.putInt(arrayName +"_size", array.length);  
                for(int i=0;i<array.length;i++)  
                    editor.putString(arrayName + "_" + i, array[i]);  
                return editor.commit();  
            } 

            public static String[] loadArray(String arrayName, Context mContext) {  
                SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
                int size = prefs.getInt(arrayName + "_size", 0);  
                String array[] = new String[size];  
                for(int i=0;i<size;i++)  
                    array[i] = prefs.getString(arrayName + "_" + i, null);  
                return array;  
            }  

            public static void setDefaults(String key, String value, Context context) {
                SharedPreferences prefs =  PreferenceManager.getDefaultSharedPreferences(context);
                SharedPreferences.Editor editor = prefs.edit();
                editor.putString(key, value);
                editor.commit();
            }

            public static String getDefaults(String key, Context context) {
                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
                return preferences.getString(key, null);
            }

    }

将数组设置为 SharedPreferences: 清除并将您的数组更新为 sharedpreferences。

//save array to shared pref
SharedPrefManager.saveArray(array, "array_tag", getApplicationContext());

从 SharedPreferences 获取数组:

String [] a = SharedPrefManager.loadArray("array_tag", getApplicationContext());

将字符串保存到 SharedPreferences

SharedPrefManager.setDefaults("your_tag", "string_value", context);

从 SharedPreferences 中获取字符串

SharedPrefManager.getDefaults("your_tag", getApplicationContext())

【讨论】:

    【解决方案2】:

    在我看来,要处理和使用大量数据,您应该选择 字符串数组列表

    关于您面临的另一个问题是携带所有数据。您可以轻松地将数组列表从一个活动传递到另一个活动。

    看看这个简单的例子:

    假设“CurrentActivity.java”是一个您要发送数组列表的活动

    首先你应该像这样声明和初始化数组列表:

    ArrayList<String> listOfItems; listOfItems = new ArrayList<String>();

    将一些值放入数组列表中:

    listOfItems.add("Add all of your items here..");

    现在在 CurrentActivity 中将这段代码放在任何按钮上点击:

    Intent i = new Intent(CurrentActivity.this, NewActivity.class); i.putStringArrayListExtra("itemList", listOfItems);

    “NewActivity.java”将是一个从 CurrentActivity 获取数组列表的活动

    所以在 New Activity selectedList 将是另一个 arraylist 对象,现在你只需要放:

    Bundle i1 = getIntent().getExtras(); selectedList = i1.getStringArrayList("itemList");

    它完成了。希望它会帮助你。 :)

    【讨论】:

    • 如果对你有用别忘了勾选正确。
    【解决方案3】:

    为此使用共享首选项,一旦您存储了数据,您就可以访问整个应用程序中的值。

    SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("EMP_ID",mUname);
        editor.commit(); //commit is important for storing the value
    

    使用下面的代码,您可以在任何活动中检索值

    SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
    final String myVariable = prefs.getString("EMP_ID", "mUname");
    

    【讨论】:

      猜你喜欢
      • 2012-09-02
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多