1

I am trying to use SharedPreferences to store data from an ArrayList. But I am lost. I am using a for loop to retrieve the data and store it in the listview. Also when I press the button the preferences are stored.

package com.example.todolist;

import java.util.ArrayList;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

    ArrayList<String> items;
    ArrayAdapter<String> adapter;
    ListView l;
    EditText et;
    Button bt, bt2;
    Object ob;
    int i = 0, k = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        et = (EditText) findViewById(R.id.editText1);
        bt = (Button) findViewById(R.id.button1);
        bt2 = (Button) findViewById(R.id.button2);
        l = (ListView) findViewById(R.id.listView1);

        items = new ArrayList<String>();

        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, items);

        l.setAdapter(adapter);

        bt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                action();

                SharedPreferences prfs = getSharedPreferences("PREFS", i);
                Editor edit = prfs.edit();

                edit.putString("" + i, et.getText().toString());
                edit.putInt("counter", i);
                edit.commit();

                et.setText("");

                k = prfs.getInt("counter", 0);
                i = i + 1;

            }

        });

        for (i = 0; i < k + 1; i++) {

            SharedPreferences prfs = getSharedPreferences("PREFS", i);
            items.add(0, (String) prfs.getString("" + i, "Null"));
            adapter.notifyDataSetChanged();

        }

    }

    private void action() {
        // TODO Auto-generated method stub

        if (!("".equals(et.getText().toString()))) {

            ob = et.getText().toString();
            items.add(0, (String) ob);
            adapter.notifyDataSetChanged();

        } else {

            Toast.makeText(getBaseContext(), "Field should not be empty!",
                    Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
user2574723
  • 125
  • 10

4 Answers4

0

Change To:

 SharedPreferences prfs;//Declare it globally

Put it in onCreate()

prfs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

In setOnClickListener of bt;

            Editor edit = prfs.edit();

            edit.putString("MyString" + String.ValueOf(i), et.getText().toString());
            edit.putInt("counter", i);
            edit.commit();

In for loop

for (i = 0; i < k + 1; i++) {


            items.add(0, (String) prfs.getString("MyString" + String.ValueOf(i), "Null"));
            adapter.notifyDataSetChanged();

        }
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
0

Parameters of getSharedPreferences are filename , mode. Don't put i put 0.

SharedPreferences prfs = getSharedPreferences("PREFS", 0);
Aniket Gupte
  • 122
  • 1
  • 8
  • I tried that also.. I put a predefined constant. But it is not displaying.. If I put some other string in the for loop add statement, the items are being displayed. – user2574723 Sep 28 '13 at 06:21
0

This does not use an array adapter. I do not remember the post I must credit for this but here it is

    public void saveArray(int[] value) {
      SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
      SharedPreferences.Editor mEdit = sharedPrefs.edit();

      for (int i = 0; i < value.length; i++) {
        mEdit.putInt("myArrayKey" + i, value[i]);
      }
      mEdit.commit();
     }

    public int[] loadArray() {
      int[] value = new int[Constant.PAGES + 1];
      SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);

      for (int i = 0; i < Constant.PAGES + 1; i++) {
        String myString = "myArrayKey" + i;
        value[i] = sharedPrefs.getInt(myString, 0);
      }
      return value;
    }
Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
0
I assigned the value to k outside the loop and it is working now. Here is the code. Thanks.


package com.example.todolist;

import java.util.ArrayList;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class EditList extends Activity implements View.OnClickListener {

    ArrayList<String> items;
    ArrayAdapter<String> adapter;
    Object ob;

    ListView l;
    EditText et;
    Button mAdd, mDelete;

    int i = 0, k;

    SharedPreferences prfs;
    Editor edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.add_list);

        initialize();

        prfs = PreferenceManager.getDefaultSharedPreferences(this);

        edit = prfs.edit();
        k = prfs.getInt("counter", 0);

        items = new ArrayList<String>();

        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, items);

        l.setAdapter(adapter);

        mDelete.setOnClickListener(this);

        mAdd.setOnClickListener(this);


        for (i = 0; i < k; i++) {

            items.add(0, (String) prfs.getString(String.valueOf(i), "null"));
            adapter.notifyDataSetChanged();

        }

    }

    private void initialize() {

        et = (EditText) findViewById(R.id.editText1);
        mAdd = (Button) findViewById(R.id.button1);
        mDelete = (Button) findViewById(R.id.button2);

        l = (ListView) findViewById(R.id.listView1);

    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        switch (arg0.getId()) {

        case R.id.button1:
            addItem();
            break;

        case R.id.button2:
            i = 0;
            adapter.clear();
            edit.clear();
            edit.commit();
            finish();
            break;

        }

    }

    private void addItem() {

        if (!("".equals(et.getText().toString()))) {

            ob = et.getText().toString();
            items.add(0, (String) ob);
            adapter.notifyDataSetChanged();

            edit.putString(String.valueOf(i), et.getText().toString());
            edit.putInt("counter", i + 1);
            edit.commit();

            et.setText("");

            i = i + 1;

        } else {

            Toast.makeText(getBaseContext(), "Field should not be empty!",
                    Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
user2574723
  • 125
  • 10