当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Storing and getting values from sharedprefernces in android

问题描述:

I want to store values in sharedprefernces and getting values from sharedprefernces finally display the values in listview. Next I want to remove values from list when click on longpress on specific value .After removing values status updated in sharedprefernces.i done this one but problem is when close application and open again i have to display sharedprefernces vales in listview. but it display NullPointerExeption,

actually my requirement is 1)storing values in sharedprefernces and display values in listview for every click .allowed only five values.And that list values are available in onresume() method also.

2) when long press on specific value of listview it have to remove and and updated values are stored in sharedprefernces also.

my code

public class ListViewDemo1 extends Activity {

/**

* Called when the activity is first created.

*/

Button btn;

static int count;

private ListView list;

public static ArrayList<String> values = new ArrayList<String>();

ArrayList countList = new ArrayList();

private ArrayAdapter adapter;

SharedPreferences shared;

Editor editor;

private static ArrayList<String> sharedList;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

list = (ListView) findViewById(R.id.list);

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

shared = this.getSharedPreferences("Myprefernces", Context.MODE_WORLD_WRITEABLE);

editor = shared.edit();

btn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

sharedList = new ArrayList();

count++;

if (count == 1) {

values.add("one");

countList.add(count);

}

if (count == 2) {

values.add("two");

countList.add(count);

}

if (count == 3) {

values.add("three");

countList.add(count);

}

if (count == 4) {

values.add("four");

countList.add(count);

}

if (count == 5) {

values.add("five");

countList.add(count);

}

if (count > 5) {

--count;

Toast.makeText(getApplicationContext(), "" + count, 100).show();

}

//put values to sharedpreferences

editor.putInt("SIZE", values.size());

for (int i = 0; i < values.size(); i++) {

editor.putString("addr" + i, values.get(i));

}

editor.commit();

// getting values from sharedpreference

int size = shared.getInt("SIZE", 0);

for (int k = 0; k < size; k++) {

sharedList.add(shared.getString("addr" + k, ""));

}

adapter = new

ArrayAdapter(ListViewDemo1.this, android.R.layout.simple_list_item_1, sharedList);

list.setAdapter(adapter);

}

});

list.setOnItemLongClickListener(new OnItemLongClickListener() {

public boolean onItemLongClick(AdapterView<?> arg0, View arg1,

int arg2, long arg3) {

// TODO Auto-generated method stub

--count;

values.remove(arg2);

sharedList.remove(arg2);

editor.clear();

editor.commit();

editor.putInt("SIZE", sharedList.size());

for (int i = 0; i < sharedList.size(); i++) {

editor.putString("addr" + i, sharedList.get(i));

}

editor.commit();

adapter.notifyDataSetChanged();

return true;

}

});

}

@Override

protected void onResume() {

// TODO Auto-generated method stub

super.onResume();

if (sharedList.size() > 0) {

adapter = new

ArrayAdapter(ListViewDemo1.this, android.R.layout.simple_list_item_1, sharedList);

list.setAdapter(adapter);

}

}

}

Logcat

04-21 09:56:02.142: E/AndroidRuntime(1160): FATAL EXCEPTION: main

04-21 09:56:02.142: E/AndroidRuntime(1160): java.lang.RuntimeException: Unable to resume activity {com.views/com.views.ListViewDemo1}: java.lang.NullPointerException

04-21 09:56:02.142: E/AndroidRuntime(1160): at android.app.ActivityThread.access$600(ActivityThread.java:141)

04-21 09:56:02.142: E/AndroidRuntime(1160): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

04-21 09:56:02.142: E/AndroidRuntime(1160): at android.os.Handler.dispatchMessage(Handler.java:99)

04-21 09:56:02.142: E/AndroidRuntime(1160): at android.os.Looper.loop(Looper.java:137)

04-21 09:56:02.142: E/AndroidRuntime(1160): at android.app.ActivityThread.main(ActivityThread.java:5041)

04-21 09:56:02.142: E/AndroidRuntime(1160): at java.lang.reflect.Method.invokeNative(Native Method)

04-21 09:56:02.142: E/AndroidRuntime(1160): at java.lang.reflect.Method.invoke(Method.java:511)

04-21 09:56:02.142: E/AndroidRuntime(1160): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

04-21 09:56:02.142: E/AndroidRuntime(1160): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

04-21 09:56:02.142: E/AndroidRuntime(1160): at dalvik.system.NativeStart.main(Native Method)

04-21 09:56:02.142: E/AndroidRuntime(1160): Caused by: java.lang.NullPointerException

04-21 09:56:02.142: E/AndroidRuntime(1160): at com.views.ListViewDemo1.onResume(ListViewDemo1.java:114)

04-21 09:56:02.142: E/AndroidRuntime(1160): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)

网友答案:

You instantiate sharedList only in onClick() method of a button. This method is not called when your activity is resumed and sharedList is null.

You need to check if it's not null:

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    if(sharedList != null && sharedList.size()>0){
        adapter=new ArrayAdapter(ListViewDemo1.this,android.R.layout.simple_list_item_1,sharedList);
        list.setAdapter(adapter);
    }
} 
网友答案:

The problem i think is with initialization(or rather not doing that),

static int count;
public static ArrayList<String>  values = new ArrayList<String>();
private static ArrayList<String> sharedList;

Now for eg, not initialized count to a value before:

count++;  
if(count==1){  

Same needs to be done for sharedList, like new ArrayList...

And yes, the reason being onCreate() is not called before onResume() every time, for more on the refer Android Activity Life Cycle
Check Figure 1. The activity lifecycle. on that link.

It will give you a better idea for your implementation/requirement and how to initialize or work with sharedPref and other components for your activity.

网友答案:

read this I hope help you http://developer.android.com/guide/topics/data/data-storage.html

You can use SharedPreferences:

To save data:

SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
 editor.putString("text", mSaved.getText().toString());
 editor.putInt("selection-start", mSaved.getSelectionStart());
 editor.putInt("selection-end", mSaved.getSelectionEnd());
 editor.commit();

To retrieve data:

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
int selectionStart = prefs.getInt("selection-start", -1);
int selectionEnd = prefs.getInt("selection-end", -1);
分享给朋友:
您可能感兴趣的文章:
随机阅读: