我有一個ListView
與自定義`ArrayAdapter我有時有時更新值有時。 一切工作正常,但在第一次更新它變成空的。android ArrayAdapter.clear()清除自定義方法變量
我的陣列適配器:
protected class ResultArrayAdapter extends ArrayAdapter<JsonObject> {
private ArrayList<JsonObject> values;
protected ResultArrayAdapter(ArrayList<JsonObject> values) {
super(context, R.layout.content_result_item, values);
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) { ... }
public void updateValues(ArrayList<JsonObject> values) {
//here values.size() > 0 and everything OK
this.clear();
//here values.size() == 0, why??
for (JsonObject value : values) {
this.add(value);
}
this.notifyDataSetChanged();
}
}
當我更新我的價值觀,我打電話updateValues()
,並呼籲this.clear()
,還參數後此方法ArrayList<JsonObject> values
是空的。因此,在此之後,this.add(value)
沒有任何價值,但僅在第一次onUpdate()
之後。
也重命名ArrayList<JsonObject> values
參數,或將它保存在另一個參數中沒有幫助。
因爲ArrayAdapter店ArrayList中通過構造和清晰的方法傳遞引用它(並不總是提防過濾!!!)做明確的...也沒有必要通知適配器後,添加爲它添加的完成方法。而是更新值使用addAll方法 – Selvin
我認爲是不正確的「清除這個」,你應該清除ArrayList。 –
oscargc