Java 变量没有值
在使用方法从其他活动中获取变量后,我在使用它进行进一步编码时遇到了问题 我得到的变量如下:Java 变量没有值,java,android,variables,Java,Android,Variables,在使用方法从其他活动中获取变量后,我在使用它进行进一步编码时遇到了问题 我得到的变量如下: public void setBank(String bank){ selectedBank=bank; System.out.println(bank); } println检查我得到的内容并将其“保存”到selectedBank变量中,我在类的开头声明了selectedBank变量,如下所示public String selectedBank=“” 完整代码:
public void setBank(String bank){
selectedBank=bank;
System.out.println(bank);
}
println检查我得到的内容并将其“保存”到selectedBank变量中,我在类的开头声明了selectedBank变量,如下所示public String selectedBank=“”代码>
完整代码:
public class MyDatabase extends SQLiteAssetHelper {
private static final String DB_NAME = "bankomati_db.sqlite";
private static final int DB_VERSION = 1;
public String selectedBank="";
public MyDatabase(Context context){
super(context, DB_NAME, null, DB_VERSION);
}
public void setBank(String bank){
selectedBank=bank;
System.out.println(bank);
}
//other methods
}
这不起作用,当我在其他方法中选中selectedBank时,它是juts“”
如何从方法setBank“保存”变量,以便在其他方法中使用它
编辑:
这是我需要变量(selectedBank)的方法
public List getBankomatLocation(){
SQLiteDatabase db=getReadableDatabase();
//字符串[]sqlSelect={“0_id”、“Banka”、“Naslov”、“Posta”、“Kraj”};
字符串[]sqlSelect={“0\u id”,“Naslov”};
字符串sqlTables=“Bankomati_db”;
System.out.println(“selectedBank:+selectedBank”);
字符串[]args={selectedBank};
List bankomati=new ArrayList();
游标c=db.query(false,sqlTables,sqlSelect,“Banka=?”,args,null,null,null,null,null);
//游标c=db.query(false,sqlTables,sqlSelect,null,null,null,null,null,null);
if(c.moveToFirst()){
做{
bankomati.add(c.getString(1));
}而(c.moveToNext());
}
c、 close();
db.close();
返回班科马蒂;
}
调用getReadableDatabase方法时,它是否也可以将变量设置为“”?您必须确保使用this
。关键字以确保您更改的值在类中保持不变。Later make可在稍后调用setBank()
方法后请求该值。我在没有实现类的情况下自由地尝试了您的代码,最终它成功了
在我尝试的这段代码中,我添加了一个构造函数方法publicmydatabase(stringselectedbank)
,以便在主函数中测试它。下面是我尝试的代码
public class MyDatabase {
private static final String DB_NAME = "bankomati_db.sqlite";
private static final int DB_VERSION = 1;
public String selectedBank = "";
public MyDatabase(String selectedBank){
//this.selectedBank = selectedBank;
}
public void setBank(String bank){
this.selectedBank = bank;
System.out.println(bank);
}
//other methods
//This is to test the values of the variable 'selectedBank'
public static void main(String[] args){
//Create new object MyDatabase to test the value of the "selectedBank"
MyDatabase d = new MyDatabase("");
//print the name of the bank in the method
d.setBank("My Bank");
//I can access the name of the bank after calling the method
System.out.println(d.selectedBank);
}
}
确保您使用的是MyDatabase类型的具体对象,在检查其他方法中的selectedBank值之前,您应该调用setbank方法。请将代码张贴在实际使用此类的位置,好吗?你所拥有的看起来应该可以工作,所以我猜你是在错误地使用它(或者存在一些冲突)。你必须确保(a)你在看同一个实例,(b)它没有被Android自己根据应用程序生命周期事件等重新创建。我编辑了这个问题。你的getBankomatLocation()是吗MyDatabase
类中的方法?不要担心构造函数。我将其用于测试代码并查看结果。最重要的是更改此选项。selectedBank=bank您必须确保使用“this.”关键字来确保您更改的值在类中保持更改。
nonsense您不必使用this
关键字来访问类变量,您只需要在希望将类变量设置为同名的局部变量时执行此操作。(在我看来)最好总是使用这个
public class MyDatabase {
private static final String DB_NAME = "bankomati_db.sqlite";
private static final int DB_VERSION = 1;
public String selectedBank = "";
public MyDatabase(String selectedBank){
//this.selectedBank = selectedBank;
}
public void setBank(String bank){
this.selectedBank = bank;
System.out.println(bank);
}
//other methods
//This is to test the values of the variable 'selectedBank'
public static void main(String[] args){
//Create new object MyDatabase to test the value of the "selectedBank"
MyDatabase d = new MyDatabase("");
//print the name of the bank in the method
d.setBank("My Bank");
//I can access the name of the bank after calling the method
System.out.println(d.selectedBank);
}
}