Below is my RSS Parser in Application. Now I want the same data to be inserted in Widget Class so that i can show RSS Feeds into widget as well. Can someone show me how to pass those application data into my widget listview. Below are the Application and Widget Classes.
Note : I will be replacing "heading" and "content" variables in ListProvider.java to show my data there. Let me know if someone needs more classes which i can post if required to refer.
For Example : imageAndTexts1.get(position).getPubDate() will contain date from RSS Feeds. How can i pass this in String in ListProvider.java ??
TwitterFeeds.java
public class TwitterFeeds extends Activity {
/** Called when the activity is first created. */
ListView _rssFeedListView;
List<JSONObject> jobs;
List<RssFeedStructure> rssStr;
private TwitterAdapter _adapter;
TextView textview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
_rssFeedListView = (ListView) findViewById(R.id.rssfeed_listview);
textview = (TextView) findViewById(R.id.loading);
RssFeedTask rssTask = new RssFeedTask();
rssTask.execute();
}
public class RssFeedTask extends AsyncTask<String, Void, String> {
// private String Content;
private ProgressDialog Dialog;
String response = "";
@Override
public void onPreExecute() {
}
@Override
public String doInBackground(String... urls) {
try {
String feed = "http://timesofindia.feedsportal.com/c/33039/f/533944/index.rss";
XmlHandler rh = new XmlHandler();
rssStr = rh.getLatestArticles(feed);
} catch (Exception e) {
}
return response;
}
@Override
public void onPostExecute(String result) {
if (rssStr != null) {
_adapter = new TwitterAdapter(TwitterFeeds.this, rssStr);
_rssFeedListView.setAdapter(_adapter);
textview.setVisibility(View.INVISIBLE);
_rssFeedListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(getBaseContext(), "TwitterFeeds", Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
}
});
}
}
}
}
TwitterAdapter.java
public class TwitterAdapter extends ArrayAdapter<RssFeedStructure> {
List<RssFeedStructure> imageAndTexts1 = null;
Context context;
public static String passtitletowidget;
public TwitterAdapter(Activity activity,List<RssFeedStructure> imageAndTexts) {
super(activity, 0, imageAndTexts);
imageAndTexts1 = imageAndTexts;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
context = getContext();
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 {
passtitletowidget = imageAndTexts1.get(position).getTitle();
Log.d("rssfeed", "imageAndTexts1.get(position).getImgLink() :: " + imageAndTexts1.get(position).getImgLink() + " :: " + imageAndTexts1.get(position).getTitle());
//Underline HashTags -- To identify Mentioned user name; use this "@([A-Za-z0-9_-]+)"
SpannableString hashtagintitle = new SpannableString(imageAndTexts1.get(position).getTitle());
Matcher matcher = Pattern.compile("#([A-Za-z0-9_-]+)").matcher(hashtagintitle);
while (matcher.find())
{
hashtagintitle.setSpan(new ForegroundColorSpan(Color.BLUE), matcher.start(), matcher.end(), 0);
}
textView.setText(hashtagintitle);
timeFeedText.setText(imageAndTexts1.get(position).getPubDate());
//Underline Date
/*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);
}
}
//Share Button
Button Button1= (Button) rowView.findViewById(R.id.sharebutton);
Button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//Share Data
String shareBody = imageAndTexts1.get(position).getDescription();
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
context.startActivity(Intent.createChooser(sharingIntent, shareBody));
//Share Data ends
}
});
//Share Button ends
//OpenWith Button
Button Button2= (Button) rowView.findViewById(R.id.openwith);
Button2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//OpenWith Data
Intent i = new Intent(Intent.ACTION_VIEW);
// We have to set data for our new Intent
i.setData(Uri.parse(imageAndTexts1.get(position).getLink()));
// And start activity with our Intent
context.startActivity(i);
//OpenWith Data ends
}
});
//OpenWith Button ends
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return rowView;
}
}
ListProvider.java
public class ListProvider implements RemoteViewsFactory {
private ArrayList<ListItem> listItemList = new ArrayList<ListItem>();
private Context context = null;
private int appWidgetId;
public ListProvider(Context context, Intent intent) {
this.context = context;
appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
populateListItem();
}
private void populateListItem() {
for (int i = 0; i < 10; i++) {
ListItem listItem = new ListItem();
listItem.heading = "Heading" + i;
listItem.content = i
+ " This is the content of the app widget listview.Nice content though";
listItemList.add(listItem);
}
}
@Override
public int getCount() {
return listItemList.size();
}
@Override
public long getItemId(int position) {
return position;
}
/*
*Similar to getView of Adapter where instead of View
*we return RemoteViews
*
*/
@Override
public RemoteViews getViewAt(int position) {
final RemoteViews remoteView = new RemoteViews(
context.getPackageName(), R.layout.list_row);
ListItem listItem = listItemList.get(position);
remoteView.setTextViewText(R.id.heading, listItem.heading);
remoteView.setTextViewText(R.id.content, listItem.content);
return remoteView;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public void onCreate() {
}
@Override
public void onDataSetChanged() {
}
@Override
public void onDestroy() {
}
}
To support a ListView in an app widget, you will need to create a RemoteViewsFactory as a replacement for your ListAdapter, plus a RemoteViewsService to allow the app widget to talk to that factory. You will then be able to "bind" the ListView to the service, and the app widget framework will then be able to populate the ListView.
This is covered in the documentation, and here is another sample app showing the use of a ListView in an app widget.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With