Tomas Tomas - 1 month ago 13
Android Question

Delete name from List while clicking a Button

I'm new in Android Studio and I have a problem.

As you can see on a screenshot, while user writes a name and click 'Add' button, new layout appears with this name and button to delete it.
Screen to add players.

Now, when I go to the next screen clicking 'Go further', all names are visible. The problem appears when I want to remove some name clicking 'Delete' button, e.g. I want to remove David. David shouldn't be visible on the next activity. Unfortunately he is.
Next activity.

And this is my code:

TeamNamesActivity

public class TeamNamesActivity extends Activity {

Button button_more_players;
EditText namesBlueTeam;
Button addPlayersBlueTeam;
LinearLayout containerBlueTeam;
EditText namesRedTeam;
Button addPlayersRedTeam;
LinearLayout containerRedTeam;
int shortestNameLength = 3;
ArrayList<String> names_blueTeam = new ArrayList<>();
ArrayList<String> names_redTeam = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_team_names);
namesBlueTeam = (EditText)findViewById(R.id.first_player_blue);
namesRedTeam = (EditText)findViewById(R.id.first_player_red);
addPlayersBlueTeam = (Button)findViewById(R.id.more_players_blue_team);
addPlayersRedTeam = (Button)findViewById(R.id.more_players_red_team);
containerBlueTeam = (LinearLayout)findViewById(R.id.container_blue);
containerRedTeam = (LinearLayout)findViewById(R.id.container_red);
}

public void addMorePlayers_blueTeam(View view) {
button_more_players = (Button) findViewById(R.id.more_players_blue_team);
addPlayers(namesBlueTeam, containerBlueTeam, names_blueTeam);
}

public void addMorePlayers_redTeam(View view) {
button_more_players = (Button) findViewById(R.id.more_players_red_team);
addPlayers(namesRedTeam, containerRedTeam, names_redTeam);
}

public void addPlayers(EditText nameOfPlayer, LinearLayout container, List<String> playersNames) {
final View addView;
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(nameOfPlayer.getText().length() >= shortestNameLength) {

addView = layoutInflater.inflate(R.layout.activity_to_add_more_players, null);
TextView textOut = (TextView)addView.findViewById(R.id.textout);
String currentName = nameOfPlayer.getText().toString();
textOut.setText(currentName);
playersNames.add(currentName);

nameOfPlayer.setText("");
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}
});
container.addView(addView);

}
}

public void goFurther(View view) {

Intent teamNamesIntent = new Intent(this, StartGameActivity.class);
teamNamesIntent.putExtra("names_redTeam", names_redTeam);
teamNamesIntent.putExtra("names_blueTeam", names_blueTeam);
startActivity(teamNamesIntent);

}

public void goFurtherWithoutNames(View view) {
}

public List<String> getNames_redTeam() {
return names_redTeam;
}

public List<String> getNames_blueTeam() {
return names_blueTeam;
}
}


StartGameActivity

public class StartGameActivity extends AppCompatActivity {

ArrayList<String> names_redTeam;
ArrayList<String> names_blueTeam;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_game);
TextView textOut = (TextView)findViewById(R.id.textout2);
names_redTeam = (ArrayList<String>) getIntent().getSerializableExtra("names_redTeam");
names_blueTeam = (ArrayList<String>) getIntent().getSerializableExtra("names_blueTeam");
textOut.setText("Red: " + names_redTeam + " ;;;;;; " + ", Blue: " + names_blueTeam);
}
}


Layout to delete

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button android:id="@+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/remove_player"
/>

<TextView android:id="@+id/textout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/remove"
/>




Do you know how I can correct it? :)

Answer

Well, you're adding names to the names_blueTeam and names_redTeam lists, and showing the content of these lists in the next Activity.

The problem is when you perform a delete, you don't remove the corresponding name from these lists, so they still appear in the next Activity.

Something like this should work:

public void addPlayers(EditText nameOfPlayer, LinearLayout container,
    final List<String> playersNames) {
    final View addView;
    LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (nameOfPlayer.getText().length() >= shortestNameLength) {
        addView = layoutInflater.inflate(R.layout.activity_to_add_more_players,
            null);
        TextView textOut = (TextView) addView.findViewById(R.id.textout);
        final String currentName = nameOfPlayer.getText().toString();
        textOut.setText(currentName);
        playersNames.add(currentName);

        nameOfPlayer.setText("");
        Button buttonRemove = (Button) addView.findViewById(R.id.remove);
        buttonRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // removing the name from the List
                playersNames.remove(currentName);

                ((LinearLayout) addView.getParent()).removeView(addView);
            }
        });
        container.addView(addView);
    }
}

(you should also take a look at the Java Naming Conventions)