0

メニューから新しい項目を追加するオプションを使用してこの ListView を作成していますが、Stackoverflow ですべてのコード/例を既に読んでおり、コードをそれらに適応させることができませんでした。

を使おうとしたonSaveInstanceStateのですが、 で使おうとするとアプリを起動できませんでしたonCreate

入力した新しい値でこのリストを保存する最良の方法は何ですか?

私が使用しようとしたコード:

protect void onCreate(...)
{
     ...
     if (savedInstanceState.containKey(MYLISTKEY))
     {
          alllist = savedInstanceState.getStringArrayList(MYLISTKEY);
     } else {

     }
}

これが私のコードです:

public class MainActivity extends Activity {
    private static final String MYLISTKEY = "myListItems";
    final Context context = this;
    private ListView mainListView;
    private ArrayAdapter<String> listAdapter;`
    private ArrayList<String> allList = new ArrayList<String>();

    @Override
    protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        outState.putStringArrayList(MYLISTKEY, allList);
    }

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

        // Find the ListView resource.
        mainListView = (ListView) findViewById(R.id.mainListView); 

        // TODO: Populate explicitely your list
        // Create and populate a List of planet names.
        String[] listitems = new String[] { };  
        allList.addAll(Arrays.asList(listitems));
        // Create ArrayAdapter using the planet list.
        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, allList);

        // Add more planets. If you passed a String[] instead of a List<String> 
        // into the ArrayAdapter constructor, you must not add more items. 
        // Otherwise an exception will occur.
        listAdapter.add( "Item A" );
        listAdapter.add( "Item B" );

        // Set the ArrayAdapter as the ListView's adapter.
        mainListView.setAdapter( listAdapter );
    }



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

    /* (non-Javadoc)
     * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        // Action Add Item to the listview
        case R.id.action_add:
            addListItem();
            return true;
            // Save list to SharedPreferences
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    // Add new item list when choose "New Item" on menu
    private void addListItem() {
        // get prompt.xml view
        LayoutInflater li = LayoutInflater.from(context);
        View promptsView = li.inflate(R.layout.prompt, null);

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

        // set prompt.xml to alertdialog builder
        alertDialogBuilder.setView(promptsView);

        final EditText userInput = (EditText) promptsView.findViewById(R.id.editTextDialogUserInput);

        // set dialog message
        alertDialogBuilder
        .setCancelable(false)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // get user input and set it to result
                // edit text
                listAdapter.add(userInput.getText().toString());

                listAdapter.notifyDataSetChanged();

            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                dialog.cancel();
            }
        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    }
4

1 に答える 1

0

あなたのコメント// Save list to SharedPreferencesが示唆しているように、リスト内のデータを何らかの永続ストレージに保存し、savedInstanceState バンドルに依存しないようにする必要があります。

Android dev docs note re を参照してください。onSaveInstanceState に保存するもの:

注: onSaveInstanceState() は必ず呼び出されるとは限らないため、アクティビティの一時的な状態 (UI の状態) を記録するためにのみ使用する必要があります。永続的なデータを保存するために使用することはできません。代わりに、 onPause() を使用して、ユーザーがアクティビティを離れたときに永続データ (データベースに保存する必要があるデータなど) を保存する必要があります。

http://developer.android.com/guide/components/activities.html

于 2013-05-25T21:53:14.840 に答える