Reputation: 344
I have the problem that instead of being added, the new items
on my ArrayList
(which are created in another Activity and sent to the Main Activity via an Intent) replace the old ones so I always have only one item shown. This is my Main Activity
:
public class AssetsOverview extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
protected List<Transaction> myTransactions = new ArrayList<Transaction>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_assets_overview);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fabplus = (FloatingActionButton) findViewById(R.id.fabAdd);
fabplus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(AssetsOverview.this, AddMoneyTransaction.class));
}
});
FloatingActionButton fabminus = (FloatingActionButton) findViewById(R.id.fabRemove);
fabminus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(AssetsOverview.this, RemoveMoneyTransaction.class));
}
});
FloatingActionButton fabhint = (FloatingActionButton) findViewById(R.id.fabHint);
fabhint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(AssetsOverview.this, HintMoneyTransaction.class));
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//transaction list
populateTransactionList();
populateListView();
//set a value to the balance variable
float startBalance = 0;
//calculate and set current (actual) balance
TextView balance_view = (TextView) findViewById(R.id.balance_view);
balance_view.setText(startBalance + " $");
}
//populated transaction list
protected void populateTransactionList() {
myTransactions.add(new Transaction(78,98,"halo"));
}
//populated list view
private void populateListView() {
ArrayAdapter<Transaction> adapter = new LastTransactionsListAdapter();
ListView list = (ListView) findViewById(R.id.last_transactions_listView);
list.setAdapter(adapter);
}
private class LastTransactionsListAdapter extends ArrayAdapter<Transaction>{
public LastTransactionsListAdapter(){
super(AssetsOverview.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 (may null)
View itemView = convertView;
if (itemView == null){
itemView = getLayoutInflater().inflate(R.layout.transaction_list_view, parent, false);
}
if(getIntent().getExtras() !=null) {
Intent depositIntent = getIntent();
Transaction deposit = depositIntent.getParcelableExtra("data");
assert false;
Float newVal = deposit.getValue();
String newDes = deposit.getDescription();
Integer newDat = deposit.getTransaction_Date();
//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(newVal + "$");
// date
TextView dateView = (TextView) itemView.findViewById(R.id.transactionDateView);
dateView.setText(newDat + "");
// description
TextView descriptionView = (TextView) itemView.findViewById(R.id.transactionDesciptionView);
descriptionView.setText(newDes);
}
return itemView;
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.assets_overview, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
And here is my Activity
where I add the new items:
public class AddMoneyTransaction extends AppCompatActivity implements View.OnClickListener {
Button addDepositButton;
EditText depositInput, depositInputDate, depositInputNote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_money_transaction);
//setup the Button and EditText
addDepositButton = (Button)findViewById(R.id.addDepositButton);
depositInput = (EditText) findViewById(R.id.depositInput);
depositInputDate = (EditText) findViewById(R.id.depositInputDate);
depositInputNote = (EditText) findViewById(R.id.depositInputNote);
//get the onClickListener
addDepositButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent depositIntent = new Intent(AddMoneyTransaction.this, AssetsOverview.class);
Transaction deposit = new Transaction(100, 16, "random comment");
deposit.setValue(Float.parseFloat(depositInput.getText().toString()));
deposit.setTransaction_Date(Integer.parseInt(depositInputDate.getText().toString()));
deposit.setDescription(depositInputNote.getText().toString());
depositIntent.putExtra("data",deposit);
startActivity(depositIntent);
}
I kow its a lot of code but after hours of troubleshooting I could not figure out what the correct solution is. Oh and I did not find anything on the google...
Upvotes: 0
Views: 68
Reputation: 778
The problem is your getView()
method. What your code is doing is the following:
You create the adapter using a reference to a list with one item (new Transaction(78,98,"halo")
), which means that the getView()
method will be called only for the position 0.
Then, inside the getView()
, you get the transaction object from the intent and change the item values for the new values. There is no mention of items being added.
You need to change the following things:
AssetOverview.onCreate()
instead of inside the adapteradapter.notifyDatasetChanged()
every time the list gets updatedTransaction current = getItem(position)
AssetOverview.onDestroy()
method, to avoid a memory leak.Upvotes: 1