我怎样才能得到以前的价值java?

How can I get previous value java?

我的 layout 上有两个 Spinner dropDown 当用户 select 每个微调器我将从 spinner 中获取一个 id,并且我有一个 class 我将这些 ID 发送给它 class .

getID.class :

public class getID {
private String tagID = "105358";

    public getID tagID(String tagID) {

        this.tagID += "," + tagID;

        return this;
    }
public URL build() throws MalformedURLException {
    return new URL(
            String.format("%s",
                          tagID));
     }
}

问题:

但是当我 select 第二个微调器上的一个项目时,我丢失了第一个微调器的第一个值。

我使用以下代码将我的值发送到 class:

URL url = new getID(Const.URLMedia)
                                .tagID("10")
                                .build(); 

例如,当我 select 在其他 class 中第一个微调器上的一个项目(为了考试我发送 10 个值)时,我看到:

105358,10

当我在其他 class 中 select 第二个微调器上的项目(为了考试我发送 85 值)时,我看到 :

105358,85

但我需要:

105358,10,85

好像你每次都在创建 getID 的新实例:

URL url = new getID(Const.URLMedia)
                                .tagID("10")
                                .build(); 

所以当你 select 第一个微调器时你得到 105358,10 并且当你 select 第二个时,你的代码将再次创建 getID 的新实例并且你得到 105358,5 所以只需创建一个 getID 实例,而不是每次都创建一个新实例。

class Activity ..{
getID  url;

         @Override
          oncreate (Bundle saveinstance){
          url=new getID();
        }
  }

现在简单附加值

URL url = obj.tagID(StringValue).build(); 

另外我看不到任何 constructor 这个 getID(Const.URLMedia),好像不见了。

Best Practices for some unexpected cases to avoid broken URL(if sequence matter):

如果用户 select 第二个微调器而不是第一个 :您可以在第一个微调器的 onclick 内创建新的 getID 对象并设置第二个微调器的默认值。

一种方法是跟踪两个滑块(我添加了一个构造函数):

public class getID {
private String tagID;
private String init;
private String firstSlider;
private String secondSlider;

public getID setFirstSlider(String value) {
    firstSlider = value;
    return this;
}
public getID setSecondSlider(String value) {
    secondSlider = value;
    return this;
}

public getID(String init) {
    this.init = init;
    tagID = "" ;
    firstSlider = "";
    secondSlider = "";      
}

    public getID tagID() {

        this.tagID = init + "," + firstSlider + "," + secondSlider;

        return this;
    }
public URL build() throws MalformedURLException {
    return new URL(
            String.format("%s",
                          tagID));
     }
}

然后你可以像这样使用class:

        try {
        getID myID = new getID("105358") ;
        URL url = myID.setFirstSlider("10").setSecondSlider("20").tagID().build();
        System.out.println("url: " + url);
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }

还有其他方法可以做到这一点,例如删除 tagID() 函数和 tagID 字符串并直接调用 build(),因为所有信息都可用:

public URL build() throws MalformedURLException {
    return new URL(
            String.format("%s,%s,%s",
                          init, firstSlider, secondSlider));
     }
}