我尝试按照此处的建议(How to refresh Android listview?)更新我的ListView,但无济于事。
相关代码如下。请注意,我在MainActivity中调用runOnUiThread来刷新此(片段)的ListView。
protected ArrayAdapter<String> adapter;
protected View view;
protected ArrayList<String> theList;
protected ListView listView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(R.layout.fragment_1, container, false);
listView = (ListView) view.findViewById(R.id.listview);
theList = new ArrayList<String>();
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, theList);
listView.setAdapter(adapter);
if(view != null) { return view;}
((ViewGroup) listView.getParent()).removeView(listView);
container.addView(listView);
return view;
}
protected void updateListViewWithPath(String resultFromServerRead) {
//TODO: Somehow split the information..
theList.add("yo");
theList.add("hey");
adapter.clear();
adapter.addAll(theList);
listView.invalidateViews();
adapter.notifyDataSetChanged();
listView.refreshDrawableState();
}
答案 0 :(得分:1)
您已经拥有对arraylist列表的引用,因此您无需再次将其添加到listView只需将字符串添加到arraylist
和notifyDataSetChanged
;
示例:
protected void updateListViewWithPath(String resultFromServerRead) {
//TODO: Somehow split the information..
theList.add("yo");
theList.add("hey");
adapter.notifyDataSetChanged();
}