我必须ListView
自定义适配器返回Twitter和Facebook Feed。他们也有自己的XML。直到现在我在单独的Activity
中显示它们,现在我计划将数据和显示组合在一个适配器中。我听说有一些概念,比如“Merge Adapters”。有人可以用下面贴出的代码帮助我吗?
档案: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();
}
}
}
答案 0 :(得分:1)
也许这个MergeAdapter可以帮助您:https://github.com/commonsguy/cwac-merge
答案 1 :(得分:0)
创建一个类似于FacebookTwitterAdapter的新适配器,可能将两个适配器作为构造函数参数。
答案 2 :(得分:0)
最简单的选择可能是创建一个巨大的项目列表,并将其填充到单个列表视图中,也许您可以在列表视图中分配两个分节符,也许您可以设计自定义布局来区分Facebook和Twitter布局。或者您可以使用&#34; mergeadapter&#34; by&#34; commonsware&#34;在&#34; github&#34;。