Как перестать заменять предыдущие записи в общих настройках? - PullRequest
0 голосов
/ 22 октября 2019

Мой код использовался для замены предыдущих записей, и я понял, что мне нужно использовать разные ключи для хранения в общих настройках. Теперь мой код ничего не выводит в списке. пожалуйста, помогите

Java-код, где я прошу информацию о человеке (имя, favcolor, favfood)

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 голосов
/ 22 октября 2019

вызовите editor.apply () после editor.putInt ("numOfEntries", count);

editor.putInt("numOfEntries", count);
editor.apply();
0 голосов
/ 22 октября 2019

Вы применяете (сохраняете) настройки перед установкой count

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

Просто ставьте перед применением

 editor.putInt("numOfEntries", count);
 editor.apply();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...