如何停止替换共享首选项中的先前条目?

时间:2019-10-22 18:08:49

标签: java android android-studio sharedpreferences

我的代码用来替换前面的条目,我意识到我需要使用不同的键来存储共享首选项。现在,我的代码在列表视图中不输出任何内容。请帮助

我要求提供有关此人的信息(姓名,收藏夹颜色,收藏夹食物)的Java代码

public class personInfo extends AppCompatActivity {

    EditText editText_name;
    EditText editText_favfood;
    EditText editText_favcolor;
    Button button_save;
    static int count = 0;
    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personinfo);
        Log.d(MainActivity.class.getSimpleName(), "onCreate");

        editText_name = (EditText) findViewById(R.id.editText_name);
        editText_favcolor = (EditText) findViewById(R.id.editText_favcolor);
        editText_favfood = (EditText) findViewById(R.id.editText_favfood);
        button_save = (Button) findViewById(R.id.button_save);


        button_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                count++;
                SharedPreferences sharedPreferences = getSharedPreferences("ENTRIES", 0);
                SharedPreferences.Editor editor = sharedPreferences.edit();

                    editor.putString("name" + count, editText_name.getText().toString());
                    editor.putString("favcolor" + count, editText_favcolor.getText().toString());
                    editor.putString("favfood" + count, editText_favfood.getText().toString());

                    editor.apply();
                    editor.putInt("numOfEntries", count);


                Intent it = new Intent(personInfo.this, listOfPeople.class);
                startActivity(it);

            }
        });
    }
}

Java代码,应该显示条目的页面

public class listOfPeople extends AppCompatActivity {

    ListView listView;
    ArrayList<listEntry> list = new ArrayList<>();
    listEntry le;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        listView = (ListView) findViewById(R.id.listView_persons);

        SharedPreferences sharedPreferences = getSharedPreferences("ENTRIES", MODE_PRIVATE);
        int count = sharedPreferences.getInt("numOfEntries", 0);

        for(int i = 1; i <= count; i++){
            String nameValue = sharedPreferences.getString("name" + i, "");
            String favcolorValue = sharedPreferences.getString("favcolor" + i, "");
            String favfoodValue = sharedPreferences.getString("favfood" + i, "");

            le = new listEntry(nameValue, favcolorValue, favfoodValue);
            list.add(le);
        }

        personListAdapter adapter = new personListAdapter(this, R.layout.entryrow, list);
        listView.setAdapter(adapter);
    }
}

2 个答案:

答案 0 :(得分:0)

您要先放置(保存)偏好设置,然后再放置count

 editor.apply();
 editor.putInt("numOfEntries", count);

在申请之前就放

 editor.putInt("numOfEntries", count);
 editor.apply();

答案 1 :(得分:0)

在editor.putInt(“ numOfEntries”,count)之后调用editor.apply();

editor.putInt("numOfEntries", count);
editor.apply();