Good practice to use Fragments and ViewPager - java

Problem Description
I am writing application which use fragments and ViewPager below you can see my FragmentPagerAdapter. I just want to know is it right way to write FragmentPagerAdapter? because in some cases when I call getActivity() in fragment it return null.
public class FragmentAdapter extends FragmentPagerAdapter {
/* Fragments */
private Fragments[] mFragments;
/**
* Constructor.
* #param fm
*/
public FragmentAdapter(FragmentManager fm) {
super(fm);
/* Initialize Fragments. */
mFragments = Fragments.values();
}
public enum Fragments {
Favorites(App.getStringByResId(R.string.favorites), new FragmentFavorites()),
Categories(App.getStringByResId(R.string.categories), new FragmentCategories()),
YellowPages(App.getStringByResId(R.string.yellow_pages), new FragmentYellowPages());
/**
* Constructor.
* #param title Fragment title.
* #param fragment Fragment object.
*/
Fragments(String title, BaseListFragment fragment) {
this.mTitle = title;
this.mFragment = fragment;
}
/* Fragment Title Text. */
private String mTitle;
/* Fragment */
private BaseListFragment mFragment;
/**
* Get Fragment Title.
* #return Title.
*/
public String getTitle() {
return mTitle;
}
/**
* Get Fragment
* #return Fragment.
*/
public BaseListFragment getFragment() {
return mFragment;
}
};
#Override
public CharSequence getPageTitle(int position) {
return mFragments[position].getTitle();
}
#Override
public Fragment getItem(int position) {
return mFragments[position].getFragment();
}
#Override
public int getCount() {
return mFragments.length;
};
};
Edited
from main activity I call this code. is it correct ? In most cases in this case getActivity() return null.
/* Call Search function for the given fragment. */
Fragments.values()[tabControl.getCurrentItem()].getFragment().search(tv.getText().toString());
Fragment
public class FragmentFavorites extends BaseListFragment {
final static String TAG = FragmentFavorites.class.getSimpleName();
/* Controllers. */
ListView lvFavorites;
TextView tvInfo;
/* Simple Cursor Adapter. */
SimpleCursorAdapter scAdapter;
/* This member keep current database language, every time then onResume method called
* application must check if database language was changed, if language changed application
* must update list view by creating new adapter. */
String language;
/* This flag shows that new favorite was added and list need to be updated. */
boolean newFavoriteAdded = false;
boolean canUpdateOnSearch = false;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View favoritesPagesView = inflater.inflate(R.layout.fragment_favorites, container, false);
Log.i(TAG, "onCreateView( )");
/* Initialize Controllers. */
tvInfo = (TextView) favoritesPagesView.findViewById(R.id.tvNoFavoritesText);
lvFavorites = (ListView) favoritesPagesView.findViewById(android.R.id.list);
return favoritesPagesView;
} /* onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate( )");
/* Tell the framework to try to keep this fragment around
during a configuration change. */
setRetainInstance(true);
/* Get context, if context is null set default language "English". */
final Context context = getActivity();
if (context != null) {
Log.wtf(TAG, "onCreate(Context is null, set default language \"en\".)");
/* Set language when fragment first created, default language is "English". */
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
language = preferences.getString(Preferences.LANGUAGE, Preferences.EN);
}
else language = Preferences.EN;
} /* onCreate(Bundle savedInstanceState) */
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(TAG, "onActivityCreated( )");
ListView listView = getListView();
/* Set on item click listener */
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View v, int position, long id) {
Cursor cursor = (Cursor)adapterView.getAdapter().getItem(position);
final String _id = cursor.getString(cursor.getColumnIndex("_id"));
Log.i(TAG, "onItemClick(_id = " + _id + ")");
/* Get current selected language. */
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
final String lang = preferences.getString(Preferences.LANGUAGE, "en");
ActionDialog dialog = new ActionDialog(getActivity());
dialog.setFavoriteRemovedListener(new OnActionDialogListeners() {
#Override
public void onFavoriteRemoved() {
/* Get Saved favorites from the preferences, if there are no favorites return form onResume. */
final String favorites = preferences.getString(Preferences.FAVORITE, null);
/* Update favorites adapter with new favorite contacts. */
updateAdapterIfNeeded(lang, favorites, true);
}
#Override
public void onFavoriteAdded() { /* Not used... */ }
});
dialog.setID(_id);
dialog.setDeleteFavorite(true);
dialog.setTitle(cursor.getString(cursor.getColumnIndex(String.format("name_%s", language))));
dialog.setPhone(cursor.getString(cursor.getColumnIndex("telephones")));
dialog.show();
}
});
} /* onActivityCreated(Bundle savedInstanceState) */
#Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume( )");
/* Check if database current language was changed. */
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
final String currentLanguage = preferences.getString(Preferences.LANGUAGE, Preferences.EN);
/* Get Saved favorites from the preferences, if there are no favorites return form onResume. */
final String favorites = preferences.getString(Preferences.FAVORITE, null);
if (favorites == null || favorites.length() == 0) {
/* Hide List View and show Text View. */
Log.i(TAG, "There are no saved favorites in the preferences.");
lvFavorites.setVisibility(View.GONE);
tvInfo.setVisibility(View.VISIBLE);
tvInfo.setText(getActivity().getString(R.string.no_selected_favorites));
return;
}
if (!currentLanguage.equalsIgnoreCase(language)) {
Log.i(TAG, "onResume(Database language was changed.)");
/* Update language with changed one. */
language = currentLanguage;
/* Update Adapter, do not pay attention if it is already created or no. */
updateAdapterIfNeeded(currentLanguage, favorites, true);
}
else if (newFavoriteAdded) {
Log.i(TAG, "onResume(New favorite was added to the list, update needed");
/* Update adapter as new favorite was added. */
updateAdapterIfNeeded(currentLanguage, favorites, true);
newFavoriteAdded = false;
}
else {
/* Update Adapter only in the case if no adapter is created (This will create new adapter) */
updateAdapterIfNeeded(currentLanguage, favorites, false);
}
} /* void onResume() */
/**
* This function update Simple Cursor Adapter in the case if it is needed or
* cursor adapter is not created yet.
*
* #param currentLanguage Current language of database.
* #param forceUpdate if true adapter will be updated in any case; otherwise it will
* not be updated.
*/
void updateAdapterIfNeeded(final String lang, final String favs, final boolean forceUpdate) {
Log.i(TAG, String.format("updateAdapterIfNeeded(%s, %b)", lang, forceUpdate));
/* Show "Favorites" list view and hide Info text view. */
lvFavorites.setVisibility(View.VISIBLE);
tvInfo.setVisibility(View.GONE);
if (forceUpdate || scAdapter == null) {
/* Preparing Adapter Settings. */
final int[] to = new int[]{ R.id.tvContactTitle, R.id.tvContactTelephone };
final String[] from = new String[] { String.format("name_%s", lang), "telephones" };
/* Get Contacts from the SQLite Database. */
final Cursor cursor = DataBaseManager.instance().getAllFavorites(favs, lang);
Log.i(TAG, "updateAdapterIfNeeded(Creating new Adapter ...)");
/* Create New Simple Cursor Adapter, even if it is already exists. */
scAdapter = new FragmentCursorAdapter(getActivity(), R.layout.contact_row, cursor, from, to, 0);
Log.i(TAG, "updateAdapterIfNeeded(Adapter was successfully created ...)");
/* Set Adapter to the list. */
this.setListAdapter(scAdapter);
}
} /* updateAdapterIfNeeded(final String currentLanguage, boolean forceUpdate) */
void updateAdapterOnSearch(final String tts, final String lang, final String favs) {
Log.i(TAG, String.format("updateAdapterOnSearch(%s, %s)", tts, lang));
/* Show "Favorites" list view and hide Info text view. */
lvFavorites.setVisibility(View.VISIBLE);
tvInfo.setVisibility(View.GONE);
/* Preparing Adapter Settings. */
final int[] to = new int[]{ R.id.tvContactTitle, R.id.tvContactTelephone };
final String[] from = new String[] { String.format("name_%s", lang), "telephones" };
/* Get Contacts from the SQLite Database. */
final Cursor cursor = DataBaseManager.instance().getSearchedFavorites(favs, lang, tts);
if (cursor.getCount() == 0) {
lvFavorites.setVisibility(View.GONE);
tvInfo.setVisibility(View.VISIBLE);
tvInfo.setText(String.format(getActivity().getString(R.string.no_companies_were_found), tts));
}
Log.i(TAG, "updateAdapterOnSearch(Creating new Adapter ...)");
/* Create New Simple Cursor Adapter, even if it is already exists. */
scAdapter = new FragmentCursorAdapter(getActivity(), R.layout.contact_row, cursor, from, to, 0);
Log.i(TAG, "updateAdapterOnSearch(Adapter was successfully created ...)");
/* Set Adapter to the list. */
this.setListAdapter(scAdapter);
} /* updateAdapterOnSearch(final String tts, final String lang) */
#Override
public void search(final String tts) {
final Context context = getActivity();
Log.i(TAG, String.format("search(%s)", tts));
/* Get application context. */
if (context == null) {
Log.wtf(TAG, "search(Search can't be completed as context is null)");
return;
}
/* Get Database current language. */
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences == null) {
Log.w(TAG, "search(Search can't be completed as preferences object is null)");
return;
}
/* Get language from the preferences */
final String lang = preferences.getString(Preferences.LANGUAGE, Preferences.EN);
/* Get Saved favorites from the preferences, if there are no favorites return form onResume. */
final String favorites = preferences.getString(Preferences.FAVORITE, null);
/* If user search for the text. */
if (tts != null && tts.length() != 0) {
updateAdapterOnSearch(tts, lang, favorites);
canUpdateOnSearch = true;
}
if ((tts == null || tts.length() == 0) && canUpdateOnSearch) {
/* In this case all contacts will be shown in the list view. */
updateAdapterIfNeeded(lang, favorites, true);
canUpdateOnSearch = false;
}
} /* search(final String tts) */
public void newFavoriteAdded(final boolean value) {
Log.i(TAG, String.format("newFavoriteAdded(%b)", value));
newFavoriteAdded = value;
}
};

