大家好。在选项菜单项上单击“我运行一种方法”,它可以正常工作,但是如果再次单击,则会更新列表,但列表的第一项没有显示出来,它将显示两次。点击列表视图时需要刷新,并且列表仅显示新项目。
这是标签页布局,因此我只写一些代码即可听到
MainActvity
TAZA taza;
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter ( FragmentManager fm ) {
super(fm);
}
@Override
public Fragment getItem ( int position ) {
switch (position) {
case 0:
taza = new TAZA();
return taza;
return null;
}
@Override
public boolean onCreateOptionsMenu ( Menu menu ) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected ( MenuItem item ) {
int id = item.getItemId();
if (id == R.id.refresh) {
taza.aa();
return true;
}
这是片段标签; TAZA.java
public class TAZA extends Fragment {
public TAZA () {
// Required empty public constructor
}
ArrayList <NewsItem> newsItemsList;
ListView lvRss;
NewsAdaptor adapter;
Button button;
ProgressDialog pDialog;
@Override
public View onCreateView ( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ) {
View view = inflater.inflate(R.layout.taza, container, false);
lvRss = (ListView) view.findViewById(R.id.listtaza);
newsItemsList = new ArrayList <>();
RequestQueue queue = MySingleton.getInstance(getActivity().getApplicationContext()).
getRequestQueue();
return view;
}
public void aa(){
StringRequest stringRequest1 = new StringRequest
("https://politepol.com/feed/18863", new Response.Listener <String>() {
@Override
public void onResponse ( String response ) {
Log.d("asd","aa");
Document documentgeo = Jsoup.parse(response);
Elements itemElementsge = documentgeo.getElementsByTag("item");
for (int i = 0; i < itemElementsge.size(); i++) {
Element itemge = itemElementsge.get(i);
String s = itemge.child(0).text();
Document document1 = Jsoup.parse(s);
String img = document1.getElementsByTag("img").first().attr("src");
String title = document1.getElementsByTag("img").first().attr("title");
String link = document1.getElementsByTag("a").first().attr("href");
String dateeee = rem1(itemge.child(3).text());
Log.d("zxc",""+link);
String datevee = dateeee;
SimpleDateFormat spf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
Date newDate = null;
try {
newDate = spf.parse(datevee);
} catch (ParseException e) {
e.printStackTrace();
}
spf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
datevee = spf.format(newDate);
NewsItem newsItem = new NewsItem();
newsItem.setImagePath(img);
newsItem.setDate(datevee);
newsItem.setTitle(title);
newsItem.setLink(link);
newsItem.setDatedisplay("DATE" + " " + rem(datevee));
newsItemsList.add(newsItem);
}
adapter = new NewsAdaptor(getContext(), newsItemsList);
lvRss.setAdapter(adapter);
lvRss.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick ( AdapterView <?> parent, View view, int position, long id ) {
NewsItem currentNews = newsItemsList.get(position);
Intent intent = new Intent(getContext(), Detail.class);
intent.putExtra("NEWS", currentNews);
startActivity(intent);
}
});
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse ( VolleyError error ) {
aa();
Log.d("asd","a");
}
});
MySingleton.getInstance(getContext()).addToRequestQueue(stringRequest1);
}
newsApapter
public class NewsAdaptor extends BaseAdapter {
TextView tvtitle;
RelativeLayout view2;
Context context;
private int color;
private int colorback;
private int textSize;
public NewsAdaptor ( Context context, ArrayList <NewsItem> newsList ) {
this.context = context;
this.newsList = newsList;
this.color = Color.RED;
}
ArrayList <NewsItem> newsList;
@Override
public int getCount () {
return newsList.size();
}
@Override
public Object getItem ( int position ) {
return newsList.get(position);
}
@Override
public long getItemId ( int position ) {
return 0;
}
@SuppressLint("ResourceAsColor")
@Override
public View getView ( int position, View convertView, ViewGroup parent ) {
if (convertView == null) {
convertView = View.inflate(context, R.layout.newsitemlist_layout, null);
}
NewsItem currentNews = newsList.get(position);
ImageView iv1 = (ImageView) convertView.findViewById(R.id.mainimg);
tvtitle = (TextView) convertView.findViewById(R.id.textView1id);
TextView tvdate = (TextView) convertView.findViewById(R.id.pubDateid);
tvtitle.setText(currentNews.getTitle());
tvdate.setText(currentNews.getDatedisplay());
return convertView;
}
答案 0 :(得分:0)
只需在方法开始时添加newsItemList.clear()
。不会重复数据。