gpt4 book ai didi

java - 背压时 Intent 未清除, "on create"中的项目不断堆叠。安卓

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:28 25 4
gpt4 key购买 nike

我正在创建一个应用程序来访问数据库中的项目,以查看某些组/团队花名册中的人员。当我搜索群组时,我会连接、创建 Intent 并传入搜索信息,然后创建与群组成员相对应的按钮。在“后按”时,我尝试清除 Intent ,删除按钮和 View ,基本上是为了破坏 Activity 。但是,它不起作用。当我单击后退按钮,然后再次调用该方法时,第一次创建 Activity 时创建的按钮仍然存在,并且新按钮堆叠在旧按钮下方。

创建 Intent :

public void run() {
//connect to db
try {
System.out.println("Connecting to a selected database...");
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection(url, "Schauls276_stacy", "123456");
System.out.println("Made connection");
Statement st = c.createStatement();
//create query
System.out.println("Executing Query...");
switch (info){
case "Electrical":
sql = "SELECT * FROM `roster` WHERE roster.team LIKE \"E%\"" ;

break;
case "Programming":
sql = "SELECT * FROM `roster` WHERE roster.team LIKE \"P%\"" ;
break;
case "Drive":
sql = "SELECT * FROM `roster` WHERE roster.team LIKE \"D%\"" ;
break;
case "Marketing":
sql = "SELECT * FROM `roster` WHERE roster.team LIKE \"M%\"" ;
break;
}
//execute query
PreparedStatement statement = c.prepareStatement(sql);
ResultSet rs = statement.executeQuery();
if(rs == null){
//create a pop up window to say there was no one found
System.out.println("rs is NULL");
}else{
while(rs.next()){
groupString = groupString + (rs.getString("name") + "\n");
System.out.println("Found person: " + rs.getString("name"));
}
}
c.close();
System.out.println("Connection Terminated.");

Bundle bun = new Bundle();
bun.putString("KEY", groupString);
groupIntent.putExtras(bun);
startActivity(groupIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}).start();

然后在 Intent 中:

private String text;
private String[] splitted;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group_members);

//create the scroll view
ScrollView sv = (ScrollView) this.findViewById(R.id.sv);
//create the linear layout
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

//get the bundled items
Bundle items = getIntent().getExtras();

// get the text and split it
if(items != null){
text = items.getString("KEY");
}
splitted = text.split("\n");
//create the new buttons, add them to the ll, then add the ll to the sv
for(int i = 0; i < splitted.length; i++){
mButton = new Button(this);
mButton.setId(i);
mButton.setText(splitted[i]);
mButton.setOnClickListener(this);
ll.addView(mButton);

}
sv.addView(ll);
}
@Override
public void onClick(View v) {
System.out.println("Button pressed! Button: " + splitted[v.getId()]);
}


@Override
public void onBackPressed(){
System.out.println("Back Pressed");
getIntent().removeExtra("Key");
finish();
}

第一次创建后:

After the first creation

第二次创建后:

After the second creation

最佳答案

看来您的 groupString 变量是全局变量,并且您不断将您的 names 附加到同一变量。在 while(rs.next()) 循环中使用 groupString 变量之前,先清除该变量。

关于java - 背压时 Intent 未清除, "on create"中的项目不断堆叠。安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44424855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com