How to combine two or more adapters and show in one ListView on Android

I have ListView custom adapters that return Twitter and Facebook feeds. They also have their own XML. So far, I have shown them in a separate Activity , now I plan to combine both data and show them in one adapter. I heard that there is such a concept as "Merge Adapters". Can someone help me with the code below?

File: FacebookAdapter.java :

 public class FacebookAdapter extends ArrayAdapter<RssFeedStructure> { List<RssFeedStructure> imageAndTexts1 = null; public FacebookAdapter(Activity activity, List<RssFeedStructure> imageAndTexts) { super(activity, 0, imageAndTexts); imageAndTexts1 = imageAndTexts; } @Override public View getView(int position, View convertView, ViewGroup parent) { Activity activity = (Activity) getContext(); LayoutInflater inflater = activity.getLayoutInflater(); View rowView = inflater.inflate(R.layout.facebookadapter, null); TextView textView = (TextView) rowView.findViewById(R.id.feed_text); TextView timeFeedText = (TextView) rowView .findViewById(R.id.feed_updatetime); ImageView imageView = (ImageView) rowView.findViewById(R.id.feed_image); try { Log.d("rssfeed", "imageAndTexts1.get(position).getImgLink() :: " + imageAndTexts1.get(position).getImgLink() + " :: " + imageAndTexts1.get(position).getTitle()); textView.setText(imageAndTexts1.get(position).getDescription()); SpannableString content = new SpannableString(imageAndTexts1.get( position).getPubDate()); content.setSpan(new UnderlineSpan(), 0, 13, 0); timeFeedText.setText(content); if (imageAndTexts1.get(position).getImgLink() != null) { URL feedImage = new URL(imageAndTexts1.get(position) .getImgLink().toString()); if (!feedImage.toString().equalsIgnoreCase("null")) { HttpURLConnection conn = (HttpURLConnection) feedImage .openConnection(); InputStream is = conn.getInputStream(); Bitmap img = BitmapFactory.decodeStream(is); imageView.setImageBitmap(img); } else { imageView.setBackgroundResource(R.drawable.rss_tab_tweets); } } } catch (MalformedURLException e) { } catch (IOException e) { } return rowView; } } 

File: FacebookActivity.java :

 public class FacebookFeeds extends Activity { /** Called when the activity is first created. */ ListView _rssFeedListView; List<JSONObject> jobs; List<RssFeedStructure> rssStr; private FacebookAdapter _adapter; String sorti = ""; String mode = ""; //Button sort_Btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview); _rssFeedListView = (ListView) findViewById(R.id.rssfeed_listview); RssFeedTask rssTask = new RssFeedTask(); rssTask.execute(); } private class RssFeedTask extends AsyncTask<String, Void, String> { // private String Content; private ProgressDialog Dialog; String response = ""; @Override protected void onPreExecute() { Dialog = new ProgressDialog(FacebookFeeds.this); Dialog.setMessage("Rss Loading..."); Dialog.show(); } @Override protected String doInBackground(String... urls) { try { String feed = "https://someurl"; XmlHandler rh = new XmlHandler(); rssStr = rh.getLatestArticles(feed); } catch (Exception e) { } return response; } @Override protected void onPostExecute(String result) { if (rssStr != null) { _adapter = new FacebookAdapter(FacebookFeeds.this, rssStr); _rssFeedListView.setAdapter(_adapter); } Dialog.dismiss(); } } } 

File: TwitterAdapter.java :

 public class TwitterAdapter extends ArrayAdapter<RssFeedStructure> { List<RssFeedStructure> imageAndTexts1 = null; public TwitterAdapter(Activity activity, List<RssFeedStructure> imageAndTexts) { super(activity, 0, imageAndTexts); imageAndTexts1 = imageAndTexts; } @Override public View getView(int position, View convertView, ViewGroup parent) { Activity activity = (Activity) getContext(); LayoutInflater inflater = activity.getLayoutInflater(); View rowView = inflater.inflate(R.layout.twitteradapter, null); TextView textView = (TextView) rowView.findViewById(R.id.feed_text); TextView timeFeedText = (TextView) rowView .findViewById(R.id.feed_updatetime); ImageView imageView = (ImageView) rowView.findViewById(R.id.feed_image); try { Log.d("rssfeed", "imageAndTexts1.get(position).getImgLink() :: " + imageAndTexts1.get(position).getImgLink() + " :: " + imageAndTexts1.get(position).getTitle()); textView.setText(imageAndTexts1.get(position).getTitle()); SpannableString content = new SpannableString(imageAndTexts1.get( position).getPubDate()); content.setSpan(new UnderlineSpan(), 0, 13, 0); timeFeedText.setText(content); if (imageAndTexts1.get(position).getImgLink() != null) { URL feedImage = new URL(imageAndTexts1.get(position) .getImgLink().toString()); if (!feedImage.toString().equalsIgnoreCase("null")) { HttpURLConnection conn = (HttpURLConnection) feedImage .openConnection(); InputStream is = conn.getInputStream(); Bitmap img = BitmapFactory.decodeStream(is); imageView.setImageBitmap(img); } else { imageView.setBackgroundResource(R.drawable.rss_tab_tweets); } } } catch (MalformedURLException e) { } catch (IOException e) { } return rowView; } } 

File: TwitterActivity.java :

 public class TwitterFeeds extends Activity { /** Called when the activity is first created. */ ListView _rssFeedListView; List<JSONObject> jobs; List<RssFeedStructure> rssStr; private TwitterAdapter _adapter; String sorti = ""; String mode = ""; //Button sort_Btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview); _rssFeedListView = (ListView) findViewById(R.id.rssfeed_listview); RssFeedTask rssTask = new RssFeedTask(); rssTask.execute(); } private class RssFeedTask extends AsyncTask<String, Void, String> { // private String Content; private ProgressDialog Dialog; String response = ""; @Override protected void onPreExecute() { Dialog = new ProgressDialog(TwitterFeeds.this); Dialog.setMessage("Rss Loading..."); Dialog.show(); } @Override protected String doInBackground(String... urls) { try { String feed = "https://someurl"; XmlHandler rh = new XmlHandler(); rssStr = rh.getLatestArticles(feed); } catch (Exception e) { } return response; } @Override protected void onPostExecute(String result) { if (rssStr != null) { _adapter = new TwitterAdapter(TwitterFeeds.this, rssStr); _rssFeedListView.setAdapter(_adapter); } Dialog.dismiss(); } } } 
+4
source share
3 answers

Maybe this MergeAdapter can help you: https://github.com/commonsguy/cwac-merge

+1
source

Make the new adapter something like FacebookTwitterAdapter, possibly taking both adapters as constructor parameters.

  • getViewTypeCount - Return 2 (view on facebook or twitter)
  • getItemViewType - returns 0 for viewing on facebook and 1 for viewing twitter
  • getView - returns either facebook or twitter, depending on how you want to display views, possibly on a timeline?
0
source

The easiest option is to create one gigantic element of the array of elements and fill it over one list, perhaps you can assign breaks to the sections between them in the list, and perhaps you can create your own layouts to distinguish between facebook and twitter layouts. OR you can use the "mergeadapter" in the "commonsware" in the "github".

0
source

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


All Articles