我如何删除共享首选项中的项目并更新共享首选项中的当前列表

时间:2018-03-11 15:03:16

标签: android listview sharedpreferences

我正在尝试使用共享首选项删除列表视图中的项目。但是我遇到了问题。当我关闭它之后打开应用程序时,我在关闭应用程序之前已经删除的项目仍然存在。我想永久删除我在共享偏好设置中删除的项目。

如何将当前列表项更新为特定的SharedPreferences键。

这是我的代码:

第一项活动

 package com.example.sy.list;

 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.inputmethod.InputMethodManager;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Toast;

 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Set;


 public class History extends AppCompatActivity {
Button translate;
Button next;
EditText enterText;
private SharedPreferences hPreferences;
ArrayList<String> hisList = new ArrayList<String>();

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

    enterText = findViewById(R.id.editText);
    next = findViewById(R.id.btnNext);

    translate = findViewById(R.id.buttonTranslate);
    View.OnClickListener addlistener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(enterText.getWindowToken(), 0);

            String input = enterText.getText().toString();
            if(input == null){
                Toast.makeText(getBaseContext(),"Input Field is empty",Toast.LENGTH_SHORT).show();
            }else
            {
                hisList.add(input);
                hPreferences  = getSharedPreferences("SaveHistory",Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = hPreferences.edit();
                Set<String> set = new HashSet<String>();
                set.addAll(hisList);
                editor.putStringSet("history",set);
                editor.commit();
                editor.apply();
                enterText.setText("");
            }





        }

    };
    translate.setOnClickListener(addlistener);
    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(History.this,MainActivity.class);
            startActivity(intent);
        }
    });



}

 }

第二项活动

 package com.example.sy.list;

 import android.content.Context;
 import android.content.SharedPreferences;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.ActionMode;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.widget.AbsListView;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;


 public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayAdapter<String> adapter;
private ArrayList<String> toDelete;
ArrayList<String> itemList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


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

    listView = findViewById(R.id.listView);
    listView.setAdapter(adapter);
    final SharedPreferences transHistory  = getSharedPreferences("SaveHistory",Context.MODE_PRIVATE);
    Set<String> tHistory = transHistory.getStringSet("history", null);
    for (String x: tHistory) {
        itemList.add(x);
    }
    adapter.notifyDataSetChanged();

    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

        private int nr = 0;
        @Override
        public void onItemCheckedStateChanged(ActionMode actionMode, int position, long id, boolean checked) {
            if (checked) {
                nr++;
                toDelete.add(adapter.getItem(position));
            } else {
                nr--;
                toDelete.remove(adapter.getItem(position));
            }
            actionMode.setTitle(nr + " selected");
        }

        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            nr = 0;
            MenuInflater menuInflater = getMenuInflater();
            menuInflater.inflate(R.menu.toolbar_cab, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.action_delete:
                    nr = 0;

                    for (String item : toDelete) {
                        transHistory.edit().remove(item).commit();
                        itemList.remove(item);
                    }
                    adapter.notifyDataSetChanged();
                    actionMode.finish();
                    return true;
                default:
                    return false;
            }
        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode) {
            toDelete.clear();
        }

    });
}


   }

1 个答案:

答案 0 :(得分:0)

您只是从列表中删除项目,但不在SharedPreferences中更新。从列表中删除后,将当前列表项更新为特定的SharedPreferences键。