Button to save certain listview

91 views Asked by At

I have a save button with this code

    @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());
}

I also have 2 activities, with 2 listViews, with the same ID of @android:id/list.
When i press save, it updates my both listViews.
How could i edit the code so it updates the desired listView(on certain activity)?

EDIT This is the main activity, which display the first 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

There are 0 answers