【问题标题】:How to stop replacing previous entries in shared preferences?如何停止替换共享首选项中的先前条目?
【发布时间】:2019-10-22 18:08:49
【问题描述】:

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

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

【问题讨论】:

  • 但这被认为是不好的做法,在这种情况下,您应该使用 sqlite 数据库进行简单方便的 CRUD 操作,并且对未来的项目很有用

标签: java android android-studio sharedpreferences


【解决方案1】:

您在输入count之前应用(保存)首选项

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

申请前先放

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      相关资源
      最近更新 更多