I need a custom adapter for my listView. It needs to be passed to a ASyncTask which will get some data from a RSS feed and then hopefully set that data (at the moment Title and Date, both strings) to the row.
The code I came up with is as follow but it's not doing anything with the adapter.
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;
}
}
itemsArrayList
? – Animadvertfor
loop inGetRssFeed
I created aadapterItem
and add the data to it then pass it to the adapter withadapter.add(adaptItem)
which has had the add function overridden to add the data toitemsArrayList
– Damalas