gpt4 book ai didi

java - 如何在 AsyncTask 中运行 ProgressDialog

转载 作者:行者123 更新时间:2023-11-30 01:49:03 24 4
gpt4 key购买 nike

我在我的 Activity 的 AsyncTask 中初始化 ProgressDialog 时遇到问题,java 文件 Activity 的名称是 BigBoard.java

下面是 BigBoard.java 中 AsyncTask 的 java 类代码:

    class syncX extends AsyncTask<String, String, String>
{
ProgressDialog progress;

@Override
protected void onPreExecute()
{
super.onPreExecute();
progress = new ProgressDialog(context); //ERROR
progress.setMessage("Setting BigBoard ");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.show();
}

@Override
protected String doInBackground(String... params)
{
return null;

}

@Override
protected void onPostExecute(String file_url)
{

}

}

现在,这就是我感到困扰的地方,progress = new ProgressDialog(context);。我尝试将其更改为:

progress = new ProgressDialog(this);
progress = new ProgressDialog(BigBoard);
progress = new ProgressDialog(BigBoard.this);

但是,无奈。如何解决?

编辑1

BigBoard 类根据要求在下面提供。

public class BigBoard extends ActionBarActivity {

ArrayList<String> countryLocal;
String temp;
ArrayAdapter<String> namesArrayAdapter;
ContactInfo ci;
List<ContactInfo> result;
ProgressDialog progress;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Parse.initialize(this, "app-id", "client-key");
setContentView(R.layout.activity_big_board);
ci = new ContactInfo();


RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
String abc="hello";

syncX runner = new syncX();
runner.execute();




result = new ArrayList<ContactInfo>();
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Credentials");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> credentialList, ParseException e) {
if (e == null) {
for(int i=0;i<credentialList.size();i++)
{
ci.name = credentialList.get(i).getString("Name");
ci.surname = credentialList.get(i).getString("SurName");
ci.email = credentialList.get(i).getString("email");
result.add(ci);


Log.d("OUT", "So the Val::------> " + credentialList.get(i).getString("email"));
//result.notifyDataSetChanged();

}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});

ContactAdapter ca = new ContactAdapter(result);
recList.setAdapter(ca);


}

最佳答案

进度对话框需要传递 Activity 的上下文。正如你所想的那样。

progress = new ProgressDialog(context);

现在的问题是如何将上下文传递给进度对话框。只是这个类的调用。 class syncX extends AsyncTask<String, String, String>{}应该将上下文传递给进度对话框。请参阅下面的修改版本类。

class syncX extends AsyncTask<String, String, String>
{
ProgressDialog progress;
Context mContext;

public syncX(Context context){
this.mContext= context;
}

public Context getContext(){
return mContext;
}


@Override
protected void onPreExecute()
{
super.onPreExecute();
progress = new ProgressDialog(getContext());
progress.setMessage("Setting BigBoard ");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.show();
}

@Override
protected String doInBackground(String... params)
{
return null;
}

@Override
protected void onPostExecute(String file_url)
{

}
}

在您的 BigBoard 类中,它应该是这样的。

new syncX(this).execute();

干杯!

关于java - 如何在 AsyncTask 中运行 ProgressDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33325957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com