从另一个类向arraylist添加对象

时间:2016-12-21 17:31:41

标签: java android object arraylist

我遇到了问题。我想添加一个名为Transactions的对象,就是这个:

public class Transaction {
private float value;
private int transaction_date;
private String description;

public Transaction(float value, int transaction_date, String description){
    super();
    this.value = value;
    this.transaction_date = transaction_date;
    this.description = description;
}

public float getValue(){
    return value;
}
public int getTransaction_date(){
    return transaction_date;
}
public String getDescription(){
    return description;
}

ArrayList中的main activity。 好吧到目前为止还没有那么多问题。但是,如何从我想设置所需变量的其他活动(在我的情况下是值,transaction_date和description)中执行此操作? 这是我的活动,我想设置值(是的,我知道还没有变量输入):

public class AddMoneyTransaction extends AppCompatActivity {

EditText depositInputSix;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_money_transaction);

    //transaction list

    populateTransactionList();
    populateListView();

    //setup the button

    Button addDepositButton;
    addDepositButton = (Button)findViewById(R.id.addDepositButton);

    //make it work

    addDepositButton.setOnClickListener(
            new View.OnClickListener()
            {
                public void onClick(View view) {


                }
            });
}

//populated transaction list

protected void populateTransactionList() {

    myTransactions.add(new Transaction(1,19,"monday"));
    myTransactions.add(new Transaction(2,20,"tuesday"));
    myTransactions.add(new Transaction(3,21,"wednesday"));


}

//populated list view

private void populateListView() {
    ArrayAdapter<Transaction> adapter = new AssetsOverview.LastTransactionsListAdapter();
    ListView list = (ListView) findViewById(R.id.last_transactions_listView);
    list.setAdapter(adapter);
}

private class LastTransactionsListAdapter extends ArrayAdapter<Transaction>{
    public LastTransactionsListAdapter(){
        super(AddMoneyTransaction.this, R.layout.transaction_list_view, myTransactions);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        //make sure there is a view to work with (maybe null)
        View itemView = convertView;
        if (itemView == null){
            itemView = getLayoutInflater().inflate(R.layout.transaction_list_view, parent, false);
        }

        //find a transaction to work with

        Transaction currentTransaction = myTransactions.get(position);

        // fill the view:
        //    value
        TextView valueView = (TextView) itemView.findViewById(R.id.transactionValueView);
        valueView.setText(currentTransaction.getValue() + "$");

        //    date
        TextView dateView = (TextView) itemView.findViewById(R.id.transactionDateView);
        dateView.setText(currentTransaction.getTransaction_date() + "");

        //    description
        TextView descriptionView = (TextView) itemView.findViewById(R.id.transactionDesciptionView);
        descriptionView.setText(currentTransaction.getDescription());

        return itemView;
    }
}

我希望有人可以帮助我,因为我没有找到任何帮助。如果您需要更多信息,请告诉我......

1 个答案:

答案 0 :(得分:0)

我假设您的 myTransactions 列表是在父类AppCompatActivity中定义的。

中的问题
addDepositButton.setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            {

            }
        });

似乎 myTransactions 未定义为最终变量。

当您创建新的匿名 OnClickListener 时,编译器所做的是它为 OnClickListener 构造函数提供对 myTransactions 的引用,如果< em> myTransactions 在 onClick 方法中使用。此 myTransactions 以后无法更改,因此必须是最终的。

因此,对于解决方案,我会尝试在 myTransactions 的定义中添加最终关键字。