我想逐個添加ListView項目。所以,如果我說 - 60項 - 應用程序會一次添加視圖到列表視圖 - 從而向用戶顯示應用程序正在加載更多東西。使用AsyncTask單獨添加項目到列表視圖
這是我的代碼:
try {
JSONArray j = getTaggsJSON();
Log.v(TAG, String.valueOf(j.length()));
a = new createSpecialAdapter(this, R.layout.individual_tagg_view,
R.layout.list_item, view, j);
ListView v = this.getListView();
v.setStackFromBottom(true);
setListAdapter(a);
new addViewsToList().execute(j);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
}
private class addViewsToList extends AsyncTask<JSONArray, View, List<View>> {
protected List<View> doInBackground(JSONArray... jsonArrays) {
List<View> v = new ArrayList<View>();
Log.v(TAG, String.valueOf(jsonArrays[0].length()));
for (int x = 0; x < jsonArrays[0].length(); x++) {
try {
Log.v(TAG, jsonArrays[0].getJSONObject(x).toString());
v.add(ViewAdapter.createTaggView(jsonArrays[0]
.getJSONObject(x), c));
} catch (JSONException e) {
e.printStackTrace();
}
publishProgress(v.get(x));
}
return v;
}
protected void onProgressUpdate(View... v) {
Log.v(TAG, "I'm updating my progress!");
a.notifyDataSetChanged();
}
protected void onPostExecute(List<View> views) {
Log.v(TAG, "i'm done!");
Log.v(TAG, String.valueOf(views.size()));
}
}
因此,它看起來像我的代碼是否能正確打印出來的意見,並把它們在列表中正確的,但是我的活動顯示什麼。我究竟做錯了什麼?我認爲之前設置適配器 - 當列表中沒有視圖 - 然後更新適配器的列表被更改是正確的方式去....顯然不是..任何人都可以幫助我嗎?
如果您需要澄清我的問題,請讓我知道。
編輯:這裏是適配器代碼來補充我的問題。
private class SpecialAdapter extends ArrayAdapter<JSONArray> {
JSONArray json;
public SpecialAdapter(Context context, int textViewResourceId,
int listItem, JSONArray j) {
super(context, textViewResourceId);
this.json = j;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
Log.v(TAG,"I'm inside the getView method.");
try {
JSONObject info = json.getJSONObject(position);
Log.v(TAG,info.toString());
if (convertView == null) {
LayoutInflater i = (LayoutInflater) RecentTaggs.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = i.inflate(R.layout.individual_tagg_view, parent,
false);
holder = new ViewHolder();
holder.username = (TextView) convertView
.findViewById(R.id.userName);
holder.review = (TextView)convertView.findViewById(R.id.review_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.review.setText(info.getString("review"));
return convertView;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
我的輸出不會輸出我的任何Log.v語句,甚至不會在GetView方法內顯示它。
如果您可以粘貼您正在使用的適配器的代碼,那也不錯。 – Janusz 2010-07-06 06:19:50
http://pastebin.com/auu2GxXc 是完整的代碼 – hwrdprkns 2010-07-06 10:25:09