0

In my app, I have one listview with one string "Add New String". When user selects this, then Alert dialog prompts with one edit text and button. User has to write name of new String in this box and when user clicks on button that must be added to original list. Till this I have done. but my problem is when user closes this app, and restarts, then that new added string is not there in List. I have to do this with shared preference.How can it be done with it? Here is my code.

MainActivity .java

public class MainActivity extends Activity {

    ListView lv;
    String add[]=new String[]{"Add new List"}; 
    final private static int DIALOG_LOGIN = 1;
    ArrayList<String> arr1;
    ArrayAdapter<String> adapter1;

    ArrayList<String> arr2 = new ArrayList<String>();
    ArrayAdapter<String> adapter2;

    int count = 0;
    int i=0;

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

        lv = (ListView)findViewById(R.id.listView1);
        adapter1 = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, add);
        lv.setAdapter(adapter1);
        //onLoad();

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                String data = lv.getItemAtPosition(arg2).toString();
                if(data == "Add new List")
                {
                     count = count + 1;
                     showDialog(DIALOG_LOGIN);
                }
            }
        });

    }


      @Override
     protected Dialog onCreateDialog(int id)
      {

      AlertDialog dialogDetails = null;

      switch (id)
      {
        case DIALOG_LOGIN:
        LayoutInflater inflater = LayoutInflater.from(this);
        View dialogview = inflater.inflate(R.layout.dialog, null);
        AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
        dialogbuilder.setView(dialogview);
        dialogDetails = dialogbuilder.create();
        break;
      }

      return dialogDetails;
     }

    @Override
     protected void onPrepareDialog(int id, Dialog dialog) {

      switch (id) {
      case DIALOG_LOGIN:
       final AlertDialog alertDialog = (AlertDialog) dialog;
       Button btnOk = (Button) alertDialog.findViewById(R.id.btnOk);
       final EditText etList = (EditText) alertDialog.findViewById(R.id.etList);


       btnOk.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(etList.length() <= 0)
            {
                etList.requestFocus();
                Toast.makeText(getBaseContext(), "Enter Name of List", 1000).show();
            }
            else
            {
                String name = etList.getText().toString();
                String newName[] = new String[]{};

                if(arr2.contains("Add new List"))
                {

                    arr2.add(name);
                    Toast.makeText(getBaseContext(), "" + name , 1000).show();
                    adapter2 = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, arr2);
                    lv.setAdapter(adapter2);
                    adapter2.notifyDataSetChanged();
                    etList.setText("");
                    Toast.makeText(getBaseContext(), "" + count, 1000).show();

                    dismissDialog(DIALOG_LOGIN);
                }
                else
                {
                    arr2.add("Add new List");
                    arr2.add(name);
                    adapter2 = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, arr2);
                    lv.setAdapter(adapter2);
                    adapter2.notifyDataSetChanged();
                    etList.setText("");
                    dismissDialog(DIALOG_LOGIN);
                }
                }
    }
    });

       break;
      }
     }  

    }

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/etList"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="16dp"
        android:ems="10"
        android:hint="@string/etHint" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnOk"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/etList"
        android:text="@string/ok" />

</RelativeLayout>
4

1 回答 1

0

btnOk您可以通过您的方法将数据保存到共享首选项onClick()。您可以在方法中保存数据之前arr2.add(name);的行onClick()

共享偏好的使用。

要获得共享偏好,

SharedPreferences prefs = this.getSharedPreferences(
      "com.example.app", Context.MODE_PRIVATE);

要编辑和保存首选项,

String myString = "TextHere";
prefs.edit().putString("com.example.app.myValue", myString).commit();

要阅读偏好,

String myValue= "com.example.app.myValue";

// use a default value using ""
String s = prefs.getString(myValue, "");
于 2013-07-25T09:27:02.207 回答