0

xml文字列を返すWebサービスを呼び出すアプリがあります。次のクラスはそのStringを処理し、必要なデータをArrayListに入れます。ArrayListのタイプはTwoDimentionalArrayListです。これらの2つのクラスを以下に投稿します。どちらも正常に動作します。

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があります。次のクラスは、このarrayListにAsyncTaskの結果を入力する必要があります。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 に答える