Rick
Rick

Reputation: 4013

How to have the items of more custom adapters in a Fragment?

In my Fragment I'm displaying the RSS feed from a news page on the Web, but I'm able to get these datas only from one Url. Then I display the data of this single Url in my Custom Listview. So, Is there a way I can show datas from more than one Url displayed in different Listviews but in the same Fragment? Perhaps what I am asking is not clear,if so comment and check the image I made below:

enter image description here

Here there's the code that I have in my Fragment:

try {
        URL rssUrl = new URL("URL1");
        SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
        SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
        XMLReader myXMLReader = mySAXParser.getXMLReader();
        RSSHandler myRSSHandler = new RSSHandler();
        myXMLReader.setContentHandler(myRSSHandler);
        InputSource myInputSource = new InputSource(rssUrl.openStream());
        myXMLReader.parse(myInputSource);

        myRssFeed = myRSSHandler.getFeed();

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    if (myRssFeed!=null)
    {
        ListView list = (ListView)view.findViewById(android.R.id.list);
        CustomList adapter = new CustomList(getActivity(),myRssFeed.getList());
        adapter.addAll();
        list.setAdapter(adapter);

    }
    else
        Toast.makeText(getActivity(), "....",
                Toast.LENGTH_LONG).show();
    return view;
}

Edit Custom ListView for a Url:

public class CustomList extends ArrayAdapter<RSSItem> {

private static Activity context = null;
private final List<RSSItem> web;
private SharedPreferences mPrefs;
final String read2 = "text1";
final String testoread2 = "img1";

public CustomList(Activity context, List<RSSItem> web) {
    super(context, R.layout.custom_list, web);
    CustomList.context = context;
    this.web = web;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    final View rowView = inflater.inflate(R.layout.custom_list, null, true);
    mPrefs = PreferenceManager.getDefaultSharedPreferences(context);

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    ....


    return rowView;

}

Any suggestion is appreciated.

EDIT2 What I have now:

try {
        URL rssUrl = new URL("URL1");
        URL rssUrl2 = new URL("URL2");
        SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();

        SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
        SAXParser mySAXParser2 = mySAXParserFactory.newSAXParser();

        XMLReader myXMLReader = mySAXParser.getXMLReader();
        XMLReader myXMLReader2 = mySAXParser2.getXMLReader();

        RSSHandler myRSSHandler = new RSSHandler();
        RSSHandler2 myRSSHandler2 = new RSSHandler2();

        myXMLReader.setContentHandler(myRSSHandler);
        myXMLReader2.setContentHandler(myRSSHandler2);

        InputSource myInputSource = new InputSource(rssUrl.openStream());
        InputSource myInputSource2 = new InputSource(rssUrl2.openStream());

        myXMLReader.parse(myInputSource);
        myXMLReader2.parse(myInputSource2);

        myRssFeed = myRSSHandler.getFeed();
        myRssFeed2 = myRSSHandler2.getFeed();

Upvotes: 1

Views: 253

Answers (3)

ymz
ymz

Reputation: 6914

instead of placing your ListView in the xml, load it dynamically:

ListView list = new ListView(this);
CustomList adapter = new CustomList(getActivity(),myRssFeed.getList());
adapter.addAll();
list.setAdapter(adapter);

then in your xml place a LinearLayout with these attributes:

android:id="@+id/layoutMain"
android:orientation="vertical"
android:layout_height="wrap_content"

back to Java - add your list view to the LinearLayout:

LinearLayout layoutMain = (LinearLayout)findViewById(R.id.layoutMain);
layoutMain.addView(list);

repeat this step for a couple of times to populate your Fragment with multiple lists

EDIT:

about the RssHandlers - please create a function to handle those:

RSSHandler createRssHandler(SAXParserFactory mySAXParserFactory, String url)
{
    URL rssUrl = new URL(url);
    SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
    XMLReader myXMLReader = mySAXParser.getXMLReader();
    RSSHandler myRSSHandler = new RSSHandler();
    myXMLReader.setContentHandler(myRSSHandler);
    InputSource myInputSource = new InputSource(rssUrl.openStream());
    myXMLReader.parse(myInputSource);

   return myRSSHandler;
}

now you can use it with array:

String[] urls = new String["url1", "url2"];
RssHandlers[] handlers = new RssHandlers[urls.length];

for (int i = 0; i < urls.length; i++)
{
     handlers[i] = createRssHandler(mySAXParserFactory, urls[i]); 
} 

combine it with your lists will result something like that:

String[] urls = new String["url1", "url2"];
RssHandlers[] handlers = new RssHandlers[urls.length];
LinearLayout layoutMain = (LinearLayout)findViewById(R.id.layoutMain);

for (int i = 0; i < urls.length; i++)
{
     handlers[i] = createRssHandler(mySAXParserFactory, urls[i]); 

     ListView list = new ListView(this);
     CustomList adapter = new CustomList(getActivity(), handlers[i].getList());
     adapter.addAll();
     list.setAdapter(adapter);
     layoutMain.addView(list);
} 

Upvotes: 1

C0D3LIC1OU5
C0D3LIC1OU5

Reputation: 8680

This is how i would do this:

  1. Upon starting this fragment, start some sort of a loading animation.
  2. Spawn a separate thread (or Loader) for each URL you are hitting (within reason, schedule further as needed) and keep track of which Loaders finished. Once all loaders finish, concatenate the articles they return into some list of objects. Since each URL returns a different object, create a unified model that is aware of the URL where article came from (or type of the article) and is able to standardize data into the same List object. Later on, when you are loading a view for it, you can customize it based on, say, the URL it came from.
  3. Once all loaders finished loading, stop loading animations and use the resulting list of objects to populate your list view.

You should user Loaders with LoaderManager for this. Here is a good tutorial.

Upvotes: 0

Bruce
Bruce

Reputation: 2377

First, I'm a bit confused about your goal. Do you mean that you are hoping to have separate ListViews in the same Fragment, where each ListView is showing items from a different URL? If that is your goal, then you should not do that. It really doesn't work well at all to have multiple ListViews in a scrolling list.

What you can do, however, is build an adapter that handles multiple URLs, and have a single ListView (maybe that is already your goal). In the adapter constructor, you would build a combined list of all the feed items that you find in all the URLs. Your adapter would look like this:

public class CustomList extends BaseAdapter {

    private final List<FeedItem> itemList;

    public CustomList(List<URL> urlList) {
        itemList = new ArrayList<FeedItem>();
        for (URL rssUrl: urlList) {
            //TODO your code to extract feed items from a URL
            itemList.addAll(myRssFeed.getList());
        }
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            //TODO inflate your view from XML
        }
        FeedItem feedItem = itemList.get(position);
        //TODO configure convertView using the data in feedItem
        return convertView;
    }
}

Upvotes: 0

Related Questions