【发布时间】:2016-02-05 16:31:27
【问题描述】:
我有一个片段页面,它通过 web 服务在列表视图中列出来自 json 的数据。我有一个下拉<SwipeRefreshLayout>。我使用滑动片段页面来刷新数据,这将再次调用 web 服务并将数据加载到列表视图中。
下面的代码在刷新后不会清空旧数据。数据仍然保留在列表视图中,并将刷新的数据添加到旧数据下方的列表视图中。
刷新方法我再次调用服务
ArrayList<DataListItem> listMockData;
DataBaseAdapter HListAdapter;
swipeLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
HListAdapter.clear();
listMockData.clear();
HListAdapter.notifyDataSetChanged();
requestWebService(currentPage);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
swipeLayout.setRefreshing(false);
}
}, 1000);
}
});
BaseAdapter
public DataBaseAdapter(Context context, ArrayList<DataListItem> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
public void clear() {
listData.clear();
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
newsItem = listData.get(position);
if (convertView == null) {
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.home_post_list, null);
holder.results = (LinearLayout) convertView.findViewById(R.id.results);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.results.setText(newsItem.getResults());
holder.position = position;
return convertView;
}
public static class ViewHolder {
TextView results;
}
编辑调用网络服务
public void requestPosts(int pageId) {
JSONObject pagedata = new JSONObject();
try {
pagedata.put("pageId", pageId);
} catch (JSONException e) {
e.printStackTrace();
}
Volley.newRequestQueue(getActivity()).
add(new JsonObjectRequest(com.android.volley.Request.Method.POST,
url, pagedata
, new com.android.volley.Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonArray) {
if (jsonArray != null) {
try {
JSONObject allPost = new JSONObject(jsonArray.toString());
contacts = allPost.optJSONArray("GetPostListResult");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String Result = c.getString("Result");
results[x] = Result;
x++;
}
} catch (JSONException e) {
e.printStackTrace();
}
} else{
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
listMockData = new ArrayList<HomeListItem>();
DataListItem newsData = new DataListItem();
newsData.setResult(results[i]);
listMockData.add(newsData);
dataToListView();
}
});
}
public void dataToListView(){
listView = (ListView) root.findViewById(R.id.custom_list);
HListAdapter = new DataBaseAdapter(getActivity(), listMockData);
listView.setAdapter(HListAdapter);
}
【问题讨论】:
-
把再次调用服务的部分注释掉,看看值是否被清除。如果是这样,您的服务将与旧数据一起返回新数据。发布您的服务代码。
-
我已经发布了我在刷新时调用服务的代码
-
results声明在哪里?你似乎没有清除这个数组,它似乎不是本地的? -
x也不是本地的,永远不会重置为 0,对吗? -
results由setResults在 bean 类中设置。并使用getResults我将其设置在BaseAdapter中的TextView上
标签: android listview android-fragments