У меня есть две деятельности. В классе CreateAccount
я делаю listView
со всей учетной записью, которую пользователь сохранил. В классе Accounts я сохраняю данные, которые появляются в новой строке в listView
от CreateAccount
.Как отредактировать сохраненные данные из выбранного элемента ListView?
Я хочу, чтобы элемент listView
был выбран для редактирования сохраненной информации и нажатия кнопки «Сохранить» для сохранения новых данных. Моя проблема в том, что когда я редактирую сохраненную информацию и нажимаю кнопку «Сохранить», она не меняет эту учетную запись, а делает другую с измененной информацией. Можете ли вы помочь мне исправить это?
Вот мой код
public class CreateAccount extends ListActivity {
Intent intent;
Button button3;
ListView listView;
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
SharedPreferences preferences;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_account);
listView = (ListView) findViewById(android.R.id.list);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CreateAccount.this, Accounts.class);
startActivity(intent);
}
});
listView = (ListView) findViewById(android.R.id.list);
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(this);
int num = Accounts.num;
num = preferences.getInt("Account_num", num);
List<String> list = new LinkedList<String>();
for (int i = 1; i <= num; i++) {
list.add(preferences.getString("account_name_" + i, ""));
}
listItems.addAll(list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
OnItemClickListener listener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(CreateAccount.this, Accounts.class);
intent.putExtra("position", position);
startActivity(intent);
}
};
listView.setOnItemClickListener(listener);
}
}
public class Accounts extends Activity {
static EditText webSite;
EditText userName;
EditText pass;
Button save;
SharedPreferences preferences;
Intent intent;
static int num;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accounts);
webSite = (EditText) findViewById(R.id.editText2);
userName = (EditText) findViewById(R.id.editText3);
pass = (EditText) findViewById(R.id.editText4);
save = (Button) findViewById(R.id.save_button);
intent = getIntent();
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences app_preferences = PreferenceManager
.getDefaultSharedPreferences(Accounts.this);
SharedPreferences.Editor editor = app_preferences.edit();
int num;
num = app_preferences.getInt("Account_num", 0);
num++;
editor.putInt("Account_num", num);
String list = webSite.getText().toString();
editor.putString("account_name_" + num, list);
editor.putString("account_user_" + num, userName.getText()
.toString());
editor.putString("account_pass_" + num, pass.getText()
.toString());
editor.commit();
Intent myIntent = new Intent(Accounts.this, CreateAccount.class);
startActivity(myIntent);
}
});
num = intent.getIntExtra("position", -1);
num++;
if (num != -1) {
String list = intent.getStringExtra("account_name_");
preferences = PreferenceManager
.getDefaultSharedPreferences(Accounts.this);
webSite.setText(preferences.getString("account_name_" + num,
webSite.getText().toString()));
userName.setText(preferences.getString("account_user_" + num,
userName.getText().toString()));
pass.setText(preferences.getString("account_pass_" + num, pass
.getText().toString()));
}
}
}
Что вы подразумеваете под термином "put methods to the same key"? – user2515973
Предположим, вы сохранили одну учетную запись под ключ account_name_1. Затем вы хотите отредактировать и сохранить эту учетную запись. В save onclick слушатель вы увеличиваете его до account_name_2 и сохраняете его. Таким образом, другой элемент account_name_2 хранится в SharedPreferences вместо редактирования account_name_1 и соответствующих значений. – th0m4d
Да, но как я могу это изменить? Если у меня нет этого + num, я не буду знать, открываю ли я аккаунт_1 или account_2. – user2515973