0

我有两个活动。在CreateAccount课堂上,我正在创建listView一个用户保存的所有帐户。在 Accounts 类中,我正在保存数据,这些数据出现在listViewfromCreateAccount类的新行中。

我希望在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()));
    }
}
}
4

1 回答 1

2

您的问题是,您使用“account_name_”+ num 更改密钥。SharedPreferences 是一个键值存储。如果将 put 方法应用于相同的键,则可以更改值。

于 2013-06-24T19:13:55.320 回答