4

我必须ListView自定义返回 Twitter 和 Facebook 提要的适配器。他们也有自己的 XML。到目前为止,我一直在单独显示它们Activity,现在我计划将这两个数据合并并显示在一个适配器中。我听说有一些概念,例如“合并适配器”。有人可以帮我下面发布的代码吗?

文件: 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;

}
}

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

}

文件: 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;

}
}

文件: 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

3 回答 3

1

也许这个 MergeAdapter 可以帮助你:https ://github.com/commonsguy/cwac-merge

于 2013-07-25T13:08:05.113 回答
0

最简单的选择可能是创建一个巨大的项目数组列表,并将其填充到单个列表视图中,也许您可​​以在列表视图之间分配分节符,也许您可​​以设计自定义布局以区分 facebook 和 twitter 布局。或者您可以在“github”中通过“commonsware”使用“mergeadapter”。

于 2014-09-24T11:51:39.170 回答
0

制作一个类似于 FacebookTwitterAdapter 的新适配器,可能将两个适配器都作为构造函数参数。

  • getViewTypeCount - 返回 2(facebook 或 twitter 视图)
  • getItemViewType - facebook 视图返回 0,twitter 视图返回 1
  • getView - 根据您希望按时间轴显示视图的方式返回 facebook 或 twitter 视图?
于 2013-07-25T13:08:26.900 回答