0
我有兩個活動。在CreateAccount
課程中,我正在製作一個listView
,其中包含用戶保存的所有帳戶。在Accounts類中,我正在保存數據,該數據類似於CreateAccount
類中的listView
中的新行。如何從單擊的ListView項目編輯保存的數據?
我希望當一個listView
項目被選中,能夠編輯保存的信息,並按下Save按鈕保存新的數據。我的問題是,當我編輯保存的信息並單擊「保存」按鈕時,它不會更改此帳戶,但會使用更改後的信息創建另一個帳戶。你能幫我解決這個問題嗎?
這裏是我的代碼
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()));
}
}
}
你是什麼意思的「把方法放在同一把鑰匙」? – user2515973
讓我們假設您已經在關鍵account_name_1下存儲了一個帳戶。然後,您要編輯並保存該帳戶。在保存onclick監聽器中,您將其添加到account_name_2並保存。因此,另一個項目account_name_2存儲在SharedPreferences中,而不是編輯account_name_1並根據值。 – th0m4d
是的,但我怎麼改變這個?如果我沒有這個+數字,我不會知道我是否打開account_1或account_2。 – user2515973