gpt4 book ai didi

android - 从异步线程更新主线程表布局

转载 作者:搜寻专家 更新时间:2023-11-01 09:15:36 26 4
gpt4 key购买 nike

我在从异步类更新表格布局时遇到问题。我的异步类正在获取 XML 数据,因此我不会阻塞主线程。我的日志显示 XML 数据正在通过,但我不知道如何使用这些数据更新我的 View 。

因此,我尝试将数据放入表格行中并将行添加到 TableLayout,但日志显示错误提示不允许 Async 类更新我的 TableLayout View 。

我的代码如下:

public class RemotePrimary extends Activity {

private static String SERVER_PATH = "http://test2.icerge.com/";
private static String XML_FILE1 = "samplexml";

//private static String SERVER_PATH = "http://tqs.mamlambo.com/";
//private static String XML_FILE1 = "scores.jsp";

private String[] data = new String[10];

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TableLayout datatable = (TableLayout)findViewById(R.id.TableLayout_data);

Downloader downloader = new Downloader();
downloader.execute(SERVER_PATH + XML_FILE1, datatable);

}

private class Downloader extends AsyncTask<Object, String, Boolean>{

TableLayout table;

@Override
protected Boolean doInBackground(Object... params) {
// TODO Auto-generated method stub
String path = (String)params[0];
table = (TableLayout)params[1];

XmlPullParser xmldata = null;
try {
URL serverPath = new URL(path);
xmldata = XmlPullParserFactory.newInstance().newPullParser();
xmldata.setInput(serverPath.openStream(), null);
addRecord(xmldata, table);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return true;
}

@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
//super.onProgressUpdate(values);
}

private boolean addRecord(XmlPullParser data, TableLayout table){

int eventType = -1;
boolean bFoundScores = false;


//find some records from xml
while(eventType != XmlResourceParser.END_DOCUMENT){
if( eventType == XmlResourceParser.START_TAG ){
//get the name of the tag(eg scores or score)
String strName = data.getName();
if( strName.equals("node") ){
bFoundScores = true;
String scoreValue = data.getAttributeValue(null, "Title");
String scoreRank = data.getAttributeValue(null, "Type");
String scoreUserName = data.getAttributeValue(null, "Nid");
Log.e("ADDING: ", scoreValue);
//Log.e("RETRIEVED", "collected : "+scoreValue+", "+scoreRank+", "+scoreUserName);
//publishProgress(scoreValue, scoreRank, scoreUserName);

TableRow newRow = new TableRow(RemotePrimary.this);
TextView rowText = new TextView(RemotePrimary.this);
rowText.setText(scoreValue);
newRow.addView(rowText);
table.addView(newRow);
}
}
try {
eventType = data.next();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



return true;
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}

@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}

}//close Downloader class

}//关闭RemotePrimary类

我知道的有点多,但我会很感激任何帮助。

非常感谢:-)

最佳答案

您只能在 UI 线程上从 UI 进行更改。 AsyncTask 为您提供了一个通过 onPostExecute 轻松执行此操作的地方。正如它在 docs 中所说的那样, onPostExecute 始终在 UI 线程上执行。

doInBackground 中,完成构建要显示的结构化数据的所有艰苦工作。返回该数据,以便将其传递到 onPostExecute,然后在其中添加适当的表行。

关于android - 从异步线程更新主线程表布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4927685/

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