如何编辑已单击的ListView项目中保存的数据?

时间:2013-06-24 19:01:47

标签: android listview

我有两项活动。在CreateAccount课程中,我正在使用用户保存的所有帐户制作listView。在Accounts类中,我正在保存数据,该数据显示在listViewCreateAccount的新行中。

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

1 个答案:

答案 0 :(得分:2)

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