You should call getActivity() only inside or after Fragment.onActivityCreated() is called, in this way activity will never be null, as in example.
Also here is another case of where null activity may occurs, when you start thread inside fragment, and call getActivity() while user already closed fragment.
new Thread() {
#Override
public void run() {
// long operations goes here
// user decided to close fragment
Activity activity = getActivity();
// here 'activity' is null because fragment is destroyed
}
}.start();

Related

RecyclerView not refreshing after a web service manual synced

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();

Images in the RecyclerView are not refreshing

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.

Android : Using Listfragment instead of Fragment

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.

adding onClick listener to gridView items (to launch unique intents depending on position)

I have a gridView and I'd like to launch a different intent depending on the position of the item clicked.
I've instantiated the following onClick listener which includes the value for position:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
But I believe I'll need to add an if statement within it to determine which intent to launch depending on this position. So far I've come up with the following but I do not believe it is exactly what I'm looking for.
if position = 1 { Intent intent = new Intent(this, Activity1.class); }else if position = 2 { Intent intent = new Intent(this, Activity1.class); }
Any suggestions are greatly appreciated.
/**
* This activity presents a screen with a grid on which images can be added and
* moved around. It also defines areas on the screen where the dragged views can
* be dropped. Feedback is provided to the user as the objects are dragged over
* these drop zones.
*
*
* Like the DragActivity in the previous version of the DragView example
* application, the code here is derived from the Android Launcher code.
*
*
* The original Launcher code required a long click (press) to initiate a
* drag-drop sequence. If you want to see that behavior, set the variable
* mLongClickStartsDrag to true. It is set to false below, which means that any
* touch event starts a drag-drop.
*
*/
public class DragActivity extends Activity implements View.OnLongClickListener,
View.OnClickListener, View.OnTouchListener // ,
// AdapterView.OnItemClickListener
{
/**
*/
// Constants
private static final int HIDE_TRASHCAN_MENU_ID = Menu.FIRST;
private static final int SHOW_TRASHCAN_MENU_ID = Menu.FIRST + 1;
private static final int ADD_OBJECT_MENU_ID = Menu.FIRST + 2;
private static final int CHANGE_TOUCH_MODE_MENU_ID = Menu.FIRST + 3;
private boolean isErase = true;
private EditText et;
private TextView tx;
/**
*/
// Variables
private DragController mDragController; // Object that handles a drag-drop
// sequence. It intersacts with
// DragSource and DropTarget
// objects.
private DragLayer mDragLayer; // The ViewGroup within which an object can be
// dragged.
private DeleteZone mDeleteZone; // A drop target that is used to remove
// objects from the screen.
private int mImageCount = 0; // The number of images that have been added to
// screen.
private ImageCell mLastNewCell = null; // The last ImageCell added to the
// screen when Add Image is clicked.
private boolean mLongClickStartsDrag = true; // If true, it takes a long
// click to start the drag
// operation.
// Otherwise, any touch
// event starts a drag.
public static final boolean Debugging = false; // Use this to see extra
// toast messages.
/**
*/
// Methods
/**
* Add a new image so the user can move it around. It shows up in the
* image_source_frame part of the screen.
*
* #param resourceId
* int - the resource id of the image to be added
*/
public void addNewImageToScreen(int resourceId) {
if (mLastNewCell != null)
mLastNewCell.setVisibility(View.GONE);
FrameLayout imageHolder = (FrameLayout) findViewById(R.id.image_source_frame);
if (imageHolder != null) {
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
Gravity.CENTER);
ImageCell newView = new ImageCell(this);
newView.setImageResource(resourceId);
imageHolder.addView(newView, lp);
newView.mEmpty = false;
newView.mCellNumber = -1;
mLastNewCell = newView;
mImageCount++;
// Have this activity listen to touch and click events for the view.
newView.setOnClickListener(this);
newView.setOnLongClickListener(this);
newView.setOnTouchListener(this);
}
}
/**
* Add one of the images to the screen so the user has a new image to move
* around. See addImageToScreen.
*
*/
public void addNewImageToScreen() {
int resourceId = R.drawable.sqwhite;
addNewTextToScreen();
int m = mImageCount % 3;
if (m == 1)
resourceId = R.drawable.sqdrk;
else if (m == 2)
resourceId = R.drawable.sqwhite;
addNewImageToScreen(resourceId);
}
private void addNewTextToScreen() {
// TODO Auto-generated method stub
// et.setVisibility(View.VISIBLE);
if (isErase) {
tx.setText(et.getText().toString());
} else {
tx.setText("");
et.setVisibility(View.GONE);
}
isErase = !isErase;
}
/**
* Handle a click on a view.
*
*/
public void onClick(View v) {
if (mLongClickStartsDrag) {
// Tell the user that it takes a long click to start dragging.
//toast("Press and hold to drag an image.");
}
}
/**
* Handle a click of the Add Image button
*
*/
public void onClickAddImage(View v) {
addNewImageToScreen();
}
/**
* onCreate - called when the activity is first created.
*
* Creates a drag controller and sets up three views so click and long click
* on the views are sent to this activity. The onLongClick method starts a
* drag sequence.
*
*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
et = (EditText) findViewById(R.id.editText1);
et.setVisibility(View.INVISIBLE);
tx = (TextView) findViewById(R.id.textView1);
GridView gridView = (GridView) findViewById(R.id.image_grid_view);
if (gridView == null)
toast("Unable to find GridView");
else {
gridView.setAdapter(new ImageCellAdapter(this));
// gridView.setOnItemClickListener (this);
}
mDragController = new DragController(this);
mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
mDragLayer.setDragController(mDragController);
mDragLayer.setGridView(gridView);
mDragController.setDragListener(mDragLayer);
// mDragController.addDropTarget (mDragLayer);
mDeleteZone = (DeleteZone) findViewById(R.id.delete_zone_view);
// Give the user a little guidance.
Toast.makeText(getApplicationContext(),
getResources().getString(R.string.instructions),
Toast.LENGTH_LONG).show();
}
/**
* Build a menu for the activity.
*
*/
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, HIDE_TRASHCAN_MENU_ID, 0, "Hide Trashcan").setShortcut('1',
'c');
menu.add(0, SHOW_TRASHCAN_MENU_ID, 0, "Show Trashcan").setShortcut('2',
'c');
menu.add(0, ADD_OBJECT_MENU_ID, 0, "Add View").setShortcut('9', 'z');
menu.add(0, CHANGE_TOUCH_MODE_MENU_ID, 0, "Change Touch Mode");
return true;
}
/**
* Handle a click of an item in the grid of cells.
*
*/
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ImageCell i = (ImageCell) v;
trace("onItemClick in view: " + i.mCellNumber);
}
/**
* Handle a long click. If mLongClick only is true, this will be the only
* way to start a drag operation.
*
* #param v
* View
* #return boolean - true indicates that the event was handled
*/
public boolean onLongClick(View v) {
if (mLongClickStartsDrag) {
// trace ("onLongClick in view: " + v + " touchMode: " +
// v.isInTouchMode ());
// Make sure the drag was started by a long press as opposed to a
// long click.
// (Note: I got this from the Workspace object in the Android
// Launcher code.
// I think it is here to ensure that the device is still in touch
// mode as we start the drag operation.)
if (!v.isInTouchMode()) {
toast("isInTouchMode returned false. Try touching the view again.");
return false;
}
return startDrag(v);
}
// If we get here, return false to indicate that we have not taken care
// of the event.
return false;
}
/**
* Perform an action in response to a menu item being clicked.
*
*/
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case HIDE_TRASHCAN_MENU_ID:
if (mDeleteZone != null)
mDeleteZone.setVisibility(View.INVISIBLE);
return true;
case SHOW_TRASHCAN_MENU_ID:
if (mDeleteZone != null)
mDeleteZone.setVisibility(View.VISIBLE);
return true;
case ADD_OBJECT_MENU_ID:
// Add a new object to the screen;
addNewImageToScreen();
return true;
case CHANGE_TOUCH_MODE_MENU_ID:
mLongClickStartsDrag = !mLongClickStartsDrag;
String message = mLongClickStartsDrag ? "Changed touch mode. Drag now starts on long touch (click)."
: "Changed touch mode. Drag now starts on touch (click).";
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG)
.show();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* This is the starting point for a drag operation if mLongClickStartsDrag
* is false. It looks for the down event that gets generated when a user
* touches the screen. Only that initiates the drag-drop sequence.
*
*/
public boolean onTouch(View v, MotionEvent ev) {
// If we are configured to start only on a long click, we are not going
// to handle any events here.
if (mLongClickStartsDrag)
return false;
boolean handledHere = false;
final int action = ev.getAction();
// In the situation where a long click is not needed to initiate a drag,
// simply start on the down event.
if (action == MotionEvent.ACTION_DOWN) {
handledHere = startDrag(v);
}
return handledHere;
}
/**
* Start dragging a view.
*
*/
public boolean startDrag(View v) {
DragSource dragSource = (DragSource) v;
// We are starting a drag. Let the DragController handle it.
mDragController.startDrag(v, dragSource, dragSource,
DragController.DRAG_ACTION_MOVE);
return true;
}
/**
* Show a string on the screen via Toast.
*
* #param msg
* String
* #return void
*/
public void toast(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
} // end toast
/**
* Send a message to the debug log. Also display it using Toast if Debugging
* is true.
*/
public void trace(String msg) {
Log.d("DragActivity", msg);
if (!Debugging)
return;
toast(msg);
}
} // end class
from the android documentation for GridView:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
edit:
It looks like the addNewImageToScreen() is where you are adding the ImageCells, so assuming that you can generate the intent in that scope..
Intent intent = new Intent(this, Activity1.class); // or whatever you want to run
ImageCell newView = ...
newView.setTag( intent );
then in your onItemClick:
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
Intent intent = (Intent) v.getTag();
// now you can startActivity with your intent..
}
Make a string array with your classes, the in the onItemClick () create another string declared with the position (item) clicked.
public class MainActivity extends Activity {
String [] classes = {"act1", "act2"}; // activity files
GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
#SuppressWarnings("rawtypes")
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String pos = classes[position];
try {
Class start = Class.forName("com.company.app." + pos); //Your package name
Intent i = new Intent(MainActivity.this, start);
startActivity(i);
} catch(ClassNotFoundException e){
e.printStackTrace();
}
Toast.makeText(
getApplicationContext(),
((TextView) v.findViewById(R.id.label))
.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}

Adapter not display content

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().

Categories

Resources