小编典典

如何正确使用加载数组和保存数组方法?

java

我有以下代码供用户单击按钮使用,当他们单击按钮时,正在查看的特定字符串将被收藏并存储在其他位置。

我有两个问题。

  1. 我现在所拥有的是什么问题?因为当您单击按钮时它会崩溃。

  2. 您将如何完成将要加载的字符串并将其保存到数组的load array方法,以便用户以后可以看到该数组?

谢谢你的时间!!

fav.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            loadArray("favorites");
            favorites = Arrays.copyOf(favorites, favorites.length+1);
            favorites[favorites.length]=display.getText().toString();
            saveArray(favorites, "favorites");

        }
    });

    home.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Intent openStartingPoint = new Intent("starting.rt.Menu2");
            startActivity(openStartingPoint);
        }
    });

    search.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Intent openStartingPoint = new Intent("starting.rt.Search");
            startActivity(openStartingPoint);
        }
    });

    moreapps.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Intent goToMarket;
            goToMarket = new Intent(Intent.ACTION_VIEW, Uri
                    .parse("market://search?q=pub:\"Wompa\""));
            startActivity(goToMarket);
        }
    });

}

public String[] loadArray(String arrayName) {  
    SharedPreferences prefs = 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 boolean saveArray(String[] array, String arrayName) {   
    SharedPreferences prefs = 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();  
}

原木猫

05-08 23:10:51.329: I/Process(495): Sending signal. PID: 495 SIG: 9
05-08 23:11:02.360: D/dalvikvm(504): GC_EXTERNAL_ALLOC freed 44K, 53% free 2553K/5379K, external 1625K/2137K, paused 221ms
05-08 23:11:08.599: D/dalvikvm(504): GC_EXTERNAL_ALLOC freed 16K, 51% free 2644K/5379K, external 2707K/3308K, paused 174ms
05-08 23:11:12.570: D/AndroidRuntime(504): Shutting down VM
05-08 23:11:12.570: W/dalvikvm(504): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-08 23:11:12.599: E/AndroidRuntime(504): FATAL EXCEPTION: main
05-08 23:11:12.599: E/AndroidRuntime(504): java.lang.NullPointerException
05-08 23:11:12.599: E/AndroidRuntime(504):  at starting.rt.Base$1.onClick(Base.java:52)
05-08 23:11:12.599: E/AndroidRuntime(504):  at android.view.View.performClick(View.java:2485)
05-08 23:11:12.599: E/AndroidRuntime(504):  at android.view.View$PerformClick.run(View.java:9080)
05-08 23:11:12.599: E/AndroidRuntime(504):  at android.os.Handler.handleCallback(Handler.java:587)
05-08 23:11:12.599: E/AndroidRuntime(504):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 23:11:12.599: E/AndroidRuntime(504):  at android.os.Looper.loop(Looper.java:123)
05-08 23:11:12.599: E/AndroidRuntime(504):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-08 23:11:12.599: E/AndroidRuntime(504):  at java.lang.reflect.Method.invokeNative(Native Method)
05-08 23:11:12.599: E/AndroidRuntime(504):  at java.lang.reflect.Method.invoke(Method.java:507)
05-08 23:11:12.599: E/AndroidRuntime(504):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-08 23:11:12.599: E/AndroidRuntime(504):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-08 23:11:12.599: E/AndroidRuntime(504):  at dalvik.system.NativeStart.main(Native Method)
05-08 23:11:17.869: I/Process(504): Sending signal. PID: 504 SIG: 9

阅读 290

收藏
2020-11-30

共1个答案

小编典典

loadArray函数返回String[]。您可能想要像这样使用它:

String [] arr = loadArray("favorites");

该数组可以稍后在第二部分中使用。希望这可以帮助。

更新 [根据您的评论]

因此,如果您想在TextView中获取文本并希望将其添加到List / String数组中:

1)尝试从TextView获取字符串

TextView display = (TextView)findViewById(R.id.display);
String toSave = display.getText().toString();

2)使用`loadArray’从SharedPreferences获取现有列表:

String [] arr = loadArray("favorites");

3)添加新的字符串:

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

for (int i=0; i<arr.length(); i++) {
     arrList.add(arr[i]); //add old values to arraylist
}
// add new string to it: 
arrList.add(toSave);

//Get back your array
String [] array = arrList.toArray(new String[arrList.size()]);

4)再次保存。

saveArray(array, "favorites");
2020-11-30