0

Can someone please help me with the following scenario as I am new to android development.

I want to put a button that would add two text fields for user to insert in input. Every time button is pressed, it would add two new text fields and move the data from the older one to on screen view.

Inputs would be name with income as a pair. And I would want to use a name's income to do changes afterwards to his income. I am not sure what would be the best way to achieve this.

charsi
  • 399
  • 1
  • 5
  • 13
  • Please understand: SO is not a "me vision, but no idea, please fill the gap" service. The idea is that you **try** something, start coding ... and come here with specific questions when this or that in your **code** doesn't work. – GhostCat Nov 04 '16 at 10:23
  • @GhostCat I do understand that, that is why I never asked for someone to write me a code. my last sentence mentioned that I want to know the best way to achieve this. I am sure there would be plenty of approaches, as I am new to android development, I wanted to know what would be the best way to do that as per the people who are experts in android dev. Hope you understand! – charsi Nov 04 '16 at 12:58
  • @GhostCat Secondly, I have seen alot of people here asking for suggestions from the experts on how to do a certain thing. That was another reason I asked a question like this. – charsi Nov 04 '16 at 13:26
  • And you see, I didn't even downvote. I just gave you feedback. Such questions can work out, but they can also attract close requests and downvotes quickly. – GhostCat Nov 04 '16 at 13:31

1 Answers1

1

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);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • This helped alot! – charsi Nov 10 '16 at 19:04
  • Would you be able to help me with the following question? http://stackoverflow.com/questions/40516631/getting-error-while-removing-specific-entry-from-listview-and-hashmap – charsi Nov 11 '16 at 20:33