Merge Listviews Android

Asked

Viewed 55 times

0

In my application, I have two Fragments, one to load Tweets and one to Read an RSS. RSS

public class RssFragment extends ListFragment {

    private RssListAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

            List<JSONObject> jobs = new ArrayList<JSONObject>();
            try {
                jobs = RssReader.getLatestRssFeed();
            } catch (Exception e) {
                Log.e("RSS ERROR", "Error loading RSS Feed Stream >> " + e.getMessage() + " //" + e.toString());
            }

            adapter = new RssListAdapter(getActivity(),jobs);
            setListAdapter(adapter);

            return super.onCreateView(inflater, container, savedInstanceState);    

    }

Tweets
public class Noticias extends ListFragment {
    final static String twitterScreenName = "";
    final static String TWITTER_API_KEY = "";
    final static String TWITTER_API_SECRET = "";
    private static final String TAG = "Tweet";


    @Override  
      public void onListItemClick(ListView l, View v, int position, long id) {  
       //new CustomToast(getActivity(), numbers_digits[(int) id]);     
      }  

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        .permitNetwork()
        .build());

            ArrayList<TwitterTweet> twitterTweets = null;
            TwitterAPI twitterAPI = new TwitterAPI(TWITTER_API_KEY,TWITTER_API_SECRET);
            twitterTweets = twitterAPI.getTwitterTweets("LinkOffTMMG");

            ArrayAdapter<TwitterTweet> adapter = new ArrayAdapter<TwitterTweet>(  
                inflater.getContext(), R.layout.twitter_tweets_list,  
                R.id.listTextView, twitterTweets);  

            setListAdapter(adapter);  

            return super.onCreateView(inflater, container, savedInstanceState);    

    }
}

inserir a descrição da imagem aqui

How do I create another Fragment by joining the content of the other two ordered by the latest date?

  • My suggestion would be to create a Adapter customized (subclass of Basedapter) with access to these two Dataset’s to show off the two.

  • But how do I get more than one guy on a Basedapter?

  • I believe you will have to create a class that implements the logic of merge. Implementing the abstract methods of the `Basedapter class.

1 answer

1

You need programming based on Interfaces.

In short, create an interface that contains two methods, one to get the news photo and the other to get the headline. After that, create a custom Adapter that you inherit from Basedapter and get a list from that interface. Done! :-)

public interface Noticia {
   Bitmap getFoto();
   String getTitulo();
}

public class NoticiaAdapter extends BaseAdapter {

   private List<Noticia> mNoticias;

   public NoticiaAdapter(Context context, List<Noticia> noticias) {
      this.mNoticias = noticias;
   }

   getCount(...) { ... }
   getItem(...) { ... }
   getItemId(...) { ... }
   getView(...) { ... }

}
  • The idea is good, but lacked a getter or field for date, OP wants the list ordered by latest news. Maybe it is the case of Noticia be an abstract class and not an interface.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.