0

我有一个调用 web 服务的应用程序,它返回一个 xml 字符串。以下类处理该字符串并将所需数据放入 ArrayList。ArrayList 是 TwoDimentionalArrayList 类型。我将在下面发布这两个类。他们都工作正常。

public class RetrieveExtraDetails {

    private static final String TAG = RetrieveExtraDetails.class.getSimpleName();
    DocumentBuilderFactory builderFactory;
    DocumentBuilder builder;
    Document document;
    TwoDimentionalArrayList<String> arrayList;


    public RetrieveExtraDetails() {
        super();
        arrayList = new TwoDimentionalArrayList<String>();
        builderFactory = DocumentBuilderFactory.newInstance();
        document = null;
        try {
            builder = builderFactory.newDocumentBuilder();
            Log.e(TAG, "built the dom factory");
        } catch (ParserConfigurationException e) {
            e.printStackTrace();  
        }

    }// end of constructor


public  TwoDimentionalArrayList<String> getExtraDetails(String xmlStr){

    ArrayList<String> array = null;

        try {

            document = builder.parse( new InputSource(new StringReader(xmlStr))); 
            Log.e(TAG, "document = " + document);

        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Element rootElement = document.getDocumentElement();
        NodeList nodes = rootElement.getChildNodes();

        Node rota = nodes.item(0);
        NodeList callList = rota.getChildNodes();

        for(int i = 0; i < callList.getLength(); i++){

            Node call = callList.item(i);
            NodeList callChildrenList = call.getChildNodes();
            array = new ArrayList<String>();

        for(int j = 0; j < callChildrenList.getLength(); j++){

                Node callChild = callChildrenList.item(j);

                if(callChild.getNodeName().equalsIgnoreCase("RecTypeID")){
                      String nodeData =  callChild.getTextContent();
                      Log.e(TAG, "RecTypeID = " + nodeData);
                      array.add(nodeData);

                }else if (callChild.getNodeName().equalsIgnoreCase("RecType")){

                    String nodeData =  callChild.getTextContent();
                      Log.e(TAG, "RecType = " + nodeData);
                      array.add(nodeData);
                }else if (callChild.getNodeName().equalsIgnoreCase("Name")){

                    String nodeData =  callChild.getTextContent();
                      Log.e(TAG, "Name = " + nodeData);
                      array.add(nodeData);
                }else if (callChild.getNodeName().equalsIgnoreCase("Relationship")){

                    String nodeData =  callChild.getTextContent();
                      Log.e(TAG, "Relationship = " + nodeData);
                      array.add(nodeData);
                }else if (callChild.getNodeName().equalsIgnoreCase("Address")){

                    String nodeData =  callChild.getTextContent();
                      Log.e(TAG, "Address = " + nodeData);
                      array.add(nodeData);
                }else if (callChild.getNodeName().equalsIgnoreCase("Postcode")){

                    String nodeData =  callChild.getTextContent();
                      Log.e(TAG, "Postcode = " + nodeData);
                      array.add(nodeData);
                }else if (callChild.getNodeName().equalsIgnoreCase("TelNo")){

                    String nodeData =  callChild.getTextContent();
                      Log.e(TAG, "TelNo = " + nodeData);
                      array.add(nodeData);
                }else if (callChild.getNodeName().equalsIgnoreCase("Keysafe")){

                    String nodeData =  callChild.getTextContent();
                      Log.e(TAG, "Keysafe = " + nodeData);
                      array.add(nodeData);
                }else if (callChild.getNodeName().equalsIgnoreCase("Notes")){

                    String nodeData =  callChild.getTextContent();
                      Log.e(TAG, "Notes = " + nodeData);
                      array.add(nodeData);
                }else if (callChild.getNodeName().equalsIgnoreCase("Meds")){

                    String nodeData =  callChild.getTextContent();
                      Log.e(TAG, "Meds = " + nodeData);
                      array.add(nodeData);
                }



            }// end of j loop
        arrayList.add(i, array);
        }//end of i loop

        return arrayList;

    }//end of getExtraDetails



}//end of class

.

public class TwoDimentionalArrayList<T> extends ArrayList<ArrayList<T>> {

