如何合并两个或多个适配器并在Android中的一个ListView中显示

如何合并两个或多个适配器并在Android中的一个ListView中显示,第1张

概述我必须使用ListView自定义适配器返回Twitter和Facebook提要.他们也有自己的 XML.直到现在我在单独的Activity中显示它们,现在我计划将数据和show组合在一个适配器中.我听说有一些概念,比如“Merge Adapters”.有人可以通过以下贴出的代码帮助我吗? 文件:FacebookAdapter.java: public class FacebookAdapter e 我必须使用ListVIEw自定义适配器返回Twitter和Facebook提要.他们也有自己的 XML.直到现在我在单独的Activity中显示它们,现在我计划将数据和show组合在一个适配器中.我听说有一些概念,比如“Merge Adapters”.有人可以通过以下贴出的代码帮助我吗?

文件:FacebookAdapter.java:

public class FacebookAdapter extends ArrayAdapter<RSSFeedStructure> {List<RSSFeedStructure> imageAndTexts1 = null;public FacebookAdapter(Activity activity,List<RSSFeedStructure> imageAndTexts) {    super(activity,imageAndTexts);    imageAndTexts1 = imageAndTexts;}@OverrIDepublic 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(),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;}}

文件: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;@OverrIDepublic 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();    }}}

文件:Twitteradapter.java:

public class Twitteradapter extends ArrayAdapter<RSSFeedStructure> {List<RSSFeedStructure> imageAndTexts1 = null;public Twitteradapter(Activity activity,VIEwGroup parent) {    Activity activity = (Activity) getContext();    LayoutInflater inflater = activity.getLayoutInflater();    VIEw rowVIEw = inflater.inflate(R.layout.twitteradapter,"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);        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;}}

文件: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;@OverrIDepublic 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,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();    }}}
解决方法 也许这个MergeAdapter可以帮到你: https://github.com/commonsguy/cwac-merge 总结

以上是内存溢出为你收集整理的如何合并两个或多个适配器并在Android中的一个ListView中显示全部内容,希望文章能够帮你解决如何合并两个或多个适配器并在Android中的一个ListView中显示所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1124965.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存