所以,我有一些代码,我认为它远非伟大,但现在我有一个特别有趣的问题。
我使用PkRSS填充List。每次我来回更改片段时,列表都会在顶部添加新获取的项目,即使我在填充之前调用public class UpcommingFragment extends Fragment {
public static final String rss = "XYZ.RSS";
public static List<Article> rssItems = Collections.emptyList();
public RecyclerAdapter recAdapter;
private RecyclerView recList;
public UpcommingFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.recycler, container, false);
recList = (RecyclerView) rootView.findViewById(R.id.recyclerview);
// recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
recList.setItemAnimator(new DefaultItemAnimator());
recAdapter = new RecyclerAdapter(rssItems);
recList.setAdapter(recAdapter);
recList.addOnItemTouchListener(
new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Article a;
a = rssItems.get(position);
String date = a.getDescription().substring(0, 12);
String uri = String.valueOf(a.getSource());
Intent i = new Intent(getActivity(), EventDetailActivity.class);
i.putExtra("date", date);
i.putExtra("head", a.getTitle());
i.putExtra("url", uri);
startActivity(i);
}
})
);
new RSSget().execute();
return rootView;
}
public void onDestroy() {
super.onDestroy();
rssItems = Collections.emptyList();
}
public boolean isOnline() {
ConnectivityManager connectivity = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (NetworkInfo anInfo : info)
if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
// AsyncTask
private class RSSget extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try {
rssItems = Collections.emptyList();
rssItems = PkRSS.with(getActivity()).load(rss).get();
Log.e("LISTCOUNTER", String.valueOf(rssItems.size()));
} catch (Exception e) {
Log.i("Error", "eror");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
recAdapter = new RecyclerAdapter(rssItems);
recList.setAdapter(recAdapter);
}
}
public void onResume() {
super.onResume();
}
}
。
继承守则:
09-12 11:33:08.050 23259-23329/com.mikebdev.douala E/LISTCOUNTER﹕ 12
09-12 11:33:11.701 23259-23306/com.mikebdev.douala E/LISTCOUNTER﹕ 24
09-12 11:33:15.017 23259-23323/com.mikebdev.douala E/LISTCOUNTER﹕ 36
并且继承了LogCat
req = "POST /incomingData/addData/"
.." HTTP/1.1\r\n"
.."Host: secret-spire-9368.herokuapp.com\r\n"
.."Connection: close\r\n"
.."Accept: */*\r\n"
.."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
.."Content-Type: application/json\r\n"
.."Content-Length: "..string.len(json).."\r\n"
.."\r\n"
..json.."\r\n"
正如您所看到的,每次我来回更改片段时,List都会将这12个元素添加到顶部。
当然,如果您在此代码中找到我可以做得更好的话,我会感激不尽
如果您还需要查看其他内容,请告知我们。
提前致谢
答案 0 :(得分:1)
不熟悉PkRSS,但在github页面上快速一瞥,似乎这是一种有状态的RSS阅读器。也许您不能简单地调用其加载方法而不清除以前读取的数据?
答案 1 :(得分:0)
试试这个
@Override
protected void onPostExecute(Void result) {
recAdapter = new RecyclerAdapter(rssItems);
recList.setAdapter(recAdapter);
recAdapter.notifyDataSetChange();
}
}