动态命名的基础上的EditText输入对象​​? [英] Dynamically name objects based on EditText input?

查看:170
本文介绍了动态命名的基础上的EditText输入对象​​?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个金融Android应用程序将打开,并要求用户添加帐户。 (这将永远是在页面上静态的。)在主要​​活动,将旁边有一个按钮一个EditText框(添加帐户)。当按钮为pressed,我想创建了一个新的对象,然后它会被存储到一个ArrayList。帐户列表(如添加),然后将下面的循环(对应的动态按钮编辑帐户)。这是我的做法/未完code。这是非常原始的在这一点上!

I am creating a finance Android app that will open up and ask the user to add an account. (This will always be static on the page.) On the Main activity, it will have an EditText box next to a Button ("Add Account"). When the Button is pressed, I want a new Object created and it will then be stored into an ArrayList. The List of accounts (as they are added) will then be looped below (with corresponding dynamic buttons to edit the account). This is my practice/unfinished code. It is very raw at this point!

String accountName = (Whatever is in EditText Box)

ArrayList<Accounts> accountList = new ArrayList<Accounts>();
int accountListSize = accountList.size();

(Button on Click)  {
    Account{accountName} = new Account();  // Not sure how to dynamically name
    accountList.add({accountName}) // Not sure how to dynamically name
}

// iterate through finance loop

for(int i = 0; i < accountList .size(); i++)
{
  // do stuff - Create Dynamicly Edit and Clear Buttons for each account
}

其中的一个大问题,我试图解决的是如何动态名称的对象?

One of the big issues I am trying to overcome is how to name an Object Dynamically?

我是不是过想这个过程的整体,使之更难比它应该是什么?我要创建一个类来处理账户细节。我最终要保持所存储的数据 - 所以,也许我应该报废面向对象的风格和使用SQLite? Shared- preferences?

Am I over-thinking this process overall and making it harder than it should be? I am going to create a class to handle the account specifics. I eventually have to keep the data stored--so maybe should I scrap the Object orientated style and use SQLite? Shared-preferences?

任何code样品将是巨大的,但我主要是希望能找到方法,建议我应该采取。

Any code samples would be great, but I am mostly hoping to find the recommend method I should take.

推荐答案

我建议创建一个账户对象,取名字的构造。例如:

I would recommend to create an Account object that takes the name in the constructor. For example:

public class Account {
    private String name;

    public Account( String name ) {
        this.name = name;
    }

    // ... other account related methods here...
    public String getName() {
        return name;
    }
}

然后在你上面的code:

Then in your code above:

List<Account> accountList = new ArrayList<Account>();

(Button on Click)  {
    Account anAccount = new Account( accountName );  // accountName taken from text box.
    accountList.add( anAccount );  
}

然后通过帐户列表循环:

Then to loop through the account list:

for( Account account : accountList ) {
    String name = account.getName();
    // .. do whatever you need to for each account...
}

一旦你有了Account对象的这份名单中,你可以做任何你需要做的与他们,比如在SQLite的DB存储供以后,等...

Once you have this list of Account objects, you can do anything you need to do with them, such as storing in SQLite DB for later, etc...

希望这有助于...

这篇关于动态命名的基础上的EditText输入对象​​?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