0

I am trying to get to the point where I have a Input (EditText) box in my Activity where users can input ther names. I need it so each time a name is inputed and the button "Add" is clicked it will save that name into a Array So that I can display them on the screen next to the Input (EditText) but also the user needs the ability to remove the last name they just added to the Array with a button that will appear next to the name once it has been added (so kind of needs to save them maybe?).

Also I need to be able to carry over the Array to the next Activity would I have to make it a global Array?

This is what I have so Far:

public class AddRemove extends Activity {

ArrayList<String> playerList = new ArrayList<String>();
String playerlist[];

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addremove);

    Button confirm = (Button) findViewById(R.id.confirm);
    confirm.setOnClickListener(new OnClickListener() {   
        public void onClick(View v) {
            final EditText playername = (EditText) findViewById(R.id.userinput);
            String name = playername.getText().toString();
            playerList.add(name); 


    }});

}
}

And the XML:

<EditText
    android:id="@+id/userinput"
    android:layout_width="190dp"
    android:layout_height="40dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="22dp"
    android:layout_marginTop="95dp"
    android:hint="Enter Name"
     >
    <requestFocus />
</EditText>

<Button
    android:id="@+id/confirm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/userinput"
    android:layout_centerVertical="true"
    android:text="Add" />

1 Answer 1

2

This is a very dense question, so I'll try to summarize what you need to do and you can tell me if you need help doing any one thing.

You would need to programmatically add views within the OnClick. In your case, a TextView and a Button. With the Button you just created, you would need to remove the TextView string from the playerList on its OnClickListener. You would also remove the View from it's parent so the user no longer sees it.

In order to pass an array over to the next Activity, you can use Bundle

Intent i = new Intent( this, NewActivity.class );
Bundle extras = new Bundle();
extras.putSerializable( "com.example.playerList", playerList );
i.putExtras( extras ); 
startActivity( i );

And to read the ArrayList from your new Activity

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle extras = getIntent().getExtras();
    ArrayList<String> playerLIst = extras.getSerializable( "com.example.playerList" );
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ok cool thats makes sense, first of though dont I need to get the user inputed info to save into an Array and then clear the Input box once the add button is clicked? How's my code looking so far for that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.