我有一个应用程序,它调用一个返回xml String的webservice。以下类处理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。以下类应使用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
}
答案 0 :(得分:2)
我的猜测是正确的。在AsyncTask.get()
方法完成后,doInBackground()
方法开始之前,onPostExecute()
方法可能会返回结果。因此,从技术上讲,此时阵列可能仍然是空的。
此外,启动后台线程并阻止主线程非常奇怪。有什么意义?