我有一個簡單的LinearLayout
與一個ID content
。AsyncTask不填寫佈局
我有其他佈局,親戚(id = entry
)。取決於php文件的響應,這個親戚會被誇大幾次。
問題是,如果我手動充氣entry
佈局4次,它工作正常。這是使用的代碼:
LinearLayout content = (LinearLayout) Events.this.findViewById(R.id.content);
for (int i=0; i<4;i++){
RelativeLayout event = (RelativeLayout) mInflater.inflate(R.layout.event_entry, null);
content.addView(event);
}
但現在,問題是,我不能在AsyncTask上做到這一點。
AsyncTask本身工作正常。
PreExecute()
被設置爲無效。OnBackground()
向我的計算機上託管的php文件發出http請求,並且此php正在返回一個json,其中包含一個名爲Event
的自定義類中的多個對象。我將這些對象保存到HashMap中。散列表填寫正確(調試:{[email protected], [email protected]}
)。如您所見,Event的兩個對象位於HashMap上。OnPostExecute()
包含此代碼:protected void onPostExecute() { Log.d("debugging","Omplo llistat amb"+Events.this.events.toString()); LinearLayout content = (LinearLayout) Events.this.findViewById(R.id.content); for (int i=0; i<Events.this.events.size()+1;i++){ RelativeLayout event = (RelativeLayout) mInflater.inflate(R.layout.event_entry, null); Event eventDesat = Events.this.events.get(""+i); TextView title = (TextView) event.findViewById(R.id.title); title.setText(eventDesat.getTitle()); content.addView(event); } }
但它不填充任何內容。
試過像event.getParent().invalidate();
這樣的東西,但結果相同。
你明白髮生了什麼事嗎? 我真的沒有看到第一個示例代碼和我下面的真實代碼之間的任何區別。請注意,該循環正在循環兩次,因爲它應該如此。
似乎onPostExecute永遠不會觸發:
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
SharedPreferences settings = getSharedPreferences("login", MODE_PRIVATE);
int loggedin = settings.getInt("logged", 0);
if (loggedin == 0){
Events.this.finish();
}
String username = settings.getString("username", "");
String password = settings.getString("password", "");
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(Events.this.urlWebRetrieveEvents, "POST", params);
// Check your log cat for JSON reponse
if (json!= null){
//Log.d("Debugging", json.toString());
try {
int success = json.getInt("success");
if (success == 1){
//this is filling hashmap correctly
Log.d("debugging","AQUI VAAA"+Events.this.events.toString());
}
} catch (Exception e){
//algo malo. no internet? perdudes les dades? Crec que mai hauriem d'entrar aquí.
}
}
return null;
}
首先排除簡單的問題......循環得到執行嗎?是'Events.this.events.size()'> 0? – loeschg 2013-04-29 21:49:56
爲什麼你在條件中加1?通常size方法從1開始返回結果0表示爲空。 – Gustek 2013-04-29 21:51:47
@Gustek只是一些調試嘗試,反正不應該有任何影響。 – Reinherd 2013-04-29 21:53:59