我的listView需要一个自定义适配器。它需要传递给ASyncTask,它将从RSS提要中获取一些数据,然后希望将该数据(当前标题和日期,两个字符串)设置为该行。
我提出的代码如下,但它没有对适配器做任何事情。
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity
{
private ListView listOne;
private ListView listTwo;
RssListViewAdapter adapterOne;
RssListViewAdapter adapterTwo;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.rss_layout);
//Connect all three columns to their ListViews
listOne = (ListView) findViewById(R.id.listOne);
listTwo = (ListView) findViewById(R.id.listTwo);
//Create both adapaters
//TODO: Make custom adapater
adapterOne = new RssListViewAdapter(this);
adapterTwo = new RssListViewAdapter(this);
//Class once for each column. Pass feed URL and Adapter.
new GetRssFeed("http://feeds.arstechnica.com/arstechnica/index", adapterOne, listOne).execute();
new GetRssFeed("http://feeds.skynews.com/feeds/rss/home.xml", adapterTwo, listTwo).execute();
}
}
GetRssFeed.java
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ListView;
/**
* Created by jameskrawczyk on 07/04/16.
*/
public class GetRssFeed extends AsyncTask<String, Void, Void>
{
String url;
RssListViewAdapter adapter;
ListView listToUse;
public GetRssFeed(String urlInput, RssListViewAdapter adapterInput, ListView listInput)
{
this.url = urlInput;
this.adapter = adapterInput;
this.listToUse = listInput;
}
@Override
protected Void doInBackground(String... params)
{
try
{
RssReader rssReader = new RssReader(url);
Integer i = 0;
adapterItem adaptItem = new adapterItem();
for (RssItem item : rssReader.getItems())
{
adaptItem.setHeader(item.getTitle());
adaptItem.setDate(item.getDate());
adapter.add(adaptItem);
i++;
if(i == 7)
{
break;
}
}
} catch (Exception e)
{
Log.v("Error Parsing Data", e + "");
}
return null;
}
@Override
protected void onPostExecute(Void aVoid)
{
super.onPostExecute(aVoid);
listToUse.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
的RSSItem
public class RssItem {
String title;
String description;
String link;
String imageUrl;
String pubDate;
public String getDescription() {
return description;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getTitle() {
return title;
}
public String getLink() {
return link;
}
public String getDate(){return pubDate;}
public void setLink(String link) {this.link = link;}
public void setDescription(String description) {
this.description = description;
}
public void setTitle(String title) {
this.title = title;
}
public void setDate(String pubDate) {this.pubDate = pubDate;}
}
RssListViewAdapater
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class RssListViewAdapter extends ArrayAdapter<adapterItem> {
private final Context context;
private adapterItem itemsArrayList;
public RssListViewAdapter(Context context) {
super(context, R.layout.basic_list_item);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.basic_list_item, parent, false);
TextView headerView = (TextView) rowView.findViewById(R.id.headerName);
TextView dateView = (TextView) rowView.findViewById(R.id.dateTime);
headerView.setText(itemsArrayList.getHeader());
dateView.setText(itemsArrayList.getDate());
return rowView;
}
@Override
public void add(adapterItem theData)
{
this.itemsArrayList = theData;
}
}
adapterItem
public class adapterItem {
private String header;
private String date;
public adapterItem() {
super();
this.header = header;
this.date = date;
}
public void setHeader(String head)
{
header = head;
}
public void setDate(String dat)
{
date = dat;
}
public String getHeader()
{
return header;
}
public String getDate()
{
return date;
}
}
答案 0 :(得分:2)
在doInBackground
的{{1}}中,您实例化GetRssFeed
外部adapterItem
循环,这会导致只有最后一个for
被添加到适配器。我认为你应该在RssItem
循环中实例化每个adapterItem
。
for
与此同时,您似乎无法正确覆盖for (RssItem item : rssReader.getItems())
{
adapterItem adaptItem = new adapterItem();
adaptItem.setHeader(item.getTitle());
adaptItem.setDate(item.getDate());
adapter.add(adaptItem);
i++;
if(i == 7)
{
break;
}
}
add
方法。事实上,您根本不必覆盖RssListViewAdapater
。 add
将为您处理。
答案 1 :(得分:1)
尝试这个。
替换
adapter.add(adaptItem);
代码为
adapter.addDatas(adaptItem);
在 GetRssFeed.java 中并将 RssListViewAdapter.java 适配器类替换为
public class RssListViewAdapter extends BaseAdapter {
private final Context context;
private ArrayList<adapterItem> itemsArrayList;
public RssListViewAdapter(Context context) {
this.context = context;
itemsArrayList=new ArrayList<adapterItem>();
}
public void addDatas(adapterItem item){
itemsArrayList.add(item);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return getItem(position);
}
@Override
public int getCount() {
return itemsArrayList.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.basic_list_item, parent, false);
TextView headerView = (TextView) rowView.findViewById(R.id.headerName);
TextView dateView = (TextView) rowView.findViewById(R.id.dateTime);
headerView.setText(itemsArrayList.get(position).getHeader());
dateView.setText(itemsArrayList.get(position).getDate());
return rowView;
}
}
由Defalut RssReader 没有从xml读取pubDate。如果您希望在 RSSReader 中阅读pubDate,请复制代码并将其替换为 RssHandler.java
<强> RssHandler.Java 强>
public class RssHandler extends DefaultHandler {
private List<RssItem> rssItemList;
private RssItem currentItem;
private boolean parsingTitle;
private boolean parsingLink;
private boolean parsingDescription;
private boolean parsingPubDate;
public RssHandler() {
//Initializes a new ArrayList that will hold all the generated RSS items.
rssItemList = new ArrayList<RssItem>();
}
public List<RssItem> getRssItemList() {
return rssItemList;
}
//Called when an opening tag is reached, such as <item> or <title>
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("item"))
currentItem = new RssItem();
else if (qName.equals("title"))
parsingTitle = true;
else if (qName.equals("link"))
parsingLink = true;
else if (qName.equals("description"))
parsingDescription = true;
else if(qName.equals("pubDate"))
parsingPubDate=true;
else if (qName.equals("media:thumbnail") || qName.equals("media:content") || qName.equals("image")) {
if (attributes.getValue("url") != null)
currentItem.setImageUrl(attributes.getValue("url"));
}
}
//Called when a closing tag is reached, such as </item> or </title>
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("item")) {
//End of an item so add the currentItem to the list of items.
rssItemList.add(currentItem);
currentItem = null;
} else if (qName.equals("title"))
parsingTitle = false;
else if (qName.equals("link"))
parsingLink = false;
else if (qName.equals("description"))
parsingDescription = false;
else if(qName.equals("pubDate"))
parsingPubDate=false;
}
//Goes through character by character when parsing whats inside of a tag.
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (currentItem != null) {
//If parsingTitle is true, then that means we are inside a <title> tag so the text is the title of an item.
if (parsingTitle)
currentItem.setTitle(new String(ch, start, length));
//If parsingLink is true, then that means we are inside a <link> tag so the text is the link of an item.
else if (parsingLink)
currentItem.setLink(new String(ch, start, length));
//If parsingDescription is true, then that means we are inside a <description> tag so the text is the description of an item.
else if (parsingDescription)
currentItem.setDescription(new String(ch, start, length));
else if(parsingPubDate)
currentItem.setDate(new String(ch,start,length));
}
}
}
答案 2 :(得分:0)
有同样的问题。 在android 4.1中,一切都在行中可见。 但在android 6中是空行。 我搞砸了。但找到原因。
只需添加文字颜色
android:textColor="#000"