我有问题我要添加一个名为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;
}
}
我希望有人可以帮助我,因为我没有发现任何帮助。请告诉我您是否需要更多信息...
最佳答案
我假设您的myTransactions列表是在父类AppCompatActivity中定义的。问题在
addDepositButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
}
});
似乎myTransactions没有定义为最终变量。
创建新的匿名OnClickListener时,如果在onClick方法中使用了myTransactions,则编译器将为OnClickListener构造函数提供对myTransactions的引用。此myTransactions以后不能更改,因此必须是最终的。
因此,对于该解决方案,我将尝试在myTransactions的定义中添加final关键字。