【发布时间】:2018-04-19 20:05:06
【问题描述】:
我创建了一个全局变量 idprof,然后将其设置为一个 id(您可以在受保护的 void doinbackground 中找到它)。我想将它的值放在公共 String getCourseFromDB 方法中的 namevaluepairs 中,但它不起作用。你能帮我吗,我有时在方法之间传递变量时感到困惑(你可以在我的代码中看到我的 cmets)
public class BackgroundWorker extends AsyncTask<String,String,Void>{
Context context;
String command;
String idprof=""; // this is the global variable
BackgroundWorker (Context ctx,String command){
context = ctx;
this.command = command;
}
@Override
protected Void doInBackground(String... arg0) {
// TODO Auto-generated method stub
intent = new Intent(context, MainActivity.class);
extras.putString("id", id.trim());
idprof=id.trim(); //this is the value i want to get
intent.putExtras(extras);
context.startActivity(intent);
}
}
return null;
}
public String getCourseFromDB() {
try {
List<NameValuePair> nameValuePairs2;
nameValuePairs2 = new ArrayList<NameValuePair>(2);
nameValuePairs2.add(new BasicNameValuePair("id",idprof)); // this is where i want to pass it
}
}
【问题讨论】:
-
您如何找出能够证明您的问题的最少代码量,然后发布?见How to create a Minimal, Complete, and Verifiable example。
-
嗯,我不能删除其中的一些,因为你看到它可能很重要:(但我在那里添加了 //cmets 以强调这个问题
-
欢迎来到 Stack Overflow!请拿起tour,环顾四周,并通读help center,尤其是How do I ask a good question? 和What topics can I ask about here?。请将您的示例简化为 Minimal, Complete, and Verifiable example 并添加您收到的 complete 堆栈跟踪/错误消息。
-
我更新了它:) 删除了不必要的部分,我添加了 //cmets,cmets 强调了我的主要问题
-
好吧我再删一次