我正在解析一個Web服務以顯示在由我自己的ArrayAdapter子類支持的listView中。數據爲Application.java
中的static ArrayList<Wait>
。你會看到它參考App.getWaits()
。使用ArrayAdapter刷新ListView
我有一個簡單的刷新方法,當有新的數據。我已經確認它正在更新,但它只會呈現,如果我離開,然後返回到視圖。
在過去,我已經能夠通過調用適配器上的notifyDataSetChanged()來刷新listView,但現在這些都沒有爲我工作。感謝您看看...任何想法!?
//1 This is how I'd normally update the listView dynamically, but not tonight.
adapter.notifyDataSetChanged();
//2 It's the same thing really, so no good.
((WaitAdapter) list.getAdapter()).notifyDataSetChanged();
//3 Saw this as the answer to a similar question, doesn't work.
adapter.getWaits().clear();
adapter.getWaits().addAll(App.getWaits());
adapter.notifyDataSetChanged();
//4 Called in onCreate but tried a 2nd time in refresh() to manually reset adapter, doesn't work.
adapter = new WaitAdapter(getHost().getApplicationContext(), App.getWaits());
list.setAdapter(adapter);
//5 Kinda the same thing, new adapter, reset adapter... also no good.
WaitAdapter adapter = new WaitAdapter(getHost().getApplicationContext(), App.getWaits());
list.setAdapter(adapter);
//6 I read ArrayAdapter keeps its own reference to initial data object but this fails too.
adapter = null;
adapter = new WaitAdapter(getHost().getApplicationContext(), App.getWaits());
list.setAdapter(adapter);
*更新以分享我的WaitAdapter.java
。
public class WaitAdapter extends ArrayAdapter<Wait> {
private LayoutInflater inflater;
private ArrayList<Wait> waits;
public WaitAdapter(Context context, ArrayList<Wait> data) {
super(context, R.layout.list_item_wait, data);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
waits = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_wait, parent, false);
holder = new ViewHolder();
holder.checkpointName = (TextView) convertView.findViewById(R.id.checkpointName);
holder.delayAmount = (TextView) convertView.findViewById(R.id.delayAmount);
holder.timeReported = (TextView) convertView.findViewById(R.id.timeReported);
holder.dateReported = (TextView) convertView.findViewById(R.id.dateReported);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Wait wait = waits.get(position);
holder.checkpointName.setText(wait.getName());
holder.delayAmount.setText(wait.getDelayInMinutes());
holder.timeReported.setText(wait.getTimeLabel());
holder.dateReported.setText(wait.getDateLabel());
return convertView;
}
@Override
public boolean isEnabled(int position) {
return false;
}
static class ViewHolder {
TextView checkpointName;
TextView delayAmount;
TextView timeReported;
TextView dateReported;
}
}
12/14/14更新:一般實施背景。
啓動時,App類啓動WaitAsyncTask,它解析遠程XML以填充其ArrayList等待。我會在幾個地方訪問這些等待,這樣我就可以保持全局。
WaitFragment與WaitAdapter一起使用,在ListView中顯示等待並偵聽等待的更改。用戶可以通過AlertDialog等待Web服務等待。一個成功的響應再次執行WaitAsyncTask,更新等待對象,觸發WaitFragment刷新()。
控制檯日誌和Web服務確認此控制流,並且等待得到更新。如果我離開WaitFragment然後返回,它顯示更新的等待。發佈了評論#1-6的代碼就是我在refresh()裏面嘗試更新ListView的內容。
我對這個應用程序中的其他數據和片段使用這種通用方法,它們的UI按預期刷新,但沒有一個是listView。我不確定如果沒有對其中的大部分內容進行編輯,我都可以發佈更多源代碼,但是一旦我開始工作,我會分享我的調查結果。我以前沒有遇到過使用ListView的麻煩,但肯定會令人尷尬。謝謝大家誰花了一點時間:)
發佈您的適配器! – mmlooloo 2014-12-13 06:44:13
這可能是因爲你的ArrayList'waits'沒有得到新數據的更新。 – Shvet 2014-12-13 07:19:59
您是否在任何時候過濾適配器? – 2014-12-13 12:02:44