I'm trying to follow this tutorial http://automateddeveloper.blogspot.com/2011/05/android-rss-reader-20.html
I'm using eclipse and developing for android 2.0
here is the problem
*I cant figure out how to set an onclick listener or something similar to each article
*I'm trying to make it go to the website the article is from onclick
public class Utezone extends ListActivity {
private RssListAdapter adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<JSONObject> jobs = new ArrayList<JSONObject>();
try {
jobs = RssReader.getLatestRssFeed();
} catch (Exception e) {
Log.e("RSS ERROR", "Error loading RSS Feed Stream >> " + e.getMessage() + " //" + e.toString());
}
adapter = new RssListAdapter(this,jobs);
setListAdapter(adapter);
}
}
public class RssListAdapter extends ArrayAdapter<JSONObject> {
public RssListAdapter(Activity activity, List<JSONObject> imageAndTexts) {
super(activity, 0, imageAndTexts);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext();
LayoutInflater inflater = activity.getLayoutInflater();
// Inflate the views from XML
View rowView = inflater.inflate(R.layout.image_text_layout, null);
JSONObject jsonImageText = getItem(position);
//////////////////////////////////////////////////////////////////////////////////////////////////////
//The next section we update at runtime the text - as provided by the JSON from our REST call
////////////////////////////////////////////////////////////////////////////////////////////////////
TextView textView = (TextView) rowView.findViewById(R.id.job_text);
try {
Spanned text = (Spanned)jsonImageText.get("text");
textView.setText(text);
} catch (JSONException e) {
textView.setText("JSON Exception");
}
return rowView;
}
}
public class RssReader {
private final static String BOLD_OPEN = "<B>";
private final static String BOLD_CLOSE = "</B>";
private final static String BREAK = "<BR>";
private final static String ITALIC_OPEN = "<I>";
private final static String ITALIC_CLOSE = "</I>";
private final static String SMALL_OPEN = "<SMALL>";
private final static String SMALL_CLOSE = "</SMALL>";
/**
* This method defines a feed URL and then calles our SAX Handler to read the article list
* from the stream
*
* #return List<JSONObject> - suitable for the List View activity
*/
public static List<JSONObject> getLatestRssFeed(){
String feed = "thefeedurl";
RSSHandler rh = new RSSHandler();
List<Article> articles = rh.getLatestArticles(feed);
Log.e("RSS ERROR", "Number of articles " + articles.size());
return fillData(articles);
}
/**
* This method takes a list of Article objects and converts them in to the
* correct JSON format so the info can be processed by our list view
*
* #param articles - list<Article>
* #return List<JSONObject> - suitable for the List View activity
*/
private static List<JSONObject> fillData(List<Article> articles) {
List<JSONObject> items = new ArrayList<JSONObject>();
for (Article article : articles) {
JSONObject current = new JSONObject();
try {
buildJsonObject(article, current);
} catch (JSONException e) {
Log.e("RSS ERROR", "Error creating JSON Object from RSS feed");
}
items.add(current);
}
return items;
}
/**
* This method takes a single Article Object and converts it in to a single JSON object
* including some additional HTML formating so they can be displayed nicely
*
* #param article
* #param current
* #throws JSONException
*/
private static void buildJsonObject(Article article, JSONObject current) throws JSONException {
String title = article.getTitle();
String description = article.getDescription();
String date = article.getPubDate();
StringBuffer sb = new StringBuffer();
sb.append(BOLD_OPEN).append(title).append(BOLD_CLOSE);
sb.append(BREAK);
sb.append(description);
sb.append(BREAK);
sb.append(SMALL_OPEN).append(ITALIC_OPEN).append(date).append(ITALIC_CLOSE).append(SMALL_CLOSE);
current.put("text", Html.fromHtml(sb.toString()));
}
}
Since the activity extends ListActivity you can simply do:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//do something
}
A onclicklistener is bound to a listview. call getListView() and set it to a ListView ivar. On that ivar implement an OnItemClickListener by calling the method setOnItemClickListener();
ListView lw = getListView();
lw.setOnItemClickListener(new OnItemClickListener({
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {}
}));
Related
I'm having an issue with RecyclerView not refreshing after I do a manual sync of a web service.
The manual sync is triggered by either swiping-down on the list or by tapping on an ActionBar item.
The manual sync uses a Volley Request to retrieve data in a JSON format, the data is parsed and saved to an SQLite database table. The sync datetime is also saved to an SQLite database table and later displayed in the Fragment's ActionBar Subtitle. The Volley Request is kicked of via a WorkManager OneTimeWorkRequest.
The problem being the RecyclerView list is not refreshed. However if I then trigger another manual sync, the sync datatime in the ActionBar Subtitle and RecyclerView contents are updated but with data from the previous manual sync. This becomes clear if I navigate away from the app to the device's Home screen and then navigate back to my app, which now shows the refreshed data from the most recent manual sync.
I have looked at numerous posts around this issue (see below) and whilst I think I have improved my code none of the recommended solutions have resolved the issue.
Recyclerview not call onCreateViewHolder
RecyclerView not calling onCreateViewHolder or onBindView
Recyclerview not call onCreateViewHolder
RecyclerView is not refreshing
get JSON data from web and display using RecyclerView
Recycler View appear blank and doesn't show SQLite data
RecyclerView onClick not working properly?
Why doesn't RecyclerView have onItemClickListener()?
ListView not updating after web service Sync
Other resources looked at:
https://www.mytrendin.com/display-data-recyclerview-using-sqlitecursor-in-android/
https://medium.com/#studymongolian/updating-data-in-an-android-recyclerview-842e56adbfd8
https://www.youtube.com/watch?v=ObU-wCqoo2I
https://www.youtube.com/watch?v=_0C18cbv6UE
So after a number of months trying to fix this issue I am now turning to the StackOverflow community for help.
Fragment
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_warning_list, container, false);
mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.warning_swipe_refresh_layout);
/* Set the Refresh Listener for the Swipe gesture */
mSwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
SyncWarningsScheduler.oneTime();
updateUI();
}
}
);
mWarningRecyclerView = (RecyclerView) view.findViewById(R.id.warning_recycler_view);
/* Set the Toolbar to replace the default ActionBar, which has been hidden */
if (mActivity != null) {
Toolbar toolbar = (Toolbar) mActivity.findViewById(R.id.toolbar_abstract_single_fragment);
mActivity.setSupportActionBar(toolbar);
/* Set the toolbar title */
ActionBar actionbar = mActivity.getSupportActionBar();
if (actionbar != null) {
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setTitle(getString(R.string.warning_list_fragment_toolbar_title));
}
}
updateUI();
return view;
}
#Override
public void onResume() {
super.onResume();
updateUI();
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_warning_list, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.sync:
mSwipeRefreshLayout.setRefreshing(true);
SyncWarningsScheduler.oneTime();
updateUI();
return true;
case R.id.information:
/* Handle the Information Menu Item */
FragmentManager fm = getFragmentManager();
if (fm != null) {
WarningListFragmentTFBInformationDialogFragment dialog = new WarningListFragmentTFBInformationDialogFragment();
dialog.show(fm, TFB_INFO_DIALOG_TAG);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void updateUI() {
WarningList warningList = WarningList.get(mActivity);
List<Warning> warnings = warningList.getWarnings();
if (mWarningAdaptor == null) {
mWarningAdaptor = new WarningAdaptor(warnings);
mWarningRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
mWarningRecyclerView.setAdapter(mWarningAdaptor);
} else {
mWarningAdaptor.setWarnings(warnings);
mWarningAdaptor.notifyDataSetChanged();
}
/* Update the ToolBar sub title to show the latest sync datetime */
updateToolBarSubTitle();
/* If visible, turn off the Swipe Refresh Progress Circle */
if (mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
}
private void updateToolBarSubTitle() {
SyncInformationList syncInformationList = SyncInformationList.get(mContext);
Date syncDate = syncInformationList.getSyncDatetime(ORMSync.getWarningSyncTypeKey());
ActionBar actionBar = mActivity.getSupportActionBar();
if (actionBar != null) {
actionBar.setSubtitle(DatabaseUtilities.formatDateSpecial(syncDate, true));
}
}
private class WarningHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private Warning mWarning;
private TextView mIssueForTextView;
private TextView mDeclarationTextView;
private View mStatusWarningViewLeft;
private View mStatusWarningViewRight;
public WarningHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.list_item_warning, parent, false));
/* Handlers a user press on a Warning */
itemView.setOnClickListener(this);
mIssueForTextView = (TextView) itemView.findViewById(R.id.issueFor_textView);
mDeclarationTextView = (TextView) itemView.findViewById(R.id.declaration_textView);
mStatusWarningViewLeft = (View) itemView.findViewById(R.id.status_warning_left);
mStatusWarningViewRight = (View) itemView.findViewById(R.id.status_warning_right);
}
public void bind(Warning warning) {
mWarning = warning;
String issueForDate;
issueForDate = DatabaseUtilities.formatDateSpecial(mWarning.getIssuedFor(), "d MMM yyyy");
mIssueForTextView.setText(issueForDate);
mDeclarationTextView.setText(mWarning.getTfbDeclaration());
/* Set Declaration text colour */
if (mWarning.isTfbStatus()) {
/* If the day is a TFB set text color to Red */
mIssueForTextView.setTextColor(getResources().getColor(R.color.red));
mDeclarationTextView.setTextColor(getResources().getColor(R.color.red));
}
/* Set left and right status warning colour based on TFB status */
mStatusWarningViewLeft.setBackgroundResource(mWarning.setStatusWarningColor());
mStatusWarningViewRight.setBackgroundResource(mWarning.setStatusWarningColor());
}
#Override
public void onClick(View v) {
/* Process onClick */
Intent intent = WarningPagerActivity.newIntent(mActivity, mWarning.getUID());
startActivity(intent);
}
}
private class WarningAdaptor extends RecyclerView.Adapter<WarningHolder> {
private List<Warning> mWarnings;
public WarningAdaptor(List<Warning> warnings) {
mWarnings = warnings;
}
#NonNull
#Override
public WarningHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(mActivity);
return new WarningHolder(layoutInflater, parent);
}
#Override
public void onBindViewHolder(#NonNull WarningHolder holder, int position) {
/* Bind data */
Warning warning = mWarnings.get(position);
holder.bind(warning);
}
#Override
public int getItemCount() {
return mWarnings.size();
}
public void setWarnings(List<Warning> warnings) {
mWarnings.clear();
mWarnings = warnings;
}
public List<Warning> getWarnings() {
return mWarnings;
}
}
}
WorkManager oneTimeWorkRequest Scheduler
public class SyncWarningsScheduler {
private static final String TAG = "SyncWarningsScheduler";
private static final String ONE_TIME_WORK_REQUEST = "OneTime";
private static final String ONE_TIME_WORK_REQUEST_TAG = TAG + ONE_TIME_WORK_REQUEST;
private static final String ONE_TIME_WORK_REQUEST_TAG_UNIQUE = ONE_TIME_WORK_REQUEST_TAG + "Unique";
/* Getters and Setters */
public static String getOneTimeWorkRequestTagUnique() {
return ONE_TIME_WORK_REQUEST_TAG_UNIQUE;
}
public static void oneTime() {
WorkManager workManager = WorkManager.getInstance();
/* Create a Constraints object that defines when and how the task should run */
Constraints constraints = new Constraints.Builder()
.setRequiresCharging(false)
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
/* Build the Input Data to pass to the Worker */
Data inputData = new Data.Builder()
.putString(SyncWarningsWorker.getWorkRequestTypeKey(), ONE_TIME_WORK_REQUEST)
.build();
/* Build the One Time Work Request */
OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(SyncWarningsWorker.class)
.setConstraints(constraints)
/* Sets the input data for the ListenableWorker */
.setInputData(inputData)
.addTag(ONE_TIME_WORK_REQUEST_TAG)
.build();
workManager.enqueueUniqueWork(ONE_TIME_WORK_REQUEST_TAG_UNIQUE, ExistingWorkPolicy.REPLACE, oneTimeWorkRequest);
}
}
WorkManager Worker
public class SyncWarningsWorker extends Worker {
private static final String TAG = "SyncWarningsWorker";
private Context mContext;
private SQLiteDatabase mDatabase;
private WarningList mWarningList;
private static final String WORK_REQUEST_TYPE_KEY = "warningworkrequesttype";
private static final String SYNC_DATE_TIME_KEY = "warningsyncdatetime";
public SyncWarningsWorker(#NonNull Context context, #NonNull WorkerParameters workerParams) {
super(context, workerParams);
/* Set the Context which must be the Application Context */
mContext = context;
mDatabase = IncidentsDatabaseHelper.get(context).getWritableDatabase();
/* Get a refer to the WarningList Singleton */
mWarningList = WarningList.get(context);
}
/* Getters and Setters */
public static String getWorkRequestTypeKey() {
return WORK_REQUEST_TYPE_KEY;
}
public static String getSyncDateTimeKey() {
return SYNC_DATE_TIME_KEY;
}
#NonNull
#Override
public Result doWork() {
/* Read passed-in argument(s) */
String workRequestType = getInputData().getString(WORK_REQUEST_TYPE_KEY);
LogUtilities.info(TAG, "doWork() - Processing EMV Warnings Work Request Type: " + workRequestType);
try {
downloadWarnings();
Date now = new Date();
long nowMilliSeconds = now.getTime();
now.setTime(nowMilliSeconds);
/* Update the WarningSyncType in SyncInformationList with the Warnings Sync Datetime */
SyncInformationList syncInformationList = SyncInformationList.get(mContext);
syncInformationList.updateSyncDatetime(ORMSync.getWarningSyncTypeKey(), now);
Data syncDateTime = new Data.Builder()
.putLong(SYNC_DATE_TIME_KEY, nowMilliSeconds)
.build();
return Result.success(syncDateTime);
} catch (Exception e){
LogUtilities.error(TAG, "doWork() - Can't download EMV Warnings data.\n\n" + e.toString());
return Result.failure();
}
}
private void downloadWarnings() {
VolleyRequestQueue volleyRequestQueue;
StringRequest request = new StringRequest(Request.Method.GET, JSONWarningsSchema.getTfbFdrJsonEndPoint(), onPostsLoaded, onPostsError);
volleyRequestQueue = VolleyRequestQueue.get(mContext);
volleyRequestQueue.addToVolleyRequestQueue(request);
}
private final Response.Listener<String> onPostsLoaded = new Response.Listener<String>() {
ContentValues contentvalues;
String noData = "NO DATA";
#Override
public void onResponse(String response) {
/* Delete all the Warning records from the SQLite table */
mWarningList.deleteAllWarnings();
try {
JSONObject jsonBody = new JSONObject(response);
/* Within jsonBody is one nested JSON Array */
JSONArray jsonArrayResults = jsonBody.getJSONArray(JSONWarningsSchema.getJsonRootArrayName());
for (int i = 0; i < jsonArrayResults.length(); i++) {
/*
* Within jsonArrayResults are 10 sometimes 9 JSON Objects, the first 5 Objects are for TFB declarations and
* the last 5 (4) Objects are for FDR declarations.
*/
JSONObject warningMetadata = jsonArrayResults.getJSONObject(i);
if (i < 5) {
/*
* The first 5 Objects are for Today and the next 4 days worth of TFB declarations. The TFB declaration in these Objects are
* used to INSERT new records into the warnings table using the issueFor date as the Alternate Primary Key.
* The FDR declarations for each day are defaulted to "NO DATA" to cater for the sometimes missing FDR data on the 5th day, this is
* to avoid null pointer errors when displaying the data in fragment_warning.
*/
Warning warning = new Warning();
String issueForDate = warningMetadata.getString(JSONWarningsSchema.Keys.getIssueFor());
warning.setIssuedFor(JSONUtilities.stringToDate(issueForDate, JSONWarningsSchema.getJsonIssueForDateFormat()));
String status = warningMetadata.getString(JSONWarningsSchema.Keys.getStatus());
warning.setTfbStatus(JSONUtilities.stringToBoolean(status));
warning.setTfbDeclaration(warningMetadata.getString(JSONWarningsSchema.Keys.getDeclaration()));
/*
* Within the warningMetadata JSONObject is a JSONArray called declareList. Need to get the Array and
* iterate through the Array to extract the TFB warning for each District for this day.
* We know the exact number of JSONObjects in the declareList Array (ie an Object for each District).
*/
JSONArray jsonArrayTFBDeclareList = warningMetadata.getJSONArray(JSONWarningsSchema.getJsonDeclareListArrayName());
/* Iterate through the TFB declareList Array */
for (int j = 0; j < jsonArrayTFBDeclareList.length(); j++) {
/* Get the JSON Object within the jsonArrayDeclareList Array */
JSONObject declareListMetadata = jsonArrayTFBDeclareList.getJSONObject(j);
/* Get the name and status pairs from the declareListMetadata Object */
String name = declareListMetadata.getString(JSONWarningsSchema.Keys.getDeclareListName());
String declareListStatus = declareListMetadata.getString(JSONWarningsSchema.Keys.getDeclareListStatus());
switch (name) {
case "Mallee":
warning.setTfbMallee(declareListStatus);
warning.setFdrMallee(noData);
break;
case "Wimmera":
warning.setTfbWimmera(declareListStatus);
warning.setFdrWimmera(noData);
break;
case "South West":
warning.setTfbSouthWest(declareListStatus);
warning.setFdrSouthWest(noData);
break;
case "Northern Country":
warning.setTfbNorthernCountry(declareListStatus);
warning.setFdrNorthernCountry(noData);
break;
case "North Central":
warning.setTfbNorthCentral(declareListStatus);
warning.setFdrNorthCentral(noData);
break;
case "Central":
warning.setTfbCentral(declareListStatus);
warning.setFdrCentral(noData);
break;
case "North East":
warning.setTfbNorthEast(declareListStatus);
warning.setFdrNorthEast(noData);
break;
case "West and South Gippsland":
warning.setTfbWestAndSouthGippsland(declareListStatus);
warning.setFdrWestAndSouthGippsland(noData);
break;
case "East Gippsland":
warning.setTfbEastGippsland(declareListStatus);
warning.setFdrEastGippsland(noData);
break;
default:
break;
}
}
contentvalues = ContentValueUtilities.getWarningListContentValues(warning, true);
mDatabase.beginTransaction();
try {
mDatabase.insert(ORMWarnings.getTableName(), null, contentvalues);
mDatabase.setTransactionSuccessful();
} catch (SQLiteException e) {
LogUtilities.error(TAG, "onPostsLoaded > onResponse - ERROR Inserting record into the '" + ORMWarnings.getTableName() + "' Table.\n\n" + e.toString());
} finally {
mDatabase.endTransaction();
}
} else {
/*
* The last 5 or sometimes 4 Objects are for Today and the next 4 days worth of FDR declarations. The FDR declarations
* in these Objects are used to UPDATE FDR attributes in the warnings table using the issueFor date to find the existing warnings
* record.
*/
String issueForFDR = warningMetadata.getString(JSONWarningsSchema.Keys.getIssueFor());
/* Ensure the retrieved issueFor date string is converted consistently */
Date issueForFDRDate = JSONUtilities.stringToDate(issueForFDR, JSONWarningsSchema.getJsonIssueForDateFormat());
/* Find the record in the warnings table by using the issueForFDRDate date. */
Warning warningExists = mWarningList.getWarning(issueForFDRDate);
/* Make sure a warning record has been returned */
if (warningExists != null) {
String issueAtDate = warningMetadata.getString(JSONWarningsSchema.Keys.getIssueAt());
warningExists.setFdrIssuedAt(JSONUtilities.stringToDate(issueAtDate, JSONWarningsSchema.getJsonIssueAtDateFormat()));
/*
* Within the warningMetadata JSONObject is a JSONArray called declareList. Need to get the Array and
* iterate through the Array to extract the FDR warning for each District for this day.
* We know the exact number of JSONObjects in the declareList Array (ie an Object for each District).
*/
JSONArray jsonArrayFDRDeclareList = warningMetadata.getJSONArray(JSONWarningsSchema.getJsonDeclareListArrayName());
/* Iterate through the FDR declareList Array */
for (int z = 0; z < jsonArrayFDRDeclareList.length(); z++) {
/* Get the JSON Object within the jsonArrayFDRDeclareList Array */
JSONObject declareListMetadataFDR = jsonArrayFDRDeclareList.getJSONObject(z);
/* Get the name and status pairs from the declareListMetadataFDR Object */
String nameFDR = declareListMetadataFDR.getString(JSONWarningsSchema.Keys.getDeclareListName());
String declareListStatusFDR = declareListMetadataFDR.getString(JSONWarningsSchema.Keys.getDeclareListStatus());
switch (nameFDR) {
case "Mallee":
warningExists.setFdrMallee(declareListStatusFDR);
break;
case "Wimmera":
warningExists.setFdrWimmera(declareListStatusFDR);
break;
case "South West":
warningExists.setFdrSouthWest(declareListStatusFDR);
break;
case "Northern Country":
warningExists.setFdrNorthernCountry(declareListStatusFDR);
break;
case "North Central":
warningExists.setFdrNorthCentral(declareListStatusFDR);
break;
case "Central":
warningExists.setFdrCentral(declareListStatusFDR);
break;
case "North East":
warningExists.setFdrNorthEast(declareListStatusFDR);
break;
case "West and South Gippsland":
warningExists.setFdrWestAndSouthGippsland(declareListStatusFDR);
break;
case "East Gippsland":
warningExists.setFdrEastGippsland(declareListStatusFDR);
break;
default:
break;
}
}
contentvalues = ContentValueUtilities.getWarningListContentValues(warningExists, false);
mDatabase.beginTransaction();
try {
mDatabase.update(ORMWarnings.getTableName(), contentvalues, ORMWarnings.getUUIDColumn() + " = ?", new String[] {warningExists.getUID().toString()});
mDatabase.setTransactionSuccessful();
} catch (SQLiteException e) {
LogUtilities.error(TAG, "onPostsLoaded > onResponse - ERROR Updating record in the '" + ORMWarnings.getTableName() + "' Table.\n\n" + e.toString());
} finally {
mDatabase.endTransaction();
}
} else {
/* Something went wrong can't find warning record using the issueForFDRDate date */
LogUtilities.wtf(TAG, "onPostsLoaded > onResponse - " + issueForFDRDate.toString() + " warning record not found.\n\n");
}
}
}
} catch (JSONException e) {
LogUtilities.error(TAG, "onPostsLoaded > onResponse - Failed to Parse JSON body.\n\n" + e.toString());
}
}
};
private final Response.ErrorListener onPostsError = new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
LogUtilities.error(TAG, "onPostsError > onErrorResponse - Failed to download JSON body.\n\n" + error.toString());
}
};
}
Use this in your Adapter
public void setWarnings(List<Warning> warnings) {
mWarnings.clear();
mWarnings = warnings;
notifyDataSetChanged();
}
I think you to updateUI(); method call after successfully api call because when api call it's working in background.
or
you can set
sleep(5000)
updateUI();
About project.
I am making an app, which takes data from website and shows it.
Now, I am working on the ListFragment, which is responsible for getting titles, dates, authors and images, from the list of last 6 posts, then putting them in the storage, and from storage to the RecyclerView.
click to see the website
Everything works fine, when I am running ListFragment. But when i try to refresh my RecyclerView with my SwipeRefreshLayout it refreshes only String data, like titles, dates and authors. Refreshing images in RecyclerView istn't working.
For example.
There is one new post on the website. I run my ListFragment without Internet connection, then turn Internet connection on and refresh RecyclerView swiping it down. Only String values are updated in RecyclerView items in a proper way, Images stay in the same places, despite there were downloaded new ones and put into the storage successfully.
How can I succesfully refresh images in my RecyclerView?
Thanks for any help!
onCreateView() method of my ListFragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/* Inflating View */
view = inflater.inflate(R.layout.fragment_list, container, false);
ImageView banerImageView = view.findViewById(R.id.newsyBanerImageView);
/* Enabling RecyclerView, and setting adapter for it */
recyclerView = view.findViewById(R.id.recyclerView);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
/* SwipeRefreshLayout varriable for recyclerView refreshing operations */
swipeRefreshLayout = view.findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(this);
/* Executing methods in TakeDataFromWebsite class. It dowloads data to save it to
storage, then read it and put in UserInterface, in another Thread */
new TakeDataFromWebSite().execute();
swipeRefreshLayout.setRefreshing(true);
/* enabling prograssbar */
progressBar = view.findViewById(R.id.progressBar);
return view;
}
onRefresh() method of my SwipeRefreshLayout
#Override
public void onRefresh() {
new TakeDataFromWebSite().execute();
}
TakeDataFromWebsite class
public class TakeDataFromWebSite extends AsyncTask<Void, Void, Void> {
//
MyRecyclerAdapter recyclerAdapterInside = new MyRecyclerAdapter();
/* In these ArrayLists is put Content from website, and saved to files, contentSources
are adresses of articles contents */
private ArrayList<String> titlesFromWebsite = new ArrayList<>();
private ArrayList<String> datesFromWebsite = new ArrayList<>();
private ArrayList<String> authorsFromWebsite = new ArrayList<>();
private ArrayList<String> imagesSourcesFromWebsite = new ArrayList<>();
private ArrayList<String> contentSourcesFromWebsite = new ArrayList<>();
//
private ArrayList<RequestCreator> imagesFromWebsite = new ArrayList<>();
/* In these arraylists is put content, which is read from storage */
private ArrayList<String> titlesFromStorage = new ArrayList<>();
private ArrayList<String> datesFromStorage = new ArrayList<>();
private ArrayList<String> authorsFromStorage = new ArrayList<>();
private ArrayList<String> contentSourcesFromStorage = new ArrayList<>();
//
private ArrayList<File> imagesFromStorage = new ArrayList<>();
/* Number of downloaded divs */
int elementsCountFromWebsite;
/* It's used to do not always check if website is available in another thread */
boolean isUrlReachable = false;
/* Method in new Thread */
#Override
protected Void doInBackground(Void... params) {
if (isURLReachable(mContext, webSiteAddress)) {
isUrlReachable = true;
Document doc = new Document("doc");
try {
/* Getting whole document */
doc = Jsoup.connect(webSiteAddress).get();
} catch (Exception e) {
e.printStackTrace();
Log.e("ListFragment", "Doc from site getting Exception.");
}
/* Getting proper divs and spans */
Elements postThumbDivs = doc.select("div[class=post-thumb]");
Elements dates = doc.select("span[class=posted-on]");
Elements authors = doc.select("span[class=author vcard]");
/* Setting variable to number of post divs downloaded */
elementsCountFromWebsite = postThumbDivs.size();
/* Variables for iteration */
Element postThumbDiv;
Element date;
Element author;
for (int i = 0; i < elementsCountFromWebsite; i++) {
/* Getting titles and adding them to list */
postThumbDiv = postThumbDivs.get(i).select("a").first();
titlesFromWebsite.add(postThumbDiv.attr("title"));
/* Getting sources od articles contents and adding them to list */
postThumbDiv = postThumbDivs.get(i).select("a").first();
contentSourcesFromWebsite.add(postThumbDiv.attr("href"));
/* Getting images sources and adding them to list */
postThumbDiv = postThumbDivs.get(i).select("img").get(0);
imagesSourcesFromWebsite.add(postThumbDiv.attr("src"));
/* Adding picasso requestCreator to the arrayList, which is used to write
data in the storage later * */
imagesFromWebsite.add(Picasso.get().load(postThumbDiv.attr("src")));
/* Getting dates and adding them to list */
date = dates.get(i).getElementsByTag("time").first();
datesFromWebsite.add(date.text());
/* Getting authors and adding them to list */
author = authors.get(i);
authorsFromWebsite.add(author.text());
}
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
/* If website is available write data to storage */
if (isUrlReachable){
write(mContext, titlesFromWebsite, fileName + "Titles");
write(mContext, datesFromWebsite, fileName + "Dates");
write(mContext, authorsFromWebsite, fileName + "Authors");
write(mContext, imagesSourcesFromWebsite, fileName + "ImagesSources");
write(mContext, contentSourcesFromWebsite, fileName + "ContentSources");
for(int i = 0; i < elementsCountFromWebsite; i++){
imagesFromWebsite.get(i).into
(picassoImageTarget(mContext, imagesDirectoryName, Integer
.toString(i) + ".png"));
}
} else{
Snackbar.make(view, getText(R.string.brak_polaczenia_z_serwerem), Snackbar
.LENGTH_LONG).show();
}
/* Reading data from storage files. These arraylists are later sent to an adapter,
whch uses them as a source of content */
titlesFromStorage = readStringArraylist(mContext, fileName + "Titles");
datesFromStorage = readStringArraylist(mContext, fileName + "Dates");
authorsFromStorage = readStringArraylist(mContext, fileName + "Authors");
contentSourcesFromStorage = readStringArraylist(mContext, fileName + "ContentSources");
/* Number of elements taken from storage used while reading data from storage */
int itemsCountFromStorage = titlesFromStorage.size();
/* Getting Images from storage into file and adding to the files arraylist */
ContextWrapper contextWrapper = new ContextWrapper(mContext);
File directory = contextWrapper.getDir(imagesDirectoryName, Context.MODE_PRIVATE);
for(int i = 0; i < itemsCountFromStorage; i++){
File myImageFile = new File(directory, Integer.toString(i) + ".png");
imagesFromStorage.add(myImageFile);
}
/* Setting FragmentManager to the recyclerAdapter */
recyclerAdapterInside.setFragmentManager(getFragmentManager());
/* Sending String Arraylist to an adapter */
recyclerAdapterInside.setTitles(titlesFromStorage);
recyclerAdapterInside.setDates(datesFromStorage);
recyclerAdapterInside.setAuthors(authorsFromStorage);
recyclerAdapterInside.setContentSources(contentSourcesFromStorage);
/* Sending File Arraylist to an adapter */
recyclerAdapterInside.setImages(imagesFromStorage);
/* Context and directory sent from this fragment to an adapter. They are used for
reading images from storage */
recyclerAdapterInside.setmContext(mContext);
recyclerAdapterInside.setImagesDirectoryName(imagesDirectoryName);
/* Setting an adapter to the recyclerView. Not in CreateView() method, because it is
need to be done after loading all data from storage*/
recyclerAdapterInside.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
recyclerView.setAdapter(recyclerAdapterInside);
}
}
My RecyclerView.Adapter class
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {
/* rraylists used to provide content */
private ArrayList<String> titles = new ArrayList<>();
private ArrayList<String> dates = new ArrayList<>();
private ArrayList<String> authors = new ArrayList<>();
private ArrayList<String> contentSources = new ArrayList<>();
/* Arraylist used, which i set with images from storage */
private ArrayList<File> images = new ArrayList<>();
/* Fragment with content of article and manager from */
private ContentFragment contentFragment;
private FragmentManager fragmentManager;
/* Context and imagesDirectoryName from ListFragment */
private Context mContext;
private String imagesDirectoryName;
class ViewHolder extends RecyclerView.ViewHolder{
public TextView itemTitleTextView;
public TextView itemDateTextView;
public TextView itemAuthorTextView;
public ImageView itemImageView;
public ImageView itemDateImageView;
public ImageView itemAuthorImageView;
public ViewHolder(View itemView){
super(itemView);
itemTitleTextView = itemView.findViewById(R.id.itemTitleTextView);
itemDateTextView = itemView.findViewById(R.id.itemDateTextView);
itemAuthorTextView = itemView.findViewById(R.id.itemAuthorTextView);
itemImageView = itemView.findViewById(R.id.itemImageView);
itemDateImageView = itemView.findViewById(R.id.itemDateImageView);
itemAuthorImageView = itemView.findViewById(R.id.itemAuthorImageView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
contentFragment = new ContentFragment();
/* Setting contentFragment attributes to proper values */
contentFragment.setWebSiteAddress(contentSources.get(position));
contentFragment.setItemIndex(position);
contentFragment.setDate(dates.get(position));
contentFragment.setAuthor(authors.get(position));
contentFragment.setTitle(titles.get(position));
contentFragment.setImagesDirectoryName(imagesDirectoryName);
/* Fragment transaction process */
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.menuFragment, contentFragment);
transaction.addToBackStack("contentFragment");
transaction.commit();
/* Test */
Snackbar.make(v, "Click detected on item " + position, Snackbar.LENGTH_LONG)
.setAction("Action",null).show();
}
});
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i){
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout,
viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i){
viewHolder.itemTitleTextView.setText(titles.get(i));
viewHolder.itemDateTextView.setText(dates.get(i));
viewHolder.itemAuthorTextView.setText(authors.get(i));
/* Process of reading images from arraylist and putting them to ImageViews */
Picasso.get().load(images.get(i)).into(viewHolder.itemImageView);
}
#Override
public int getItemCount(){
return titles.size();
}
public void setTitles(ArrayList<String> titles){
this.titles = titles;
}
public void setDates(ArrayList<String> dates){
this.dates = dates;
}
public void setAuthors(ArrayList<String> authors){
this.authors = authors;
}
public void setContentSources(ArrayList<String> contentSources){
this.contentSources = contentSources;
}
public void setFragmentManager(FragmentManager fragmentManager){
this.fragmentManager = fragmentManager;
}
public void setmContext(Context mContext){
this.mContext = mContext;
}
public void setImagesDirectoryName(String imagesDirectoryName) {
this.imagesDirectoryName = imagesDirectoryName;
}
public void setImages(ArrayList<File> images){
this.images = images;
}
}
You're notifying recyclerAdapterInside before providing it to recyclerview :
recyclerAdapterInside.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
recyclerView.setAdapter(recyclerAdapterInside);
Just change that sequence inside your onPostExecute(Void aVoid) of your AsyncTask :
swipeRefreshLayout.setRefreshing(false);
recyclerView.setAdapter(recyclerAdapterInside); // Set adapter first.
recyclerAdapterInside.notifyDataSetChanged(); // Then notify for data change.
Note: Never try to update UI (i.e. Activity/Fragment) from
AsyncTask, it may throw NullPointerException for UI view objects. Use Interface to communicate between UI and
AsyncTask.
I am trying to populate data from my main activity using the adapter below. When i run the activity the screen remains blanked. I believe it has to do with the ArrayList which is null perhaps. Can someone tell me why my data is not being displayed. am on this bug for three days now :/
public class CopyOfSecondWheelAdapter extends AbstractWheelTextAdapter {
ArrayList<convertor_pst> PostList = new ArrayList<convertor_pst>();
public ImageLoader imageLoader;
Convertor main;
public CopyOfSecondWheelAdapter(Context context) {
super(context, R.layout.count_layout, NO_RESOURCE);
setItemTextResource(R.id.country_name);
}
#Override
public View getItem(int index, View cachedView, ViewGroup parent) {
View view = super.getItem(index, cachedView, parent);
ImageView img = (ImageView) view.findViewById(R.id.flag);
imageLoader.DisplayImage(PostList.get(index).getDevise(), img);
System.out.println("get item count:"+getItemsCount() );
TextView text = (TextView)view.findViewById(R.id.lib);
text.setText(PostList.get(index).getQuotite());
return view;
}
#Override
public int getItemsCount() {
return PostList.toArray().length;
}
#Override
protected CharSequence getItemText(int index) {
return PostList.get(index).getDevise().toString();
}
}
UPDATE:
In my Main class i have already an
ArrayList<convertor_pst> PostList = new ArrayList<convertor_pst>();
which is populated.
Here is my main class that is my convertor.class
ArrayList<convertor_pst> PostList = new ArrayList<convertor_pst>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.convertor);
context = this;
text_devise_two = (TextView)findViewById(R.id.text_spacetwo);
final WheelView country = (WheelView) findViewById(R.id.country);
country.setVisibleItems(10);
country.setViewAdapter(new FirstWheelAdapter(this));
edt_validate = (EditText)findViewById(R.id.edt_validate);
current_type_loc = (TextView)findViewById(R.id.current_type_loc);
refresh_header= (TextView)findViewById(R.id.refresh_header);
//set current time
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy");
String formattedDate = df.format(c.getTime());
refresh_header.setText(getResources().getString(R.string.mise_a_jour)+" "+formattedDate);
image_one = (ImageView)findViewById(R.id.image_one);
image_two = (ImageView)findViewById(R.id.image_two);
final WheelView currency = (WheelView) findViewById(R.id.currency);
currency.setVisibleItems(10);
currency.setViewAdapter(new CopyOfSecondWheelAdapter(this));
country.addChangingListener(new OnWheelChangedListener() {
#Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
if (!scrolling) {
}
}
});
country.addScrollingListener( new OnWheelScrollListener() {
#Override
public void onScrollingStarted(WheelView wheel) {
scrolling = true;
}
#Override
public void onScrollingFinished(WheelView wheel) {
scrolling = false;
//1.
wheelSelector(country.getCurrentItem());
}
});
currency.addScrollingListener( new OnWheelScrollListener() {
#Override
public void onScrollingStarted(WheelView wheel) {
scrolling = true;
}
#Override
public void onScrollingFinished(WheelView wheel) {
scrolling = false;
//1.
secondWheel(currency.getCurrentItem());
}
});
country.setCurrentItem(1);
currency.setCurrentItem(3);
new loadingTask().execute();
}
/*1. Change currency */
public void wheelSelector (int id){
if (id==0){
current_type_loc.setText("EUR");
image_one.setBackgroundResource(R.drawable.eur);
}else {
current_type_loc.setText("USD");
image_one.setBackgroundResource(R.drawable.usd);
}
}
class loadingTask extends AsyncTask<Void, Void,Void> {
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
pd = ProgressDialog.show(Convertor.this, "", "Chargement en cours..", true);
super.onPreExecute();
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
doc = Jsoup.parse(getxml,"", Parser.xmlParser());
taux = doc.select("taux");
for (int i = 0; i < taux.size(); i++) {
PostList.add(new convertor_pst(taux.get(i).getElementsByTag("devise").text().toString(),
taux.get(i).getElementsByTag("dateCours").text().toString(),
taux.get(i).getElementsByTag("libelle").text().toString(),
taux.get(i).getElementsByTag("quotite").text().toString(),
taux.get(i).getElementsByTag("fixing").text().toString()));
}
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
envelope =
"soap content"
String requestEnvelope=String.format(envelope, "28-03-2013","true");
getxml = Util.CallWebService(URL,SOAP_ACTION,requestEnvelope);
System.out.println(getxml);
return null;
}
}
public void secondWheel(int index){
text_devise_two.setText(PostList.get(index).getDevise());
edt_validate.setText(" "+PostList.get(index).getFixing());
}
/*
*
* (non-Javadoc)
* #see android.app.Activity#onPause()
* check if activity go to background
*/
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (Util.isApplicationBroughtToBackground(Convertor.this)==true){
startActivity(new Intent(Convertor.this,Compte.class));
}
}
}
This is the original wheel adapter class
public class CopyOfSecondWheelAdapter extends AbstractWheelTextAdapter {
ArrayList<convertor_pst> PostList;
public ImageLoader imageLoader;
// Countries names
private String countries[] =
new String[] {"EUR", "USD","EUR", "USD","EUR", "USD","EUR", "USD","EUR", "USD","EUR", "USD"};
// Countries flags
private int flags[] = new int[] {R.drawable.eur, R.drawable.usd,R.drawable.eur, R.drawable.usd,R.drawable.eur, R.drawable.usd,R.drawable.eur, R.drawable.usd,R.drawable.eur, R.drawable.usd,R.drawable.eur, R.drawable.usd};
/**
* Constructor
*/
Convertor main;
public CopyOfSecondWheelAdapter(Context context) {
super(context, R.layout.count_layout, NO_RESOURCE);
setItemTextResource(R.id.country_name);
}
#Override
public View getItem(int index, View cachedView, ViewGroup parent) {
View view = super.getItem(index, cachedView, parent);
ImageView img = (ImageView) view.findViewById(R.id.flag);
img.setImageResource(flags[index]);
TextView text = (TextView)view.findViewById(R.id.lib);
text.setText("code");
return view;
}
#Override
public int getItemsCount() {
return countries.length;
}
#Override
protected CharSequence getItemText(int index) {
return countries[index];
}
}
As far as I understand
currency.setViewAdapter(new CopyOfSecondWheelAdapter(this));
this line creates the adapter, but you fill it up at this line :
new loadingTask().execute();
which is after, so you must call
yourAdapter.notifyDataSetChanged();
on your adapter to update the data.
Android Developer Help says
notifyDataSetChanged()
Notifies the attached observers that the
underlying data has been changed and any View reflecting the data set
should refresh itself.
So in your case you must
create an adapter (yourAdapter = new CopyOfSecondWheelAdapter ....)
assign it with the setViewAdater (WheelView.setViewAdapter(yourAdapter))
in the "postExecute" of your async task, do a call with yourAdapter.notifyDataSetChanged();
By the way, I am not sure to understand what you are doing, but in case you need to have a set of data displayed at two different locations, you don't need to duplicate (create a copy). The two list display can share the same adapter.
UPDATE
You have done an update to your question and I answer to that update :
In the original adapter the countries are not loaded in the async task. So when you assign the adapter, the display show the correct values because they are present in the adapter at the moment you assign it.
In your case, you load the values in the async task. When you create the adapter it is empty and you assign it empty, so the display shows an empty list. You should notify your display of the data change.
So in the original, no need to notify as the data is the correct one at the time of assignment. In your case you have to implement a notifyDataSetChanged(). Or change the type of adapter you are extending.
If I see it correctly, you have 2 times a variable name PostList which confuses you. One is created in your activity and one in your adapter and ass you call add() to the variable of your activity, the list in your adapter never gets the items.
Create a setter for the list in your adapter and set the list in your onPostExecute().
I'm getting a json exception when I try to run my code.
The problem happens on the onclick listener. I'm trying to have the application go to the article of the rss feed on click
Here is the main activity
public class Blocku extends ListActivity {
private RssListAdapter adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<JSONObject> jobs = new ArrayList<JSONObject>();
try {
jobs = BlockuReader.getLatestRssFeed();
} catch (Exception e) {
Log.e("RSS ERROR", "Error loading RSS Feed Stream >> " + e.getMessage() + " //" + e.toString());
}
adapter = new RssListAdapter(this,jobs);
setListAdapter(adapter);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String link = null;
try {
String url = Article.getUrl().toString();
link = adapter.getItem(position).getString(url).toString();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(link));
startActivity(i);
} catch (JSONException e) {
Context context = getApplicationContext();
CharSequence text = "error";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
e.printStackTrace();
}
}
}
here is the adapter:
public class RssListAdapter extends ArrayAdapter<JSONObject> {
public RssListAdapter(Activity activity, List<JSONObject> imageAndTexts) {
super(activity, 0, imageAndTexts);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext();
LayoutInflater inflater = activity.getLayoutInflater();
// Inflate the views from XML
View rowView = inflater.inflate(R.layout.image_text_layout, null);
JSONObject jsonImageText = getItem(position);
//////////////////////////////////////////////////////////////////////////////////////////////////////
//The next section we update at runtime the text - as provided by the JSON from our REST call
////////////////////////////////////////////////////////////////////////////////////////////////////
TextView textView = (TextView) rowView.findViewById(R.id.job_text);
try {
Spanned text = (Spanned)jsonImageText.get("text");
textView.setText(text);
} catch (JSONException e) {
textView.setText("JSON Exception");
}
return rowView;
}
}
and the article class
public class Article {
private long articleId;
private long feedId;
private String title;
private String description;
private String pubDate;
private static URL url;
private String encodedContent;
private static String link;
/**
* #return the articleId
*/
public long getArticleId() {
return articleId;
}
/**
* #param articleId the articleId to set
*/
public void setArticleId(long articleId) {
this.articleId = articleId;
}
/**
* #return the feedId
*/
public long getFeedId() {
return feedId;
}
/**
* #param feedId the feedId to set
*/
public void setFeedId(long feedId) {
this.feedId = feedId;
}
/**
* #return the title
*/
public String getTitle() {
return title;
}
/**
* #param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* #return the url
*/
public static URL getUrl() {
return url;
}
/**
* #param url the url to set
*/
public void setUrl(URL url) {
Article.url = url;
}
/**
* #param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* #return the description
*/
public String getDescription() {
return description;
}
/**
* #param pubDate the pubDate to set
*/
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
/**
* #return the pubDate
*/
public String getPubDate() {
return pubDate;
}
/**
* #param encodedContent the encodedContent to set
*/
public void setEncodedContent(String encodedContent) {
this.encodedContent = encodedContent;
}
/**
* #return the encodedContent
*/
public String getEncodedContent() {
return encodedContent;
}
}
JSONException is usually thrown when there are issues with parsing. Check how you are parsing the JSON. this says that you are trying to get a value for a key named "then it lists the site i want"
I am new to programming so please go easy on me, I have been messing around with a simple RSS Reader, trying to get the link to the artice to open in a webview when the user clicks on the article.
I have found the string that controls and stores the link but when I try to print the link in the toast the link appears but with the whole article publishing date ect... how can I get the link to print on it own and what commands do I need to use to pass the link to the webview once I have isolated it, here is some of the code I have
RSSActivity
public class RssActivity extends ListActivity {
private RssListAdapter adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<JSONObject> jobs = new ArrayList<JSONObject>();
try {
jobs = RssReader.getLatestRssFeed();
} catch (Exception e) {
Log.e("RSS ERROR", "Error loading RSS Feed Stream >> " + e.getMessage() + " //" + e.toString());
}
adapter = new RssListAdapter(this,jobs);
setListAdapter(adapter);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
adapter.getItem(position).toString();
String link = o.toString();
Toast.makeText(this, "You selected: " + link, Toast.LENGTH_LONG)
.show();
}
}
Article.class
public class Article {
private long articleId;
private long feedId;
private String title;
private String description;
private String pubDate;
private URL url;
private String encodedContent;
private String link;
public void setArticleId(long articleId) {
this.articleId = articleId;
}
/**
* #return the feedId
*/
public long getFeedId() {
return feedId;
}
/**
* #param feedId the feedId to set
*/
public void setFeedId(long feedId) {
this.feedId = feedId;
}
public String getLink() {
return link;
}
/**
* #param title the title to set
*/
public void setLink(String link) {
this.link = link;
}
/**
* #return the title
*/
public String getTitle() {
return title;
}
/**
* #param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* #return the url
*/
public URL getUrl() {
return url;
}
/**
* #param url the url to set
*/
public void setUrl(URL url) {
this.url = url;
}
/**
* #param description the description to set
*/
public void setDescription(String description) {
this.description = description;
//parse description for any image or video links
if (description.contains("<img ")){
String img = description.substring(description.indexOf("<img "));
String cleanUp = img.substring(0, img.indexOf(">")+1);
int indexOf = img.indexOf("'");
if (indexOf==-1){
}
this.description = this.description.replace(cleanUp, "");
}
}
/**
* #return the description
*/
public String getDescription() {
return description;
}
/**
* #param pubDate the pubDate to set
*/
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
/**
* #return the pubDate
*/
public String getPubDate() {
return pubDate;
}
/**
* #param encodedContent the encodedContent to set
*/
public void setEncodedContent(String encodedContent) {
this.encodedContent = encodedContent;
}
/**
* #return the encodedContent
*/
public String getEncodedContent() {
return encodedContent;
}
}
RSS Handler
public class RSSHandler extends DefaultHandler {
// Feed and Article objects to use for temporary storage
private Article currentArticle = new Article();
private List<Article> articleList = new ArrayList<Article>();
// Number of articles added so far
private int articlesAdded = 0;
// Number of articles to download
private static final int ARTICLES_LIMIT = 15;
//Current characters being accumulated
StringBuffer chars = new StringBuffer();
public void startElement(String uri, String localName, String qName, Attributes atts) {
chars = new StringBuffer();
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if (localName.equalsIgnoreCase("title"))
{
Log.d("LOGGING RSS XML", "Setting article title: " + chars.toString());
currentArticle.setTitle(chars.toString());
}
else if (localName.equalsIgnoreCase("description"))
{
Log.d("LOGGING RSS XML", "Setting article description: " + chars.toString());
currentArticle.setDescription(chars.toString());
}
else if (localName.equalsIgnoreCase("pubDate"))
{
Log.d("LOGGING RSS XML", "Setting article published date: " + chars.toString());
currentArticle.setPubDate(chars.toString());
}
else if (localName.equalsIgnoreCase("encoded"))
{
Log.d("LOGGING RSS XML", "Setting article content: " + chars.toString());
currentArticle.setEncodedContent(chars.toString());
}
else if (localName.equalsIgnoreCase("item"))
{
}
else if (localName.equalsIgnoreCase("link"))
{
Log.d("LOGGING RSS XML", "Setting article link: " + chars.toString());
currentArticle.setLink(chars.toString());
try {
Log.d("LOGGING RSS XML", "Setting article link url: " + chars.toString());
currentArticle.setUrl(new URL(chars.toString()));
} catch (MalformedURLException e) {
Log.e("RSA Error", e.getMessage());
}
}
// Check if looking for article, and if article is complete
if (localName.equalsIgnoreCase("item")) {
articleList.add(currentArticle);
currentArticle = new Article();
// Lets check if we've hit our limit on number of articles
articlesAdded++;
if (articlesAdded >= ARTICLES_LIMIT)
{
throw new SAXException();
}
}
}
public void characters(char ch[], int start, int length) {
chars.append(new String(ch, start, length));
}
public List<Article> getLatestArticles(String feedUrl) {
URL url = null;
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
url = new URL(feedUrl);
xr.setContentHandler(this);
xr.parse(new InputSource(url.openStream()));
} catch (IOException e) {
Log.e("RSS Handler IO", e.getMessage() + " >> " + e.toString());
} catch (SAXException e) {
Log.e("RSS Handler SAX", e.toString());
} catch (ParserConfigurationException e) {
Log.e("RSS Handler Parser Config", e.toString());
}
return articleList;
}
}
public class RssReader {
private final static String BOLD_OPEN = "<B>";
private final static String BOLD_CLOSE = "</B>";
private final static String BREAK = "<BR>";
private final static String ITALIC_OPEN = "<I>";
private final static String ITALIC_CLOSE = "</I>";
private final static String SMALL_OPEN = "<SMALL>";
private final static String SMALL_CLOSE = "</SMALL>";
private final static String WEB_LINK = "<A>";
private final static String WEB_CLOSE = "<A/";
public static List<JSONObject> getLatestRssFeed(){
String feed = "http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/eng_prem/rss.xml";
RSSHandler rh = new RSSHandler();
List<Article> articles = rh.getLatestArticles(feed);
Log.e("RSS ERROR", "Number of articles " + articles.size());
return fillData(articles);
}
private static List<JSONObject> fillData(List<Article> articles) {
List<JSONObject> items = new ArrayList<JSONObject>();
for (Article article : articles) {
JSONObject current = new JSONObject();
try {
buildJsonObject(article, current);
} catch (JSONException e) {
Log.e("RSS ERROR", "Error creating JSON Object from RSS feed");
}
items.add(current);
}
return items;
}
private static void buildJsonObject(Article article, JSONObject current) throws JSONException {
String link = article.getLink();
String title = article.getTitle();
String description = article.getDescription();
String date = article.getPubDate();
StringBuffer sb = new StringBuffer();
sb.append(BOLD_OPEN).append(title).append(BOLD_CLOSE);
sb.append(BREAK);
sb.append(description);
sb.append(BREAK);
sb.append(SMALL_OPEN).append(ITALIC_OPEN).append(date).append(ITALIC_CLOSE).append(SMALL_CLOSE);
sb.append(BREAK);
sb.append(BREAK);
sb.append(BOLD_OPEN).append(WEB_LINK).append(link).append(BOLD_CLOSE).append(WEB_CLOSE);
current.put("link", link);
current.put("text", Html.fromHtml(sb.toString()));
}
}
RssListAdapter
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class RssListAdapter extends ArrayAdapter<JSONObject> {
public RssListAdapter(Activity activity, List<JSONObject> imageAndTexts) {
super(activity, 0, imageAndTexts);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext();
LayoutInflater inflater = activity.getLayoutInflater();
// Inflate the views from XML
View rowView = inflater.inflate(R.layout.image_text_layout, null);
JSONObject jsonImageText = getItem(position);
//////////////////////////////////////////////////////////////////////////////////////////////////////
//The next section we update at runtime the text - as provided by the JSON from our REST call
////////////////////////////////////////////////////////////////////////////////////////////////////
TextView textView = (TextView) rowView.findViewById(R.id.job_text);
try {
Spanned text = (Spanned)jsonImageText.get("text");
textView.setText(text);
} catch (JSONException e) {
textView.setText("JSON Exception");
}
return rowView;
}
}
Open URL in default browser
Open URL with Android
If you have the URL or URI object at one point, probably one of the explanations in the links above will get you on your way.
--
Edit:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
adapter.getItem(position).toString();
String link = o.toString();
Toast.makeText(this, "You selected: " + link, Toast.LENGTH_LONG)
.show();
}
Guessing this is the code to show the link but if you don't supply us what the getListAdapter() method etc do then I'm afraid it's hard to help you out.
Try this
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
JSONObject o = (JSONObject) this.getListAdapter().getItem(position);
adapter.getItem(position).toString();
String link = o.getString("NameOfLinkInJsonObject");
Toast.makeText(this, "You selected: " + link, Toast.LENGTH_LONG)
.show();
mMyWebView.loadUrl(link);
}
Be aware that you need to fix "NameOfLinkInJsonObject".
In your activity add a field like so
private WebView mMyWebView;
In your onCreate method add a
mMyWebView = (WebView) findViewById(R.id.webViewId);
You will have to change the R.id.webViewId to the appropriate id for your web view.