我正在解析一个Web服务,以显示在我自己的ArrayAdapter子类支持的listView中。 static ArrayList<Wait>
中的数据为Application.java
。您会看到App.getWaits()
引用它。
我使用简单的刷新方法来获取新数据。我已经确认它已更新,但只有在我离开然后返回视图时才会显示。
过去我通过调用适配器上的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,更新waits对象,触发WaitFragment refresh()。
控制台日志和Web服务确认此控制流并等待更新。如果我离开WaitFragment然后返回,它会显示更新的等待。使用注释#1-6发布的代码是我在refresh()内部尝试更新ListView的。
我在这个应用程序中使用这种通用方法与其他数据和片段,并且他们的UI按预期刷新,但没有一个是listViews。我不确定我是否可以发布更多来源而不会编辑大部分内容,但是一旦我开始工作,我就会分享我的发现。我之前没有遇到过ListView的问题,但它确实令人尴尬。感谢所有花了一点时间的人:)
答案 0 :(得分:0)
只需在Adapter类中创建一个方法来更新/刷新listview,如下所示,
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;
}
/**
* Update content
*/
public void updateListContent(ArrayList<Wait> data)
{
waits = data;
notifyDataSetChanged();
}
}
在您的acivity类中,只需调用此适配器方法即可更新内容。请参阅以下代码
注意:不要清除适配器的数组内容。
//Dont clear the arraylist of adapter
adapter.updateListContent(App.getWaits());
这可能会对你有帮助。