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.
Related
I have created a button, so that, when pressed it will load the next link in the array, but the only problem is that it doesn't update the view pager container. I have created a button for both next and previous, but I feel like I am missing a small detail, because this method does not load data into the viewerpager
Here is the code;
lstData = new ArrayList<>();
getData();
ViewPager myrv = (ViewPager) findViewById(R.id.view_page);
myViewPager = new PageViewAdapter(this, lstData);
myrv.setAdapter(myViewPager);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (int_of_inital_chapter - 1 < 0) {
Log.d("Newest Data Present","YUH");
} else {
URL = list.get(int_of_inital_chapter - 1).getLink();
lstData = new ArrayList<>();
getData();
}
}
});
previous.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (int_of_inital_chapter + 1 > list.size()) {
Log.d("Oldest Chapter Enabled","YUH");
} else {
URL = list.get(int_of_inital_chapter + 1).getLink();
lstData = new ArrayList<>();
getData();
}
}
});
PageViewAdapter Contructor
public PageViewAdapter(Context context, List<Page> PageList) {
this.context = context;
this.PageList = PageList;
inflater = LayoutInflater.from(context);
}
The algorithm works so that when the activity is created it automatically loads the data assigned to the inputted URL. Which Works. But I want to have the buttons load whatever is next or before the URL in the list array. What am I missing?
Try this:
declare the following globally:
private ViewPager myrv;
private ViewPagerAdapter myViewPager;
and then initialize them:
myrv = (ViewPager) findViewById(R.id.view_page);
myViewPager = new PageViewAdapter(this, lstData);
and also put the following in the buttons onClick
myViewPager = new PageViewAdapter(this, lstData);
myrv.setAdapter(myViewPager);
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();
i'm making a news app which in the news activity i have a tab slider with 2 tabs called , "new news" - "random or top news". which each tabs content are fragment.(i hope u get me!).
but the problem is im using a code to parse the this page content > http://aliak.xzn.ir/rap/get_all_products.php (i think u findout what site code im using)...
the problem is that if i want to show this in fragment(not activity) i have to write this code:
public class tab1 extends ListFragment {}
instead of this :
public class tab1 extends Fragment {}
and i the pager adapter i have problem which tells me the listfragment is not accepted! and if i dont use list fragment i have many errors!
THE CODES FOR APP :
my tab1 fragment ::
public class tab1 extends ListFragment {
//static final String url_all_products = "http://aliak.xzn.ir/rap/get_all_products.php";
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
final String TAG_SUCCESS = "success";
final String TAG_PRODUCTS = "products";
final String TAG_PID = "pid";
final String TAG_NAME = "name";
// JSON Node names
// products JSONArray
JSONArray products = null;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab_1, container, false);
/**
* Background Async Task to Load all product by making HTTP Request
*/
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
*/
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("درحال دريافت اخبار،کمي صبر کنيد!");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
*/
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest("http://aliak.xzn.ir/rap/get_all_products.php", "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getActivity().getApplicationContext(),
Main.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* *
*/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
getActivity(), productsList,
R.layout.list_item, new String[]{TAG_PID,
TAG_NAME},
new int[]{R.id.pid, R.id.name});
// updating listview
setListAdapter(adapter);
}
});
}
}
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
return v;
}
}
my adapter class :
/**
* Created by hp1 on 21-01-2015.
*/
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
}
//This method return the fragment for the every position in the View Pager
#Override
public Fragment getItem(int position) {
if (position == 0) // if the position is 0 we are returning the First tab
{
tab1 tab1 = new tab1();
return tab1;
} else // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
{
tab2 tab2 = new tab2();
return tab2;
}
}
/*
*/
// This method return the titles for the Tabs in the Tab Strip
#Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
// This method return the Number of tabs for the tabs Strip
#Override
public int getCount() {
return NumbOfTabs;
}
}
IF U NEEDED MORE CODES JUST TELL ME , ILL ANSWER IN 2 MIN TOPS!
check your imports and make sure your Fragment and ListFragment come from the same package, i.e. both android.support.v4.app.Fragment/ListFragment or both android.app.Fragment/ListFragment. Whichever one you use it has to be consistent with your actual fragment class implementations.
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 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) {}
}));