我是Android Studio中的新手,我遇到了问题。
正如您在屏幕截图中看到的,当用户写入名称并单击“添加”按钮时,会出现带有此名称和按钮的新布局以将其删除。 Screen to add players
现在,当我进入下一个屏幕点击“进一步”时,所有名称都可见。当我想要删除某些名称时,会出现问题,点击“删除”按钮,例如我想删除大卫。大卫不应该在下一个活动中看到。不幸的是他是。 Next activity
这是我的代码:
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);
}
}
要删除的布局
<?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"
/>
你知道我怎么纠正它吗? :)
答案 0 :(得分:0)
好吧,您要为names_blueTeam
和names_redTeam
添加名称,并显示这些列表的内容。
问题是当您执行删除操作时,不会从中删除相应的名称,因此它们仍会显示在下一个Activity
。
这样的事情应该有效:
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);
}
}
(你还应该看一下Java Naming Conventions)