User adapter does not show data in ListView

I need a custom adapter for my list. It should be passed to ASyncTask, which will receive some data from the RSS feed, and then, hopefully, set this data (at the time of the title and date, both lines) to the string.

The code I came up with corresponds to the following, but it does nothing 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; } } 
+5
source share
4 answers

In doInBackground of GetRssFeed you create an adapterItem outside of for , this only adds the last RssItem to the adapter. I think you should create an instance of each adapterItem inside the for loop.

 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; } } 

Meanwhile, it looks like you are not overriding the add RssListViewAdapater method correctly. In fact, you do not need to override add at all. ArrayAdapter will handle it for you.

+2
source

Use BaseAdapter

 public class TestBaseAdap extends BaseAdapter { ArrayList<adapterItem> itemsArrayList; public TestBaseAdap(ArrayList<String> itemsArrayList) { super(); this.itemsArrayList = itemsArrayList; } @Override public int getCount() { return itemsArrayList.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) parent.getContext() .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 convertView; } } 

You just need to take one gobal ArrayList

 ArrayList<adapterItem> mArrayList = new ArrayList<adapterItem>(); 

and then in asyncTask add this:

 mArrayList.add(adaptItem); 

Finally, install the adapter in this way.

 listToUse.setAdapter(new TestBaseAdap(mArrayList)); 

Done.

+2
source

try this one.

Replace

 adapter.add(adaptItem); 

as

 adapter.addDatas(adaptItem); 

in GetRssFeed.java and replace the adapter class RssListViewAdapter.java as

 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; } } 

by Defalut RssReader does not read pubDate from xml. If you want to read pubDate in RssReader, copy the code and replace it with 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)); } } } 
+1
source

Have the same problem. In android 4.1, everything is visible in lines. But in android 6 there are empty lines. I was frightened. But find out why.

Just add text color

android:textColor="#000"

0
source

Source: https://habr.com/ru/post/1246755/


All Articles