嘗試使用以下:機器人:JSONArray&的JSONObject到的ListView奇怪讀數
爲了使該裝置使用含有JSON對象一個JsonArray列表視圖。出於某種原因, '公共視圖getView(int位置,查看convertView,ViewGroup父母)' 代碼被觸發的次數多於jsonarray中的內容。
我做了一個控制測試來檢查這一點,我發現,即使在jsonarray中只有1個Jsonobject,我想出了32次getView代碼被激活。
我很困惑,爲什麼會發生這種情況,因爲我的朋友已經設法爲我的類似代碼,但沒有我所遭受的大量激活。我是不是很慢,這是因爲單個Jsonobject不僅有其中的圖像和文本,還有其他約15個項目?還是另一個原因?
我希望朝這個任何援助,我張貼適配器下面的代碼:
public class ArticleAdapter extends BaseAdapter{
private JSONArray items;
private Context cont;
public ArticleAdapter(Context context, JSONArray array)
{
super();
this.items = array;
this.cont = context;
}
@Override
public int getCount() {
return items.length();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
WebIView sath;
TextView sati;
Log.i("Seiji", "Checking! " + position);
try
{
if(!items.isNull(position))
{
JSONObject item = items.getJSONObject(position);
if (v == null) {
v = LayoutInflater.from(cont).inflate(R.layout.saved_articles_listitem, null);
}
sath = (WebIView) v.findViewById(R.id.sathumbnail);
sati = (TextView) v.findViewById(R.id.satitle);
if(item.has("image") && sath != null)
{
JSONObject thisImage = item.getJSONObject("image");
sath.reset();
sath.setImageUrl(thisImage.getString("thumbnail"));
sath.loadImage();
}
if(sati != null)
{
sati.setText(item.getString("title"));
}
}else{
return null;
}
}
catch(Exception e)
{
Log.e("num", "Saved Art Error! " + e.toString());
}
return v;
}
}
激活這個類的代碼如下:
ListView savedArtList = (ListView) sav.findViewById(R.id.savelist);
ArticleAdapter savedadapter = new ArticleAdapter(cont, flip);
ArtList.setAdapter(savedadapter);
編輯:
感謝一些非常有幫助的建議,我能夠弄清楚出了什麼問題。每次添加新行時,Listview都會自行調整大小,因爲我已將視圖高度設置爲'wrap_content'。我沒有意識到這會造成問題,但是一旦我將它設置爲'fill_parent'(或者在其他情況下爲設置值),問題就消失了,我再也沒有這個問題了。 再次感謝您的有益建議!
難道你不能簡單地寫出你的問題嗎? –
我剛編輯它,抱歉,如果它不清楚。爲了清楚起見,我認爲我應該解釋爲什麼我會問及問題。我的appoligies。 – SKato