1

我正在尝试使用 SharedPreferences 来存储来自 ArrayList 的数据。但我迷路了。我正在使用 for 循环来检索数据并将其存储在列表视图中。此外,当我按下按钮时,首选项会被存储。

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;
    }

}
4

4 回答 4

0

这不使用阵列适配器。我不记得我必须为此归功于的帖子,但在这里

    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;
    }
于 2013-09-28T07:40:49.017 回答
0

getSharedPreferences 的参数是文件名、模式。不要放我放0。

SharedPreferences prfs = getSharedPreferences("PREFS", 0);
于 2013-09-28T06:09:50.463 回答
0

改成:

 SharedPreferences prfs;//Declare it globally

把它放在 onCreate()

prfs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

在 bt 的 setOnClickListener 中;

            Editor edit = prfs.edit();

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

在 for 循环中

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


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

        }
于 2013-09-28T06:01:00.553 回答
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;
    }

}
于 2013-09-28T16:01:11.617 回答