重命名ListView项目

时间:2015-12-25 14:51:05

标签: android listview sharedpreferences rename

如何在ListView中重命名项目,然后更新SharedPreferences以便活动(打开后)加载以前的内容而不是空白活动?

我尝试删除该项目,然后在已删除项目的位置添加新项目...点击后,它总是会打开一个新的空白活动...我怎么能让这个工作?提前致谢

switch (item.getItemId()) {

        case R.id.rename:


            final AlertDialog.Builder alert = new AlertDialog.Builder(getView().getContext());
            alert.setTitle("Rename");
            final EditText editText = new EditText(getActivity().getApplicationContext());
            editText.setTextColor(Color.BLACK);
            alert.setView(editText);

            jsonArray = new JSONArray();
            jsonArrayName = new JSONArray();

            try {

                jsonArray = new JSONArray(preferences.getString("ListNotes", jsonArray.toString()));
                subCategoryName = jsonArray.getString(id);

                jsonArrayName = new JSONArray(preferences_notes.getString("subjectNotes", jsonArrayName.toString()));
                subCategoryNameRename = jsonArrayName.getString(id);


            } catch (Exception e) { }

            editText.setText(subCategoryName);

            for(int i = 0; i < jsonArrayName.length(); i++) {
                try {
                    arrayListItemName.add(jsonArrayName.get(i).toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            for (int i = 0; i < jsonArray.length(); i++) {

                try {
                    arrayListRename.add(jsonArray.get(i).toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    newSubCategoryName = editText.getText().toString();

                    subCategoryName = newSubCategoryName;
                    subCategoryNameRename = subCategoryName;

                    arrayListRename.remove(id);
                    arrayListRename.add(id, newSubCategoryName);

                    Toast.makeText(getActivity().getApplicationContext(), "Updated text: " + subCategoryName, Toast.LENGTH_SHORT).show();


                    editorNotes.putString("subjectNotes", subCategoryNameRename).apply();
                    editor.putString("ListNotes", arrayListRename.toString()).apply();

                    m_adapter_notes.notifyDataSetChanged();
                    onResume();
                }
            });

0 个答案:

没有答案