ListView
Twitter フィードと Facebook フィードの両方を返すアダプターをカスタマイズする必要があります。また、独自の XML もあります。今までは別々に表示していましたがActivity
、現在は両方のデータを結合して 1 つのアダプターで表示することを計画しています。「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();
}
}
}