gpt4 book ai didi

android - Android中如何实现ASyncTask

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:16:03 27 4
gpt4 key购买 nike

谁能告诉我如何使用 ASyncTask 更新 UI 并举个例子?

我从两个网络服务中获取值(value)。从两个 web 服务中,我在两个 arraylist 的 post 方法中获取值将不会返回值,它将在循环中四舍五入。如何从 onPostExecuteMethod 返回?

这是我的异步类:

public class HomeTask extends AsyncTask<Void,Void,Bundle> 
{

private Context ctx;
public HomeTask(Context context) {
ctx = context;
}

@Override
protected void onPreExecute() {
//super.onPreExecute();
//setContentView(R.layout.splash);

dlg = new ProgressDialog(getDialogContext());
dlg.setMessage("Loading....");
dlg.show();
//setContentView(R.layout.splash);
}


@Override
protected Bundle doInBackground(Void... params) {
Bundle b=new Bundle();

// TODO Auto-generated method stub
try
{


WebService tableservice=new WebService();
tableservicevalue=tableservice.callTableServer(SOAP_ACTION,"",strUsername,strPassWord,questGroupId,URL);
tableservicevalue=decodeXMLData(tableservicevalue);

DomTableParser parser=new DomTableParser();
parser.setTableservicevalue(tableservicevalue);
parsedValue=parser.parseXmlFile(tableservicevalue);
Log.d("1234%%%%$$$$$$$parsed value$$$$$", parsedValue.toString());
WebService service=new WebService();
webservicevalue=service.callHomeServer(SOAP_ACTION,"",strUsername,strPassWord,questGroupId,URL);
webservicevalue=decodeXMLData(webservicevalue);
ArticleParser articleParser=new ArticleParser();
articleParsedValue=articleParser.parseXmlArticle(webservicevalue);

//b.putStringArrayList("articleParsedValue", articleParsedValue);

Log.d("(((((((parsed value is",parsedValue.toString());

b.putStringArrayList("parsedValue", parsedValue);
b.putStringArrayList("articleParsedValue", articleParsedValue);
Log.d("(((((((parsed value is",articleParsedValue.toString());



}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

return b;
}
@Override
protected void onPostExecute(Bundle b) {


Log.d("vijay checking","checking&&&&");
Log.d("****","*********");
Log.d("calling","handler");
Log.d("****","*********");


parsedValue1=b.getStringArrayList("parsedValue");
articleParsedValue1=b.getStringArrayList("articleParsedValue");

}

更新用户界面

new HomeTask(HomeActivity2.this).execute(null); 

for(int i=0;i<parsedValue1.size();i++)
{
DomParserTableDataSet dataSet=(DomParserTableDataSet)parsedValue1.get(i);

if(i==0)
{
Log.d("&&&&&&index name is", dataSet.getIndexName());
Log.d("&&&&&&index name is", dataSet.getLastValue());
Log.d("&&&&&&index name is",dataSet.getChangePercentage() );
txtindex0.setText(dataSet.getIndexName());
txtlast0.setText(dataSet.getLastValue());
txtchange0.setText(dataSet.getChangePercentage());
}
if(i==1)
{
Log.d("&&&&&&index name is", dataSet.getIndexName());
Log.d("&&&&&&index name is", dataSet.getLastValue());
Log.d("&&&&&&index name is",dataSet.getChangePercentage() );
txtindex1.setText(dataSet.getIndexName());
txtlast1.setText(dataSet.getLastValue());
txtchange1.setText(dataSet.getChangePercentage());
}
if(i==2)
{
Log.d("&&&&&&index name is", dataSet.getIndexName());
Log.d("&&&&&&index name is", dataSet.getLastValue());
Log.d("&&&&&&index name is",dataSet.getChangePercentage() );
txtindex2.setText(dataSet.getIndexName());
txtlast2.setText(dataSet.getLastValue());
txtchange2.setText(dataSet.getChangePercentage());
}

if(i==3)
{
Log.d("&&&&&&index name is", dataSet.getIndexName());
Log.d("&&&&&&index name is", dataSet.getLastValue());
Log.d("&&&&&&index name is",dataSet.getChangePercentage() );
txtindex3.setText(dataSet.getIndexName());
txtlast3.setText(dataSet.getLastValue());
txtchange3.setText(dataSet.getChangePercentage());
}

}


//
for(int i=0;i<articleParsedValue1.size();i++)
{

System.out.println("for loop checking i is"+i);
ArticleDataSet articleDataset=(ArticleDataSet)articleParsedValue1.get(i);

System.out.println("articleDataset.getArticle_title()"+articleDataset.getArticle_title());

HashMap<String, String> mapValue=new HashMap<String, String>();
WebView webviewcontent=new WebView(HomeActivity2.this);
if(articleDataset.getArticle_summary().length()>75)
{
summary=articleDataset.getArticle_summary().substring(0,75)+"...";
}
else
{
summary=articleDataset.getArticle_summary();
}


String html ="<html><body><div><label> <font face=\"verdana\" color=\"#C1002B\" size=\"4\"><b>"+articleDataset.getArticle_title()+"</b> </font> </label>"+ "<label> <font color=\"#000000\" size=\"2\" face=\"verdana\">"+"|"+"</font></label> "+"<label> <font color=\"#AAAAAA\" face=\"verdana\" size=\"2\">"+articleDataset.getArticle_date()+" </font></label></div>";
html=html+"<div><label> <font color=\"#000000\" face=\"verdana\" size=\"2\">"+summary+" </font></label></div></body></html>" ;
webviewcontent.getSettings().setJavaScriptEnabled(true);
webviewcontent.clearCache(true);
final Activity activity = HomeActivity2.this;


webviewcontent.loadData(html, "text/html", "utf-8");

if(i==articleParsedValue1.size()-1)
{
webviewcontent.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}

public void onPageFinished(WebView view, String url)
{

// setContentView(R.layout.home);

if (dlg.isShowing())
{

dlg.dismiss();
dlg = null;


}
splash.setVisibility(View.GONE);

// setContentView(repl);


}
});
}
mainlinear4.addView(webviewcontent);
//webviewcontent.setOnTouchListener(HomeActivity2.this);

}

}

最佳答案

List<String> data; // declare here so that we can use it in onPostExecute().
public class YourActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
starttask();
}
private void starttask() {
new FileAsync().execute("start");
Toast toast = Toast.makeText(yourActivity.this," start:" , 7);toast.show();
}

class FileAsync extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
m_ProgressDialog = ProgressDialog.show(yourActivity.this,
"", "Loading ...", true);
}

@Override
protected String doInBackground(String... aurl) {

// These methods are calling data from web service made
// in sperate class but you can do this here as well.

TempDAO tempDAO = new TempDAOImpl();
data = tempDAO.getDataList(arg1,arg2);

return null;

}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);

}

@Override
protected void onPostExecute(String unused) {
TextView textData= new TextView(YourActivity.this);
textData.setText(data.get(1));
textData.setTextColor(Color.WHITE);
textData.setGravity(Gravity.CENTER);
setContentView(textData);
m_ProgressDialog.dismiss();
}
}

关于android - Android中如何实现ASyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4792992/

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