2017-04-06 40 views
2

我有一個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參數,或將它保存在另一個參數中沒有幫助。

+2

因爲ArrayAdapter店ArrayList中通過構造和清晰的方法傳遞引用它(並不總是提防過濾!!!)做明確的...也沒有必要通知適配器後,添加爲它添加的完成方法。而是更新值使用addAll方法 – Selvin

+0

我認爲是不正確的「清除這個」,你應該清除ArrayList 。 – oscargc

回答

1

試試這個:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { ... } 
public void updateValues(ArrayList<JsonObject> newValues) { 
    //here values.size() > 0 and everything OK 
    values.clear(); 

    //here values.size() == 0, why?? 
    for (JsonObject value : newValues) { 
     values.add(value); 
    } 

    this.notifyDataSetChanged(); 
} 
+0

thx,但已經嘗試過......沒有工作 – wendt88

1

好像你正在與ArrayList你用構造函數設置的相同實例更新值。您可以通過不設置外部的初始值來超越這一點。另外,您實際上並不需要班上的values會員。你

protected class ResultArrayAdapter extends ArrayAdapter<JsonObject> { 
    protected ResultArrayAdapter(List<JsonObject> values) { 
     super(context, R.layout.content_result_item, new ArrayList<JsonObject>()); 
     updateValues(values); 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { ... } 
    public void updateValues(List<JsonObject> values) { 
     this.clear(); 
     this.addAll(values); 
     this.notifyDataSetChanged(); 
    } 
} 

也可以使用addAll()代替add()內的for循環,並提供List而不是ArrayList的。如果你喜歡,你甚至可以選擇Collection

0

的問題是:如果我與它的值更新一個ArrayAdapter我喜歡初始化:

ListView resultsView = (ListView)findViewById(R.id.results); 
ResultArrayAdapter adapter = (ResultArrayAdapter)resultsView .getAdapter(); 
if(adapter == null){ 
    adapter = new ResultArrayAdapter(MyActivity.this, values); 
    resultsView .setAdapter(adapter); 
} else { 
    adapter.updateValues(values); 
} 

所以this.clear()清除我的價值觀。例如我初始化我的數組適配器的值列表1,然後我更新值列表2,所以它崇拜,但更新值列表1再次,它是空的。 我的工作是你的例子的混合,所以THX!

protected class ResultArrayAdapter extends ArrayAdapter<JsonObject> { 
    private ArrayList<JsonObject> values; 
    protected ResultArrayAdapter(Context context, ArrayList<JsonObject> values) { 
     super(context, R.layout.content_result_item, new ArrayList<JsonObject>()); //passing new fake values 
     this.values = values; //without this, it crashes 
     updateValues(values); 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { ... } 
    public void updateValues(List<JsonObject> values) { 
     this.clear(); 
     this.addAll(values); 
    } 
}