用于保存某些列表视图的按钮

时间:2015-06-08 14:30:17

标签: android listview android-listview

我有一个带有此代码的保存按钮

    @Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.save_button:;
        String accountNameStr = accountName.getText().toString();


        // Ensure all the required data has been entered before saving the
        // account
        if (accountNameStr.trim().length() == 0) {
            Toast.makeText(this, R.string.account_name_required_error, Toast.LENGTH_SHORT).show();
        } else {

            // If editing this account then ensure another account doesn't exist with this same name
            if (mode == EDIT_MODE) {

                AccountInformation accountToEdit =
                        getPasswordDatabase().getAccount(this.accountToEdit);

                AccountInformation secondAccount = getPasswordDatabase().getAccount(accountNameStr);
                if (secondAccount != null && secondAccount != accountToEdit) {
                    Toast.makeText(this, getString(R.string.account_already_exists_error),  Toast.LENGTH_SHORT).show();
                } else {
                    saveAccount(accountNameStr);
                }

            } else { // must be adding account

                // Check if an account with this name already exists
                if (getPasswordDatabase().getAccount(accountNameStr) != null) {
                    Toast.makeText(this, getString(R.string.account_already_exists_error),  Toast.LENGTH_SHORT).show();
                } else {
                    saveAccount(accountNameStr);
                }
            }
        }

        break;
    case R.id.cancel_button:
        this.finish();
        break;
    }
}

private void saveAccount(final String accountName) {
    String useridBytes = userid.getText().toString();
    String passwordBytes = password.getText().toString();
    String urlBytes = url.getText().toString();
    String notesBytes = notes.getText().toString();

    AccountInformation ai = new AccountInformation(
            accountName, useridBytes,
            passwordBytes, urlBytes, notesBytes);

    // If editing an account then delete the exiting one before adding it again
    if (mode == EDIT_MODE) {
        getPasswordDatabase().deleteAccount(this.accountToEdit);
        // Put the edited account back on the ViewAccountDetails
        // activity so that the view can be re-populated with the
        // edited details
        ViewAccountDetails.account = ai;
    }

    getPasswordDatabase().addAccount(ai);
    new SaveDatabaseAsyncTask(this, new Callback() {
        @Override
        public void execute() {
            // If the account name has changed or we're added a new account
            // then pass back a value instructing the FullAccountList to
            // refresh the list of accounts
            if (!accountName.equals(originalAccountName) || mode == ADD_MODE) {
                setResult(EDIT_ACCOUNT_RESULT_CODE_TRUE);
            }
            AddEditAccount.this.finish();
        }
    }).execute(getPasswordDatabase());
}

我还有2个活动,有2个listView,ID为 @android:id / list
当我按保存时,它会更新我的两个listViews 我怎么能编辑代码,以便更新所需的listView(在某些活动上)?

编辑这是主要活动,显示第一个ListView:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    registerForContextMenu(getListView());
    populateAccountList(); // this right here is what i believe is populating the list on the activity

    dugme = (ImageButton) findViewById(R.id.btn2);
    dugme.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Intent myIntent = (new Intent(FullAccountList.this,
                    main2.class).addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));

            startActivity(myIntent);
            overridePendingTransition(0,0);
            finish();


        }
    });



}

    private void populateAccountList() { // this is the source of it.
    if (getPasswordDatabase() == null) {
        // If the UPM process was restarted since AppEntryActivity was last
        // run then databaseFileToDecrypt won't be set so set it here.
        EnterMasterPassword.databaseFileToDecrypt = Utilities.getDatabaseFile(this);

        setResult(RESULT_ENTER_PW);
        finish();
    } else {
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getPasswordDatabase().getAccountNames())); // maybe here i can somehow declare what list to populate?
    }
}

0 个答案:

没有答案