无法在Button的点击中保存我的ArrayList

时间:2014-08-29 17:30:42

标签: java android arraylist save sharedpreferences

这是一个简单的应用程序,需要获取位置,从arraylist添加/删除它们并使用Sharedpreferences而不是其他存储方法将列表保存在单击按钮上。 出于某种原因,它只是没有保存任何东西。 这是我的代码:

    package com.example.pearls1;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

    ArrayList<String> list = new ArrayList<String>();
    ArrayAdapter<String> adapter;
    SharedPreferences sharedPreferences;
    public int ItemCounter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener locListener = new MyLocationListener();
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                locListener);

        Button buttonAdd = (Button) findViewById(R.id.buttonAdd);
        Button buttonDelete = (Button) findViewById(R.id.buttonDelete);
        Button buttonSave = (Button) findViewById(R.id.buttonSave);

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

        // Opening the sharedPreferences object
        sharedPreferences = getSharedPreferences("SaveListItem", 0);
        SharedPreferences mypreferences = getSharedPreferences(
                "myPrefs", Context.MODE_PRIVATE);

        for (int Counter=0; Counter< sharedPreferences.getInt("Count", 0); Counter++)
        {
            list.add(sharedPreferences.getString("Item" + Integer.toString(Counter), ""));
        }

        OnClickListener listenerAdd = new OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText edit = (EditText) findViewById(R.id.PearlNumber);
                EditText edit1 = (EditText) findViewById(R.id.txtLatitude);
                EditText edit2 = (EditText) findViewById(R.id.txtLongitude);

                list.add("Pearl Number:" + edit.getText().toString() + "   X:"
                        + edit1.getText().toString() + "   Y:"
                        + edit2.getText().toString());
                // adapter.notifyDataSetChanged();
                SaveItem(edit.getText().toString(), edit1.getText().toString(),
                        edit2.getText().toString());
            };

        };

        OnClickListener listenerDel = new OnClickListener() {
            @Override
            public void onClick(View v) {
                SparseBooleanArray checkedItemPositions = getListView()
                        .getCheckedItemPositions();
                int itemCount = getListView().getCount();

                for (int i = itemCount - 1; i >= 0; i--) {
                    if (checkedItemPositions.get(i)) {
                        adapter.remove(list.get(i));
                    }
                }
                checkedItemPositions.clear();
                adapter.notifyDataSetChanged();

            }
        };

        OnClickListener listenerSave = new OnClickListener() {
            @Override
            public void onClick(View v) {
                sharedPreferences = getSharedPreferences("SaveListItem", 0);
                SharedPreferences mypreferences = getSharedPreferences(
                        "myPrefs", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = mypreferences.edit();
                editor.putInt("Count", list.size());
                for (int i = 0; i < ItemCounter; i++) {
                    editor.putString("Item" + Integer.toString(i), list.get(i));
                }
                editor.commit();
            }

        };

        buttonAdd.setOnClickListener(listenerAdd);
        buttonDelete.setOnClickListener(listenerDel);
        buttonSave.setOnClickListener(listenerSave);
        setListAdapter(adapter);

    }

    private void SaveItem(String pearl, String Anumber, String Bnumber) {
        sharedPreferences = getSharedPreferences("SaveListItem", 0);

        ItemCounter++;

        /** Opening the editor object to write data to sharedPreferences */
        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putString("pearl" + Integer.toString((ItemCounter - 1)), pearl);
        editor.putString("Anumber" + Integer.toString((ItemCounter - 1)),
                Anumber);
        editor.putString("Bnumber" + Integer.toString((ItemCounter - 1)),
                Bnumber);

        // Storing the count of Item count
        editor.putInt("ItemCounter", ItemCounter);
        /** Saving the values stored in the shared preferences */
        editor.commit();

    }
