Android-将字符串传递给Asynctask?
与我之前关于ANR问题()的问题相关 我试着按照受访者的建议使用AsyncTask,但现在我不知所措 我需要将一个字符串从我的菜单活动传递给一个异步任务,但这真的让我很困惑。我已经搜索和学习了5个小时,但仍然做不到 以下是我的代码片段:Android-将字符串传递给Asynctask?,android,string,android-asynctask,parameter-passing,Android,String,Android Asynctask,Parameter Passing,与我之前关于ANR问题()的问题相关 我试着按照受访者的建议使用AsyncTask,但现在我不知所措 我需要将一个字符串从我的菜单活动传递给一个异步任务,但这真的让我很困惑。我已经搜索和学习了5个小时,但仍然做不到 以下是我的代码片段: @Override public boolean onCreateOptionsMenu(Menu menu) { /** Create an option menu from res/menu/items.xml */ getMenuInfla
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Create an option menu from res/menu/items.xml */
getMenuInflater().inflate(R.menu.items, menu);
/** Get the action view of the menu item whose id is search */
View v = (View) menu.findItem(R.id.search).getActionView();
/** Get the edit text from the action view */
final EditText txtSearch = ( EditText ) v.findViewById(R.id.txt_search);
/** Setting an action listener */
txtSearch.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
final EditText txtSearch = ( EditText ) v.findViewById(R.id.txt_search);
String enhancedStem = txtSearch.getText().toString();
TextView databaseOutput = (TextView)findViewById(R.id.textView8);
new AsyncTaskRunner().execute();
// should I put "enhancedStem" inside execute?
}
});
return super.onCreateOptionsMenu(menu);
}
以下是异步部分:已更新
public class AsyncTaskRunner extends AsyncTask<String, String, String> {
String curEnhancedStem;
private ProgressDialog pdia;
public AsyncTaskRunner (String enhancedStem)
{
this.curEnhancedStem = enhancedStem;
}
@Override
protected void onPreExecute() {
// Things to be done before execution of long running operation. For
// example showing ProgessDialog
super.onPreExecute();
pdia = ProgressDialog.show(secondactivity.this, "" , "Searching for words");
}
@Override
protected String doInBackground(String... params) {
if(curEnhancedStem.startsWith("a"))
{
String[] wordA = getResources().getStringArray(R.array.DictionaryA);
String delimiter = " - ";
String[] del;
TextView databaseOutput1 = (TextView)findViewById(R.id.textView8);
for (int wordActr = 0; wordActr <= wordA.length - 1; wordActr++)
{
String wordString = wordA[wordActr].toString();
del = wordString.split(delimiter);
if (curEnhancedStem.equals(del[0]))
{
databaseOutput1.setText(wordA[wordActr]);
pdia.dismiss();
break;
}
else
databaseOutput1.setText("Word not found!");
}
}
return null;
}
@Override
protected void onProgressUpdate(String... text) {
// Things to be done while execution of long running operation is in
// progress. For example updating ProgessDialog
}
@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
}
}
公共类AsyncTaskRunner扩展了AsyncTask{
线固化剂;
私家侦探;
公共AsyncTaskRunner(字符串增强项)
{
this.curEnhancedStem=增强型tem;
}
@凌驾
受保护的void onPreExecute(){
//在执行长时间运行的操作之前要做的事情
//显示ProgesDialog的示例
super.onPreExecute();
pdia=ProgressDialog.show(secondactivity.this,“,”搜索单词“);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
如果(currenhancedstem.startsWith(“a”))
{
String[]wordA=getResources().getStringArray(R.array.DictionaryA);
字符串分隔符=“-”;
字符串[]del;
TextView数据库输出1=(TextView)findViewById(R.id.textView8);
对于(int-wordActr=0;wordActr这就是问题所在,它们是您在中声明它们的方法的本地类,然后您正在声明一个没有访问它们的AsyncTask
类。如果AsyncTask
是菜单活动的内部类,那么您可以将它们声明为成员变量
public class MenuActivity extends Activity
{
String enhancedStem;
....
如果它是一个单独的类,那么您可以在Async
类中创建一个构造函数,并将变量传递给构造函数
public class AsyncTaskRunner extends AsyncTask<String, String, String> {
String curEnhancedStem;
private ProgressDialog pdia;
public void AsyncTaskRunner (String variableName)
{
this.curEnhancedStem = variableName;
}
此外,您不能在doInBackground
中执行UI
操作,因此这需要在活动
类中更改,或者在Async
类中更改其他方法,例如onPostExecute()
如果它是一个内部类。否则,您可以将一个值传递回您的菜单活动,以更新您的TextView
编辑
您仍在尝试使用此选项更改doInBackground()
中的UI
TextView databaseOutput1 = (TextView)findViewById(R.id.textView8);
然后再次调用setText()
。这需要放在onPostExecute()
中,但随后需要将TextView
的引用传递给AsyncTask
。您只需将onPostExecute()中要设置文本的String
并在您的活动中设置它。希望这对您有所帮助这就是问题所在,它们是您在其中声明它们的方法的本地对象,那么您正在声明一个没有访问它们的AsyncTask
类。如果AsyncTask
是菜单活动的内部类,那么您可以将它们声明为成员variab莱斯
public class MenuActivity extends Activity
{
String enhancedStem;
....
如果它是一个单独的类,那么您可以在Async
类中创建一个构造函数,并将变量传递给构造函数
public class AsyncTaskRunner extends AsyncTask<String, String, String> {
String curEnhancedStem;
private ProgressDialog pdia;
public void AsyncTaskRunner (String variableName)
{
this.curEnhancedStem = variableName;
}
此外,您不能在doInBackground
中执行UI
操作,因此这需要在活动
类中更改,或者在Async
类中更改其他方法,例如onPostExecute()
如果它是一个内部类。否则,您可以将一个值传递回您的菜单活动,以更新您的TextView
编辑
您仍在尝试使用此选项更改doInBackground()
中的UI
TextView databaseOutput1 = (TextView)findViewById(R.id.textView8);
然后再次调用setText()
。这需要放在onPostExecute()
中,但随后需要将TextView
的引用传递给AsyncTask
。您只需将onPostExecute()中要设置文本的String
并在您的活动中设置它。希望这有帮助
为AsyncTaskRunner
类创建构造函数
将上下文
(活动上下文)和数据库输出
文本视图
作为参数传递给AsyncTaskRunner
类构造函数
将对这两个对象的引用保存在AsyncTaskRunner
中
将enhancedStem
传递给execute()
方法
使用传递给构造函数的上下文
作为ProgessDialog.show()的第一个参数
无法从doInBackground()
方法访问databaseOutput
。只能在运行在UI线程上的onPostExecute()
中访问它。因此,请使用传递给构造函数的databaseOutput
引用更新onPostExecute()中的文本视图
方法
请注意,从doInBackground()
方法返回的任何内容都将作为onPostExecute()
方法的参数提供给您
请参阅
我建议您传递所需的数据,而不是使用封闭类访问它-这使您的ASyncTaskRunner
更加灵活,通常是更好的做法
为AsyncTaskRunner
类创建构造函数
将上下文
(活动上下文)和数据库输出
文本视图
作为参数传递给AsyncTaskRunner
类构造函数
将对这两个对象的引用保存在AsyncTaskRunner
中
将enhancedStem
传递给execute()
方法
使用传递给构造函数的上下文
作为ProgessDialog.show()的第一个参数
无法从doInBackground()
方法访问databaseOutput
。只能访问