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

标签 java android android-studio

我正在创建一个应用程序来访问数据库中的项目,以查看某些组/团队花名册中的人员。当我搜索群组时,我会连接、创建 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/

相关文章:

javascript - 如何使用 PhoneGap Javascript 在 android 中读取文件

android-studio - 使用AndroidTestOrchestrator进行的调试在等待调试器时卡住

gradle - Android Studio Gradle 变量和任务范围访问

java - 动态设置 Maven Surefire JVM 类路径

Java:使用 getter 与缓存值

android - 水平缩放 NinePatch 图像(没有 child ,在 RelativeLayout 中)

java - Android Studio 无法识别字符串

java - 可以在其他程序上打开的文件的 ZipException

java - 正则表达式 for/something/<任何字母数字和 ->/

java - 在 Android 上下载文件时计算下载百分比和剩余时间