我正在开发一个Android应用程序,它将RSS数据从给定的URL下载到ListView,我可以通过多种方式对项目进行排序(按字母顺序,按日期,按标题等)。
我希望能够删除从给定网址下载的所有项目。 排序会更改原始项目位置,因此我不能简单地按位置删除项目。
在这里,我详细解释它是如何工作的:
加载到片段B ListView中的每个RSS项目都有唯一的标题,链接和描述以及发布日期。
这意味着加载的项目中没有共同的值。 我下载了“频道”部分,它是从给定URL下载的所有“项目”的父项,但我没有在片段的B适配器中使用它。
以下是POJO的代码:
@Root(name = "rss", strict = false)
public class RSS {
@Element
private Channel channel;
@Attribute
private String version;
public Channel getChannel() {
return channel;
}
}
@Root(name = "channel", strict = false)
public class Channel {
@ElementList(name = "item", inline = true)
List<Article> articleList;
@Element
private String title;
@Element
private String link;
@Element
private String description;
// setters and getters
}
@Root(name = "item", strict = false)
public class Article {
@Element
private String title;
@Element
private String description;
@Element
private String link;
@Element(required = false)
private String author;
@Element(required = false)
private String pubDate;
// setters and getters
}
这是用于在片段A的ListView适配器中存储URL的URL类。
public class URL {
private String url;
URL(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
public void setUrl(String title) {
this.url = title;
}
}
现在,片段B下载RSS对象(此处称为“文章”),这就是我将项目加载到另一个ListView适配器的方式:
previewList = articles.getChannel().articleList;
adapter.addTop(previewList);
adapter.notifyDataSetChanged();
最后,片段B的ListView适配器:
public class ArticlePreviewAdapter extends ArrayAdapter<Article> {
List<Article> articlePreviewItems;
public ArticlePreviewAdapter(Activity activity, List<Article> articlePreviewItems) {
super(activity, R.layout.item_article_preview, articlePreviewItems);
this.articlePreviewItems = articlePreviewItems;
}
private static class ViewHolder {
TextView articlePreviewTitle;
TextView articlePreviewLink;
TextView articlePreviewDescription;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
Article articlePreviewItem = getItem(position);
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_article_preview, parent, false);
viewHolder.articlePreviewTitle = (TextView) convertView.findViewById(R.id.articleTitle);
viewHolder.articlePreviewLink = (TextView) convertView.findViewById(R.id.articleLink);
viewHolder.articlePreviewDescription = (TextView) convertView.findViewById(R.id.articleDescription);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.articlePreviewTitle.setText(articlePreviewItem.getTitle());
viewHolder.articlePreviewLink.setText(articlePreviewItem.getLink());
viewHolder.articlePreviewDescription.setText(articlePreviewItem.getDescription());
return convertView;
}
public void removeChannelItems(what do) {
what do?
}
public void addTop(List<Article> articles) {
articlePreviewItems.addAll(0, articles);
}
}
提前感谢您的帮助。
答案 0 :(得分:0)
如果您的Article
对象(或RSS
或Channel
)可以保留对多个URL
的引用,则可以实现如下所述的关联({{1}以身作为例)
Article
然后你会public class URL {
private String url;
private ArrayList<String> articleTitles;
public class URL(){
this.articleTitles = new ArrayList<String>();
}
public void setArticleTitles(String articleTitle){
this.articleTitle.add(articleTitle);
}
public ArrayList<String> getAarticleTitles(){
return this.articleTitles;
}
URL(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
public void setUrl(String title) {
this.url = title;
}
}
引用你的对象。删除URL
后,您会从相应的URL
中删除所有Articles
,最后在adapter
上调用notifyDataSetChanged();
。