Android 从异步线程更新主线程tablelayout

Android 从异步线程更新主线程tablelayout,android,Android,从异步类更新表布局时出现问题。 我的异步类正在获取XML数据,因此我不会阻塞主线程。我的日志显示XML数据正在通过,但我不知道如何用数据更新视图 因此,我试图将数据放入tablerows并将这些行添加到TableLayout中,但日志显示错误,表明不允许异步类更新我的TableLayout视图 我的代码如下: public class RemotePrimary extends Activity { private static String SERVER_PATH = "http:/

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

因此,我试图将数据放入tablerows并将这些行添加到TableLayout中,但日志显示错误,表明不允许异步类更新我的TableLayout视图

我的代码如下:

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扩展活动{
专用静态字符串服务器\u路径=”http://test2.icerge.com/";
私有静态字符串XML_FILE1=“samplexml”;
//专用静态字符串服务器\u路径=”http://tqs.mamlambo.com/";
//私有静态字符串XML_FILE1=“scores.jsp”;
私有字符串[]数据=新字符串[10];
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout datatable=(TableLayout)findViewById(R.id.TableLayout\u数据);
Downloader Downloader=新的Downloader();
执行(服务器路径+XML文件1,数据表);
}
私有类下载程序扩展异步任务{
表格布局表;
@凌驾
受保护的布尔doInBackground(对象…参数){
//TODO自动生成的方法存根
字符串路径=(字符串)参数[0];
table=(TableLayout)参数[1];
XmlPullParser xmldata=null;
试一试{
URL服务器路径=新的URL(路径);
xmldata=XmlPullParserFactory.newInstance().newPullParser();
setInput(serverPath.openStream(),null);
addRecord(xmldata,table);
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回true;
}
@凌驾
受保护的void onProgressUpdate(字符串…值){
//TODO自动生成的方法存根
//super.onProgressUpdate(值);
}
私有布尔addRecord(XmlPullParser数据,TableLayout表){
int eventType=-1;
布尔bFoundScores=false;
//从xml中查找一些记录
while(eventType!=XmlResourceParser.END_文档){
if(eventType==XmlResourceParser.START_标记){
//获取标签的名称(如分数或分数)
字符串strName=data.getName();
if(strName.equals(“节点”)){
bFoundScores=真;
String scoreValue=data.getAttributeValue(null,“Title”);
字符串scoreRank=data.getAttributeValue(null,“Type”);
字符串scoreUserName=data.getAttributeValue(null,“Nid”);
Log.e(“添加:”,scoreValue);
//Log.e(“已检索”、“已收集:“+scoreValue+”、“+scoreRank+”、“+scoreUserName”);
//发布进度(scoreValue、scoreRank、scoreUserName);
TableRow newRow=新TableRow(RemotePrimary.this);
TextView rowText=新的TextView(RemotePrimary.this);
rowText.setText(scoreValue);
newRow.addView(rowText);
table.addView(newRow);
}
}
试一试{
eventType=data.next();
}catch(XMLPullParseRexE){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
返回true;
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
}
@凌驾
受保护的void onPostExecute(布尔结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
}
}//关闭下载程序类
}//关闭RemotePrimary类

我知道的有点多,但我会感谢你的帮助


非常感谢:-)

您只能在UI线程上从UI进行更改。AsyncTask通过
onPostExecute
为您提供了一个轻松的位置。如中所述,
onPostExecute
始终在UI线程上执行


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

请原谅新手遗漏了明显的:-)非常感谢。@sisko很高兴提供帮助!如果此答案或其他答案对您有所帮助,您应该单击答案旁边的复选框,将其标记为正确答案。