    private static final long serialVersionUID = 1L;

    public void addToInnerArray(int index, T element) {
        while (index >= this.size()) {
            this.add(new ArrayList<T>());
        }
        this.get(index).add(element);
    }

    public void addToInnerArray(int index, int index2, T element) {
        while (index >= this.size()) {
            this.add(new ArrayList<T>());
        }

        ArrayList<T> inner = this.get(index);
        while (index2 >= inner.size()) {
            inner.add(null);
        }

        inner.set(index2, element);
    }
}

现在在下面的类中,我有一个设置在类范围内的 ArrayList。以下类应使用 AsyncTask 的结果填充此 arrayList。在 onPostExecute 中,数组的大小为 4,但是当我注销类范围数组的值时,它在由 AsyncTask 设置后为 0。我什至在 Async 上调用 .get() 以确保活动在恢复执行之前等待 Async 的结果。

任何想法为什么这个 ArrayList 没有被填充?我已经问过一个类似的问题,但在这个问题中,我已将代码缩减,以便更容易理解问题。谢谢。

ArrayList<ArrayList<String>> array;

.

public class GetRotaDetails extends NfcBaseActivity{



    ArrayList<ArrayList<String>> array;

    private static final String TAG = GetRotaDetails.class.getSimpleName();
    NfcScannerApplication nfcscannerapplication;
    String callID;
    ListView listView;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         //set titlebar to carer's name
        nfcscannerapplication = (NfcScannerApplication) getApplication();
        intent = this.getIntent();
        Cursor cursorCarerName = nfcscannerapplication.loginValidate.queryAllFromCarer();
        cursorCarerName.moveToLast();
        String carerTitleName = cursorCarerName.getString(cursorCarerName
                .getColumnIndex(LoginValidate.C_CARER_NAME));
        setTitle(carerTitleName + " is currently logged in");


        array = new  ArrayList<ArrayList<String>>();

        listView = (ListView) findViewById(R.id.getrotadetailslistview);
        setContentView(R.layout.getrotadetailslayout);


        callID = intent.getStringExtra("callIDExtra");
        String[] params = { callID };

        AsyncGetRotaDetails agrd = new AsyncGetRotaDetails();
        try {
            agrd.execute(params).get();
        }  catch (Exception e) {
            e.printStackTrace();
        }


        Log.e(TAG, "array size = " + array.size());



    }// end of onCreate



    private class AsyncGetRotaDetails extends AsyncTask<String, Void, String> {

        ProgressDialog progressDialog;
        String rotaDetails = null;

        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog
                    .show(GetRotaDetails.this, "Connecting to Server",
                            " retrieving rota details...", true);

        };

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

            try {
                Log.e(TAG, "inside doInBackground");

                rotaDetails = nfcscannerapplication.loginWebservice.getRotaDetail(params[0]);

            } catch (Exception e) {

                e.printStackTrace();

            }
            return rotaDetails;

        }

        @Override
        protected void onPostExecute(String xmlResult) {
            super.onPostExecute(xmlResult);
            if (progressDialog != null)
                progressDialog.dismiss();

                RetrieveExtraDetails red = new RetrieveExtraDetails();
                array = red.getExtraDetails(xmlResult);
                Log.e(TAG, "array from WS = " + array.size());


        }// end of postExecute

    }//end of Async


}
4

1 回答 1

2

我的猜测是对的。AsyncTask.get()方法可能会在方法完成后返回结果doInBackground(),但在onPostExecute()方法开始之前。因此,从技术上讲,此时您的数组可能仍然是空的。

此外,启动后台线程并阻止主线程是非常奇怪的。重点是什么?

于 2012-10-09T16:50:17.633 回答