You want to use a ListAdapter
bound to a ListView
Your Activity layout will have 2 EditText
's, a Button
, and a ListView
laid out however you want.
onClick
of that button will build a new User(name, income)
from those EditText
values, then add
to some adapter
or ArrayList<User>
that is loaded into the adapter
.
public void onClick(View v) {
String name = nameInput.getText().toString();
Double income = Double.parseDouble(incomeInput.getText().toString());
adapter.add(new User(name, income)); // for an ArrayAdapter<User>
}
This User
class is to be loaded into an ArrayAdapter<User>
and looks like so
public class User {
public String name;
public double income;
public User(String name, double income) {
this.name = name;
this.income = income;
}
@Override
public String toString() {
return String.format("%s : %.2f", this.name, this.income);
}
}
The Adapter
can be declared as this, which will toString()
anything you add to it, and display it as a string on one line.
adapter = new ArrayAdapter<User>(
MainActivity.this,
android.R.layout.simple_list_item_1);