package com.example.pearls1;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

    ArrayList<String> list = new ArrayList<String>();
    ArrayAdapter<String> adapter;
    SharedPreferences sharedPreferences;
    public int ItemCounter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener locListener = new MyLocationListener();
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                locListener);

        Button buttonAdd = (Button) findViewById(R.id.buttonAdd);
        Button buttonDelete = (Button) findViewById(R.id.buttonDelete);
        Button buttonSave = (Button) findViewById(R.id.buttonSave);

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

        // Opening the sharedPreferences object
        sharedPreferences = getSharedPreferences("SaveListItem", 0);
        SharedPreferences mypreferences = getSharedPreferences(
                "myPrefs", Context.MODE_PRIVATE);

        for (int Counter=0; Counter< sharedPreferences.getInt("Count", 0); Counter++)
        {
            list.add(sharedPreferences.getString("Item" + Integer.toString(Counter), ""));
        }

        OnClickListener listenerAdd = new OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText edit = (EditText) findViewById(R.id.PearlNumber);
                EditText edit1 = (EditText) findViewById(R.id.txtLatitude);
                EditText edit2 = (EditText) findViewById(R.id.txtLongitude);

                list.add("Pearl Number:" + edit.getText().toString() + "   X:"
                        + edit1.getText().toString() + "   Y:"
                        + edit2.getText().toString());
                // adapter.notifyDataSetChanged();
                SaveItem(edit.getText().toString(), edit1.getText().toString(),
                        edit2.getText().toString());
            };

        };

        OnClickListener listenerDel = new OnClickListener() {
            @Override
            public void onClick(View v) {
                SparseBooleanArray checkedItemPositions = getListView()
                        .getCheckedItemPositions();
                int itemCount = getListView().getCount();

                for (int i = itemCount - 1; i >= 0; i--) {
                    if (checkedItemPositions.get(i)) {
                        adapter.remove(list.get(i));
                    }
                }
                checkedItemPositions.clear();
                adapter.notifyDataSetChanged();

            }
        };

        OnClickListener listenerSave = new OnClickListener() {
            @Override
            public void onClick(View v) {
                sharedPreferences = getSharedPreferences("SaveListItem", 0);
                SharedPreferences mypreferences = getSharedPreferences(
                        "myPrefs", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = mypreferences.edit();
                editor.putInt("Count", list.size());
                for (int i = 0; i < ItemCounter; i++) {
                    editor.putString("Item" + Integer.toString(i), list.get(i));
                }
                editor.commit();
            }

        };

        buttonAdd.setOnClickListener(listenerAdd);
        buttonDelete.setOnClickListener(listenerDel);
        buttonSave.setOnClickListener(listenerSave);
        setListAdapter(adapter);

    }

    private void SaveItem(String pearl, String Anumber, String Bnumber) {
        sharedPreferences = getSharedPreferences("SaveListItem", 0);

        ItemCounter++;

        /** Opening the editor object to write data to sharedPreferences */
        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putString("pearl" + Integer.toString((ItemCounter - 1)), pearl);
        editor.putString("Anumber" + Integer.toString((ItemCounter - 1)),
                Anumber);
        editor.putString("Bnumber" + Integer.toString((ItemCounter - 1)),
                Bnumber);

        // Storing the count of Item count
        editor.putInt("ItemCounter", ItemCounter);
        /** Saving the values stored in the shared preferences */
        editor.commit();

    }

1 个答案:

答案 0 :(得分:0)

这是一个显示已保存项目的代码。我删除了一些不相关的内容。无论如何,你使用2个字符串保存计数&#34; itemCount&#34;和&#34;计数&#34;。尝试做这样的事情:

package com.example.stackoverflow;

import android.app.ListActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;

public class MyActivity extends ListActivity {

  private static final String SAVE_LIST_ITEM = "SaveListItem";
  private static final String COUNT = "Count";
  ArrayList<String> list = new ArrayList<String>();
  ArrayAdapter<String> adapter;
  SharedPreferences sharedPreferences;
  public int itemCounter = 0;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
Button buttonAdd = (Button) findViewById(R.id.buttonAdd);
Button buttonDelete = (Button) findViewById(R.id.buttonDelete);
Button buttonSave = (Button) findViewById(R.id.buttonSave);

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

// Opening the sharedPreferences object
sharedPreferences = getSharedPreferences(SAVE_LIST_ITEM, MODE_PRIVATE);
itemCounter = sharedPreferences.getInt(COUNT, 0);
for (int i = 0; i < itemCounter; i++) {
  list.add(sharedPreferences.getString("Item" + Integer.toString(i), ""));
}

buttonAdd.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    EditText edit = (EditText) findViewById(R.id.PearlNumber);
    EditText edit1 = (EditText) findViewById(R.id.txtLatitude);
    EditText edit2 = (EditText) findViewById(R.id.txtLongitude);

    list.add("Pearl Number:" + edit.getText().toString() + "   X:"
        + edit1.getText().toString() + "   Y:"
        + edit2.getText().toString());
    // adapter.notifyDataSetChanged();
    saveItem(edit.getText().toString(), edit1.getText().toString(),
        edit2.getText().toString());
  }
});

buttonDelete.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    SparseBooleanArray checkedItemPositions = getListView()
        .getCheckedItemPositions();
    int itemCount = getListView().getCount();

    for (int i = itemCount - 1; i >= 0; i--) {
      if (checkedItemPositions.get(i)) {
        adapter.remove(list.get(i));
      }
    }
    checkedItemPositions.clear();
    adapter.notifyDataSetChanged();
  }
});

buttonSave.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(COUNT, list.size());
    for (int i = 0; i < itemCounter; i++) {
      editor.putString("Item" + Integer.toString(i), list.get(i));
    }
    editor.commit();
  }
});

setListAdapter(adapter);

}

private void saveItem(String pearl,String Anumber,String Bnumber){     itemCounter ++;

/** Opening the editor object to write data to sharedPreferences */
SharedPreferences.Editor editor = sharedPreferences.edit();

editor.putString("pearl" + Integer.toString((itemCounter - 1)), pearl);
editor.putString("Anumber" + Integer.toString((itemCounter - 1)),
    Anumber);
editor.putString("Bnumber" + Integer.toString((itemCounter - 1)),
    Bnumber);

// Storing the count of Item count
editor.putInt(COUNT, itemCounter);
/** Saving the values stored in the shared preferences */
editor.commit();

} }