How can I refresh the view of a fragment, when the back button is pressed?
I have tried this in the onResume method of the fragment but it doesn't work.
OK, here is the code
#SuppressWarnings("unused")
public class RestaurantMenuFragment extends Fragment {
private static final String TAG = "MenuItemsFragment";
private static final String CATEGORIES_KEY = "categories";
private static final String SELECTED_CATEGORY_ID_KEY = "category";
private static final String RESTAURANT_KEY = "restaurant123";
private static final String RESTAURANT_KCITY = "city";
private Spinner mCategoriesSpinner;
private ArrayAdapter<CategoriesResponse.Category> mCategoriesAdapter;
private ListView mListView;
private List<MenuItem> mItems;
private MenuItemsAdapter mItemsAdapter;
private EmptyLayout mEmptyLayout;
private Restaurant mRestaurant;
private int mCategoryId;
private List<CategoriesResponse.Category> mCategories;
private RestaurantActivity mActivity;
private MainApplication mApplication;
private CategoriesResponse mCategoriesResponse;
private ActionBar mActionBar;
private Gson mGson;
int categ;
private ObjectGetter mObjectGetter;
public static RestaurantMenuFragment newInstance(Restaurant restaurant) {
RestaurantMenuFragment fragment = new RestaurantMenuFragment();
Bundle args = new Bundle();
args.putString(RESTAURANT_KEY, new Gson().toJson(restaurant));
String dd=restaurant.city;
Log.i("dd12", dd);
fragment.setArguments(args);
return fragment;
}
public RestaurantMenuFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = (RestaurantActivity) getActivity();
mApplication = (MainApplication) mActivity.getApplication();
mActionBar = mActivity.getSupportActionBar();
mGson = new Gson();
mObjectGetter = new ObjectGetter();
mCategories = new ArrayList<CategoriesResponse.Category>();
Log.i("mCategories",""+mCategories);
mItems = new ArrayList<MenuItem>();
Log.i("12345",""+mItems);
mItemsAdapter = new MenuItemsAdapter(getActivity(), mItems);
Bundle args = getArguments();
if (args != null) {
mRestaurant = mGson.fromJson(args.getString(RESTAURANT_KEY),
Restaurant.class);
}
if (savedInstanceState != null) {
mRestaurant = mGson.fromJson(
savedInstanceState.getString(RESTAURANT_KEY),
Restaurant.class);
mCategoryId = savedInstanceState.getInt(SELECTED_CATEGORY_ID_KEY);
mCategoriesResponse = mGson.fromJson(
savedInstanceState.getString(CATEGORIES_KEY),
CategoriesResponse.class);
}
assert mRestaurant != null;
updateCart();
}
public void updateCart() {
View view = mActionBar.getCustomView();
Button cartButton = (Button) view.findViewById(R.id.cartButton);
int nOfItems = 0;
if (mApplication.isCartCreated()) {
nOfItems = mApplication.getCart().getNOfAllItems();
}
cartButton.setText(String.format("%d", nOfItems));
if (nOfItems > 0) {
cartButton.setEnabled(true);
} else {
cartButton.setEnabled(false);
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Gson gson = new Gson();
outState.putString(RESTAURANT_KEY, gson.toJson(mRestaurant));
outState.putInt(SELECTED_CATEGORY_ID_KEY, mCategoryId);
outState.putString(CATEGORIES_KEY, gson.toJson(mCategoriesResponse));
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.spinner_list, container, false);
RestaurantActivity activity = (RestaurantActivity) getActivity();
String myDataFromActivity = activity.getMyData();
String myDataFromActivity1 = activity.getMyData1();
Log.i("myDataFromActivity",myDataFromActivity);
Log.i("myDataFromActivity1",myDataFromActivity1);
categ=Integer.parseInt(myDataFromActivity1);
mListView = (ListView) view.findViewById(R.id.list122334);
mListView.setAdapter(mItemsAdapter);
Log.d(TAG,"Querying items url "
+ Urls.menuItemsQuery(mRestaurant.id,categ));
mEmptyLayout = EmptyLayout.with(getActivity()).to(mListView)
.setEmptyMessage(R.string.categories_empty_message)
.showLoading();
loadItems();
return view;
}
private void loadItems() {
mEmptyLayout.showLoading();
mItems.clear();
mObjectGetter.getJsonObjectOrDialog(mActivity,
Urls.menuItemsQuery(mRestaurant.id, categ),
ItemsResponse.class,
new ObjectGetter.OnFinishedListener<ItemsResponse>() {
#Override
public void onFinishedLoadingObject(
ItemsResponse itemsResponse) {
mEmptyLayout.showEmpty();
if (itemsResponse != null
&& itemsResponse.items != null) {
mItems.addAll(itemsResponse.items);
}
mItemsAdapter.notifyDataSetChanged();
}
});
}
private class MenuItemsAdapter extends ArrayAdapter<MenuItem> {
private static final String TAG = "MenuItemsAdapter";
public MenuItemsAdapter(Context context, List<MenuItem> menuItems) {
super(context, 0, menuItems);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final MenuItem menuItem = getItem(position);
View view = convertView;
final ViewHolder viewHolder;
LayoutInflater inflater;
if (convertView == null) {
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.menu_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) view.findViewById(R.id.name);
viewHolder.description = (TextView) view.findViewById(R.id.description);
viewHolder.price = (TextView) view.findViewById(R.id.price);
viewHolder.add = (Button) view.findViewById(R.id.add);
viewHolder.selectedView = view.findViewById(R.id.selectedView);
viewHolder.remove = (Button) view.findViewById(R.id.remove);
viewHolder.total = (TextView) view.findViewById(R.id.itemTotal);
viewHolder.quantity = (TextView) view.findViewById(R.id.quantity);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
try
{
viewHolder.name.setText(menuItem.name);
viewHolder.description.setText(menuItem.description);
viewHolder.price.setText(String.valueOf(menuItem.price));
}catch(NullPointerException e){
e.printStackTrace();
}
viewHolder.add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mApplication.createNewCartIfPossibleAndAskIfNot(
getActivity(), mRestaurant,
new MainApplication.OnCreateCartListener() {
#Override
public void onCreateCart(Cart cart) {
cart.addOne(menuItem);
updateItemFromCart(menuItem, viewHolder);
updateCart();
}
});
}
});
viewHolder.remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!mApplication.isCartCreated()) {
return;
}
mApplication.getCart().removeOne(menuItem);
updateItemFromCart(menuItem, viewHolder);
updateCart();
}
});
return view;
}
private void updateItemFromCart(MenuItem menuItem, ViewHolder viewHolder) {
if (!mApplication.isCartCreated()) {
return;
}
int quantity = mApplication.getCart().getNOfItemsOfType(menuItem);
if (quantity > 0) {
viewHolder.selectedView.setVisibility(View.VISIBLE);
} else {
viewHolder.selectedView.setVisibility(View.GONE);
}
viewHolder.quantity.setText(String.valueOf(quantity));
viewHolder.total.setText(String.valueOf(quantity
* menuItem.price));
}
class ViewHolder {
TextView name;
TextView description;
TextView price;
Button add;
View selectedView;
Button remove;
TextView total;
TextView quantity;
}
}
#Override
public void onResume() {
super.onResume();
updateCart();
mItems.clear();
if (mItemsAdapter != null) {
mItemsAdapter.notifyDataSetChanged();
}
}
#Override
public void onDestroy() {
if (mObjectGetter != null) {
mObjectGetter.stopRequests();
}
super.onDestroy();
}
}
Now, i want to update the listvieww data when the user pressed the back button. I set the new loadItems() method in the onResume() Method of the Fragment. This Method is called but the old listview data appears and new data also appears...
Back button should be handled from Activity.
You can override onBackPressed in Activity and call a function on corresponding fragment to reloadItems().
Here are your 3 options I could think of.
Get reference to Fragment and call function to reLoadItems and its better to define an interface for this communication which fragment implements.
Better solution than first one. Add a LocalBroadcast which Activity broadcasts and your fragment listens and updates data on receiving broadcast.
Example for this :
http://luboganev.github.io/blog/messaging-with-localbroadcastmanager/
Otto event bus where both activity and fragment classes are connected to the event bus and they activity publishes event and fragment subscribes to it. This is what I am using for something similar in my application. (But I have pretty frequent asynchronous events that come along. SO I am using this. 2nd option might be sufficient in your case).
Example for this :
http://www.vogella.com/tutorials/JavaLibrary-EventBusOtto/article.html
As ramesh already mentioned, back button handling happens in your activity class that holds the fragments. Here is a simple example, how you can handle these back button events for your fragment.
Activity Code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
boolean returnSuperKeyDown = true;
if(keyCode == KeyEvent.KEYCODE_BACK){
Fragment fragment = getYourCurrentFragment();
if (fragment instanceof YourFragment) {
returnSuperKeyDown = ((YourFragment) fragment).onFragmentKeyDown();
}
}
if (returnSuperKeyDown) {
return super.onKeyDown(keyCode, event);
} else {
return true;
}
}
YourFragment Method:
public boolean onFragmentKeyDown() {
updateYourFragment();
return false;
}
#Rithe, #sunder sharma
As per me there is simple to refresh the fragment when come back from other fragment,
We just have to override the onActivityCreated Method for refresh fragment.
Like as
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//your code which you want to refresh
loadItems();
}
You can also update/refresh the fragment using onStart() method.
public void onStart(){
super.onStart();
//update your fragment
}
This worked fine for me.
call your loadItem() method onHiddenChanged(boolean hidden)method.onHiddenChanged is overrided method
Related
I am trying to make my app handle orientation changes. Right now I am working with a RecyclerView that has an CustomAdapter to handle the data. I have implemented onSaveInstanceState to save the data that is in the Adapter, but when I re-insert it after orientation change, the method onBindViewHolder is not called. I have confirmed the data is saved and the array lists are not empty and contain the data I need.
Here is the CustomAdapter:
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private static final String TAG = "CustomAdapter";
private ArrayList<Words> mDataSet;
private RecyclerView mRecyclerView;
private PopupWindow mPopupWindow;
private int currentPos;
private String srcOld;
private String targetOld;
private TranslateActivityFragment mFragment;
private EditText mEditSrc;
private EditText mEditTarget;
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
private TextView textView2;
private TextView textView3;
public ViewHolder(View v){
super(v);
textView = v.findViewById(R.id.adapter_textview1);
textView2 = v.findViewById(R.id.adapter_textview2);
textView3 = v.findViewById(R.id.adapter_textview3);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
mPopupWindow.showAtLocation(mRecyclerView, Gravity.CENTER,0,0);
mPopupWindow.setFocusable(true);
mPopupWindow.update();
currentPos = getAdapterPosition();
setCurrText();
}
private void setCurrText(){
Words tmp = mDataSet.get(currentPos);
srcOld = tmp.getW1();
targetOld = tmp.getW2();
mEditSrc.setText(srcOld);
mEditTarget.setText(targetOld);
}
});
}
public TextView getLeftTextView(){
return textView;
}
public TextView getCenterTextView(){return textView2;}
public TextView getRightTextView(){return textView3;}
}
public CustomAdapter(RecyclerView recyclerView, Context context, TranslateActivityFragment fragment){
mRecyclerView = recyclerView;
mDataSet = new ArrayList<Words>();
mFragment = fragment;
this.notifyDataSetChanged();
View v = LayoutInflater.from(mFragment.getActivity()).inflate(R.layout.edit_translate,null,false);
createPopupWindow(v, mFragment.getActivity());
setEditText(v);
}
private void setEditText(View v){
mEditSrc = v.findViewById(R.id.edit_src);
mEditTarget = v.findViewById(R.id.edit_target);
}
private void createPopupWindow(View v, Activity activity){
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
float width = 300*metrics.density;
float height = 200*metrics.density;
mPopupWindow = new PopupWindow(v,(int)width, (int)height);
Button buttonOk = v.findViewById(R.id.button_ok);
Button buttonCancel = v.findViewById(R.id.button_cancel);
Button buttonDelete = v.findViewById(R.id.button_delete);
buttonCancel.setOnClickListener(new CancelListener());
buttonOk.setOnClickListener(new OkListener());
buttonDelete.setOnClickListener(new DeleteListener());
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType){
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.textview_layout, viewGroup,false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position){
Log.d(TAG, "Element " + position + " set." + mDataSet.get(position).getW1());
viewHolder.getLeftTextView().setText(mDataSet.get(position).getW1());
viewHolder.getCenterTextView().setText(" - ");
viewHolder.getRightTextView().setText( mDataSet.get(position).getW2());
}
#Override
public int getItemCount() {
return mDataSet.size();
}
public void addData(Words newData, RecyclerView recyclerView){
mDataSet.add(newData);
System.out.println("------ ADDING DATA--------");
this.notifyDataSetChanged();
if (mDataSet.size() > 0) {
recyclerView.smoothScrollToPosition(mDataSet.size() - 1);
}
}
public void dismissPopup(){
mPopupWindow.dismiss();
}
public void removeWord(){
mDataSet.remove(currentPos);
System.out.println("mDataSet.size(): " + mDataSet.size());
System.out.println("mFragment.getTranslater.getData.size(): " + mFragment.getTranslater().getWords().size());
mFragment.getTranslater().deleteWord(currentPos);
this.notifyDataSetChanged();
}
// Update the current word from EditTexts
public void updateWord(){
String src = mEditSrc.getText().toString();
String target = mEditTarget.getText().toString();
mDataSet.get(currentPos).setW1(src);
mDataSet.get(currentPos).setW2(target);
mFragment.getTranslater().updateWord(src, target, currentPos);
this.notifyDataSetChanged();
}
private class OkListener implements View.OnClickListener {
#Override
public void onClick(View view) {
updateWord();
dismissPopup();
}
}
private class CancelListener implements View.OnClickListener{
#Override
public void onClick(View view){
dismissPopup();
}
}
private class DeleteListener implements View.OnClickListener{
#Override
public void onClick(View view){
removeWord();
dismissPopup();
}
}
public ArrayList<Words> getWords(){
return mDataSet;
}
public ArrayList<String> getW1List() {
ArrayList<String> tmp = new ArrayList<>();
for (Words w : mDataSet){
tmp.add(w.getW1());
}
return tmp;
}
public ArrayList<String> getW2List(){
ArrayList<String> tmp = new ArrayList<>();
for (Words w : mDataSet){
tmp.add(w.getW2());
}
return tmp;
}
//
// Here is where I try to restore data.
//
public void restoreWords(ArrayList<String> w1, ArrayList<String> w2, RecyclerView recyclerView){
if (mDataSet == null){
mDataSet = new ArrayList<>();
}
System.out.println("Restoring words");
for (int i = 0; i < w1.size(); ++i){
Words tmp = new Words(w1.get(i), w2.get(i), 0,0,0,false);
addData(tmp, recyclerView);
}
}
}
Code in Fragment to handle the view:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = new CustomAdapter(mRecyclerView, this.getContext(), this);
mRecyclerView.setAdapter(mAdapter);
if (savedInstanceState != null){
ArrayList<String> w1 = savedInstanceState.getStringArrayList(TranslateActivity.W1LIST);
ArrayList<String> w2 = savedInstanceState.getStringArrayList(W2LIST);
restoreWords(w1,w2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_translate, container, false);
mRecyclerView = root.findViewById(R.id.translate_recycle);
mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayout.VERTICAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
return root;
}
#Override
public void onViewCreated(View v, Bundle savedInstanceState){
mHelper = new Helper();
if (savedInstanceState == null) {
Intent intent = getActivity().getIntent();
source_lang = intent.getStringExtra(MainActivityFragment.SOURCE_LANG);
target_lang = intent.getStringExtra(MainActivityFragment.TARGET_LANG);
} else {
source_lang = savedInstanceState.getString(MainActivityFragment.SOURCE_LANG);
target_lang = savedInstanceState.getString(MainActivityFragment.TARGET_LANG);
}
key = mHelper.getKey(source_lang, target_lang);
mTranslater = new Translater(source_lang, target_lang, key);
setHasOptionsMenu(true);
setToolbarText();
}
public void restoreWords(ArrayList<String> w1, ArrayList<String> w2){
System.out.println("..................RESTORE WORDS....................");
mAdapter.restoreWords(w1, w2, mRecyclerView);
}
onCreate in Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
helper = new Helper();
mInAppPurchase = new InAppPurchase(this, this);
mInAppPurchase.setUpNoQuery();
setContentView(R.layout.activity_translate);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mFragment = new TranslateActivityFragment();
View v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.start_purchase_ui, null, false);
createPopup(v, this);
getSupportFragmentManager().beginTransaction().replace(R.id.translate_layout, mFragment).commit();
}
I have looked around and seen some suggestions to change getItemCount to not return 0. That did not help and I do not really see how that can be the problem. Here it says for ListViews one must do the restoration in either onCreateView or onActivityCreated, that did not help either.
Note that notifyDataSetChangedis called in addData(Words word)
EDIT: I have changed the flow to the following:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_translate, container, false);
mRecyclerView = root.findViewById(R.id.translate_recycle);
mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayout.VERTICAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new CustomAdapter(mRecyclerView, this.getContext(), this);
mRecyclerView.setAdapter(mAdapter);
return root;
}
#Override
public void onViewCreated(View v, Bundle savedInstanceState){
mHelper = new Helper();
if (savedInstanceState == null) {
// Create helper instance
// Get the input view from which we get the source word
textInput = v.findViewById(R.id.source_input);
// Extract the chosen langauges
Intent intent = getActivity().getIntent();
source_lang = intent.getStringExtra(MainActivityFragment.SOURCE_LANG);
target_lang = intent.getStringExtra(MainActivityFragment.TARGET_LANG);
} else {
source_lang = savedInstanceState.getString(MainActivityFragment.SOURCE_LANG);
target_lang = savedInstanceState.getString(MainActivityFragment.TARGET_LANG);
ArrayList<String> w1 = savedInstanceState.getStringArrayList(TranslateActivity.W1LIST);
ArrayList<String> w2 = savedInstanceState.getStringArrayList(W2LIST);
restoreWords(w1,w2);
}
key = mHelper.getKey(source_lang, target_lang);
mTranslater = new Translater(source_lang, target_lang, key);
setHasOptionsMenu(true);
setToolbarText();
}
More Information:
I have an EditText item from which I read the input and add to the mAdapter. After rotation, that does still work. It is only when I try to re-insert the saved data it doesn't work. So I suspect restoreWords are called from the wrong method?
So it seems like the solution was to do the restoration in the Activity instead of the Fragment. I did almost the same exactly as in my question. I overwrote onRestoreInstanceState and called restoreWords implemented as
public void restoreWords(ArrayList<String> w1, ArrayList<String> w2){
System.out.println("..................RESTORE WORDS....................");
for (int i = 0; i < w1.size(); ++i){
Words tmp = new Words(w1.get(i), w2.get(i), 0, 0, 0, false);
System.out.println(tmp.getW1() + " --- " + tmp.getW2());
mFragment.addWordToView(tmp);
}
System.out.println("-------------------Number of words in adapter: " + mFragment.getAdapter().getItemCount());
}
Then it magically worked. Can someone explain why?
I am working on an app that includes shopping feature and items are added to cart.
I have navigation drawer. In that, I have my first fragment called as "POSFragment" in which I have implemented a viewpager as data to be shown in the form of swipeable tabs. The data will be inflated dynamically. So, I have used PagerTabStrip and a viewpager. There is a common fragment called "MenuDetailFragment" in which data will be inflated dynamically through a webservice. the adapter for this fragment is "MenuPagerAdapter". Now, In this fragment the data will shown in listview. In Listview in each row items can be incremented or decremented using plus and minus Buttons in each row. Between these button there is textview where the quantity will be displayed correspondingly. I have used a Linkedhashmap on both buttons so, the items along with the quantity is saved in linkedhashmap and then in Sharedpreferences. It's working fine till now. The issue occurs when I swipe in the View pager and next fragment appears and When I click on increment button or decrement button on item there. In shared preferences, It starts saving the current fragment items and previous fragment data is not there anymore. I have already used the "yourcustomviewpager.setOffscreenPageLimit(3)" So, I can see the selected items with the quantities whe I swipe to and fro but the data i stored in sharedpreferences using Linkedhashmap is not there instead current fragment's data is saved. I want that the new data is added to previous data and data in all the fragments should persist. Is there a way to do so? I have been facing this issue for quite a long time.
Here are some screenshots and relevant code snippet.
public class PosFragment extends Fragment {
private ViewPager viewPager;
View rootview;
String code;
String name;
String desc;
String rate;
String key;
Button viewCartBtn;
static TextView displayPrice;
static TextView noOfItemsInCart;
RippleView rippleView;
//private sendPosition pos;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
Log.e("oncreateview", "called");
// Inflate the layout for this fragment
rootview = inflater.inflate(R.layout.pos_fragment_layout, container, false);
initViews();
new AsyncTaskGetMenu().execute();
return rootview;
}
public void initViews() {
Log.e("init", "called");
viewPager = (ViewPager) rootview.findViewById(R.id.viewPager);
Button viewCartButton = (Button) rootview.findViewById(R.id.view_cart_btn);
viewPager.setOffscreenPageLimit(3);
viewCartBtn = (Button) rootview.findViewById(R.id.view_cart_btn);
AppMethods.setGlametrixBoldFont(getActivity(), viewCartBtn);
displayPrice = (TextView) rootview.findViewById(R.id.textView_totalprice_cart);
noOfItemsInCart = (TextView) rootview.findViewById(R.id.cart_items_quantity);
viewCartBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ViewCartActivity.class);
intent.putExtra(AppConstants.SUBTOTAL, Integer.parseInt(displayPrice.getText().toString()));
startActivity(intent);
}
});
LinearLayout itemsInCartLinearLayout = (LinearLayout) rootview.findViewById(R.id.ll_items_in_cart);
itemsInCartLinearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ViewCartActivity.class);
intent.putExtra(AppConstants.SUBTOTAL, Integer.parseInt(displayPrice.getText().toString()));
startActivity(intent);
}
});
}
This is "MenuDetailFragment" class
public class MenuDetailFragment extends Fragment {
View rootview;
private ListView listView;
private ArrayList<MenuHeadingDetailsModel> menuheadingDetailList;
private Communicate comm;
LinkedHashMap<String, ViewCartDetailsModel> viewCartDetailsModelMap = new LinkedHashMap<>();
ViewCartDetailsModel viewCartDetailsModel;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootview = inflater.inflate(R.layout.menu_list, container, false);
InitializeViews();
return rootview;
}
public void InitializeViews() {
listView = (ListView) rootview.findViewById(R.id.menu_items_list);
Bundle bundle = getArguments();
menuheadingDetailList = bundle.getParcelableArrayList(AppConstants.MENU_KEY);
listView.setAdapter(new MenuAdapter(getActivity(), menuheadingDetailList));[enter image description here][1]
This is"MenuPagerAdapter" class
public class MenuPagerAdapter extends FragmentStatePagerAdapter {
List<MenuModel> menu;
public MenuPagerAdapter(FragmentManager fm, List<MenuModel> menuList) {
super(fm);
this.menu = menuList;
}
#Override
public CharSequence getPageTitle(int position) {
return menu.get(position).getHeading().toString();
}
private List<MenuHeadingDetailsModel> getMenuHeadingDetails(int pos) {
return this.menu.get(pos).getMenuHeadingDetailList();
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new MenuDetailFragment();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(AppConstants.MENU_KEY, (ArrayList<? extends Parcelable>) getMenuHeadingDetails(position));
fragment.setArguments(bundle);
return fragment;
}
#Override
public int getCount() {
return menu.size();
}
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {
if (observer == null) {
super.unregisterDataSetObserver(observer);
}
}
#Override
public void destroyItem (ViewGroup container, int position, Object object)
{
Log.e("destroyitem","called");
}
}
class MenuAdapter extends BaseAdapter {
//ViewHolder holder;
Context context;
List<MenuHeadingDetailsModel> myItems = new ArrayList<>();
Button buttonPlus, buttonMinus;
int totalAmount = 0;
int pagePosition;
public MenuAdapter(Context context, List<MenuHeadingDetailsModel> myItems) {
this.context = context;
this.myItems = myItems;
}
#Override
public int getCount() {
return myItems.size();
}
#Override
public Object getItem(int position) {
return myItems.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder = new ViewHolder();
final LayoutInflater inflater = getActivity().getLayoutInflater();
convertView = inflater.inflate(R.layout.menu_list_item, null);
viewHolder.codeText = (TextView) convertView.findViewById(R.id.textView_code);
viewHolder.nameText = (TextView) convertView.findViewById(R.id.textView_name);
AppMethods.setGlametrixBoldFont(getActivity(), viewHolder.nameText);
viewHolder.descText = (TextView) convertView.findViewById(R.id.textView_desc);
viewHolder.rateText = (TextView) convertView.findViewById(R.id.textView_rate);
viewHolder.textViewNoOfItems = (TextView) convertView.findViewById(R.id.tv_no_of_items);
buttonMinus = (Button) convertView.findViewById(R.id.button_minus);
buttonPlus = (Button) convertView.findViewById(R.id.button_plus);
final MenuHeadingDetailsModel menu = myItems.get(position);
viewHolder.codeText.setText(menu.getCode());
viewHolder.nameText.setText(menu.getItemName());
viewHolder.descText.setText(menu.getDescription());
viewHolder.rateText.setText(menu.getRate());
viewHolder.textViewNoOfItems.setText("" + menu.getQuantity());
buttonPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MenuHeadingDetailsModel menuList = myItems.get(position);
menuList.setQuantity(menuList.getQuantity() + 1);
myItems.set(position, menuList);
int itemRate = Integer.parseInt(myItems.get(position).getRate());
int itemQuan = menuList.getQuantity();
String code = menuList.getCode();
String name = menuList.getItemName();
int totalAmountPerItem = itemQuan * itemRate;
viewCartDetailsModel = new ViewCartDetailsModel(code, name, itemQuan, itemRate);
viewCartDetailsModelMap.put(viewCartDetailsModel.getCode(), viewCartDetailsModel);
Log.e("hashmap", "size" + viewCartDetailsModelMap.size());
Gson gson = new Gson();
String hashMapToString = gson.toJson(viewCartDetailsModelMap);
AppPreferences.saveDataInSharedpreferences(getActivity(), AppConstants.VIEW_ITEMS_INT_CART_KEY, hashMapToString);
totalAmount += itemRate;
int amount = totalAmount;
onButtonPressedForAmount(amount);
notifyDataSetChanged();
int quan = 0;
TextView tv;
for (int i = 0; i < listView.getCount(); i++) {
View view = listView.getAdapter().getView(i, null, null);
tv = (TextView) view.findViewById(R.id.tv_no_of_items);
quan = quan + Integer.parseInt(tv.getText().toString());
}
onButtonClickedForQuantity(quan);
}
}
);
buttonMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MenuHeadingDetailsModel menuList = myItems.get(position);
if ((menuList.getQuantity() - 1) >= 0) {
menuList.setQuantity(menuList.getQuantity() - 1);
myItems.set(position, menuList);
int itemRate = Integer.parseInt(myItems.get(position).getRate());
int itemQuan = menuList.getQuantity();
String code = menuList.getCode();
String name = menuList.getItemName();
//int totalAmountPerItem = itemQuan * itemRate;
viewCartDetailsModel = new ViewCartDetailsModel(code, name, itemQuan, itemRate);
if (itemQuan == 0) {
viewCartDetailsModelMap.remove(code);
} else {
viewCartDetailsModelMap.put(viewCartDetailsModel.getCode(), viewCartDetailsModel);
}
Gson gson = new Gson();
String hashMapToString = gson.toJson(viewCartDetailsModelMap);
AppPreferences.saveDataInSharedpreferences(getActivity(), AppConstants.VIEW_ITEMS_INT_CART_KEY, hashMapToString);
if (totalAmount > 0) {
totalAmount -= itemRate;
Log.e("totalamount", "minus" + totalAmount);
int amount = totalAmount;
onButtonPressedForAmount(amount);
}
} else {
menuList.setQuantity(0);
myItems.set(position, menuList);
}
notifyDataSetChanged();
int quan = 0;
TextView tv;
for (int i = 0; i < listView.getCount(); i++) {
View view = listView.getAdapter().getView(i, null, null);
tv = (TextView) view.findViewById(R.id.tv_no_of_items);
quan = quan + Integer.parseInt(tv.getText().toString());
}
onButtonClickedForQuantity(quan);
}
}
);
return convertView;
}
public void onButtonPressedForAmount(int userData) {
if (comm != null) {
comm.sendAmount(userData);
}
}
public void onButtonClickedForQuantity(int quantity) {
if (comm != null) {
comm.sendQuantity(quantity);
}
}
class ViewHolder {
TextView nameText;
TextView codeText;
TextView descText;
TextView rateText;
TextView textViewNoOfItems;
}
}
//For settings fonts to the alert diaolog for customization of items
//public void setGlametrixBoldFont(TextView textView) {
//Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/GlametrixBold.otf");
//textView.setTypeface(typeface);
// }
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
comm = (Communicate) activity;
} catch (Exception e) {
AppMethods.getStackTrace(e);
}
}
public interface Communicate {
public void sendAmount(int amount);
public void sendQuantity(int quantity);
}
// public void getPosition(int Position) {
//
// int pagePosition = Position;
// Log.e("pos", " " + pagePosition);
// }
}
You can get your current fragment inside view pager by following below way
SampleFragment sampleFr = (SampleFragment) viewPager.getAdapter().instantiateItem(viewPager, 0);
Now you can access any data you want using that fragment.
For the first time adding fragments in view pager do like below way.
BaseFragment frTimeLine = (BaseFragment) Fragment.instantiate(this, SampleFragment.class.getName());
fragments.add(frTimeLine);
BaseFragment frEarnTiqs = (BaseFragment) Fragment.instantiate(this, SampleFragment1.class.getName());
fragments.add(frEarnTiqs);
adapter = new ViewPagerAdapter(getSupportFragmentManager(), getResources(), fragments);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(3);
I am having some trouble with my app, I am trying to develop a list app that you have the option to add new "Goals" to as well as new lists so that you can categorize goals to a list like the list object title can be "Eat healthy" and then you'll have goals inside like "Eat more vegetables", etc. So in order to do this I have set up a ViewPagerHost class that grabs all the List objects from a SQLite database and initializes a factory developed "List fragment" that handles all the logic of adding a goal, editing and deleting, etc. I believe I've gotten this to work correctly because everything on the database side seems to work fine, and then the other classes are just supposed to display and manipulate that information. But everything on the display side seems to be going wrong. I have each view in the recyclerview display a star next to any goal marked as "important" or "life changing" but it displays the star wherever it wants. Also the list does not load until I've click on something.
public class ViewPagerHost extends Activity_Logger {
private static final String ARG_TITLE = "title";
private ViewPager mViewPager;
private ArrayList<Fragment> mFragmentList;
private ArrayList<GoalList> goalList;
private ArrayList<Goal> goalArrayList;
Database_Controller controller;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager_host);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
MobileAds.initialize(getApplicationContext(), "ad-String");
AdView adView = (AdView) findViewById(R.id.ad);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
setSupportActionBar(toolbar);
toolbar.setTitle("Ambition");
FragmentManager fragmentManager = getSupportFragmentManager();
controller = Database_Controller.get(getApplicationContext());
goalList = controller.getAllLists();
goalArrayList = controller.getAllGoals();
mViewPager = (ViewPager)findViewById(R.id.view_fragments);
if(goalList.get(0) == null){
GoalList listOne = new GoalList();
listOne.setTitle("DEFAULT");
controller.insertList(listOne);
for(int i =0; i< goalArrayList.size(); i++){
goalArrayList.get(i).setWhich_List(listOne.getTitle());
controller.updateGoal(goalArrayList.get(i));
}
}
mFragmentList = getFragmentList(goalList);
ViewPagerAdapter adapter = new ViewPagerAdapter(fragmentManager, mFragmentList);
if(adapter == null){
}else{
mViewPager.setAdapter(adapter);
}
}
public ArrayList<Fragment> getFragmentList(ArrayList<GoalList> list){
mFragmentList = new ArrayList<>();
Bundle bundle = new Bundle();
for(int i = 0; i< list.size(); i++){
List_Fragment list_fragment = new List_Fragment();
bundle.putString(ARG_TITLE, goalList.get(i).getTitle());
list_fragment.setArguments(bundle);
mFragmentList.add(list_fragment);
}
return mFragmentList;
}
}
Here is my ViewPagerHost class that creates the factory list.
public class List_Fragment extends Fragment {
private RecyclerView mRecyclerView;
private static final String ARG_TITLE = "title";
private static final String ARG_GOALNAME ="Goal";
private static final String ARG_IMPORTANT = "Important";
private Database_Controller mDatabaseController;
private static final String ARG_ID = "ID";
ArrayList<Goal> goalList;
LinearLayoutManager manager;
public RecyclerView.Adapter adapter;
public ReceiverThread UiThread;
FragmentManager fragmentManager;
String list_title;
public List_Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_list_, container, false);
Bundle bundle = getArguments();
if(bundle != null){
list_title= bundle.getString(ARG_TITLE);
}
mDatabaseController = mDatabaseController.get(getContext());
goalList = mDatabaseController.getAllListsGoals(list_title);
mRecyclerView = (RecyclerView)view.findViewById(R.id.recyclerview);
manager = new LinearLayoutManager(getActivity());
UiThread = new ReceiverThread();
mRecyclerView.setLayoutManager(manager);
final RecyclerView_Adapter wrapperClass = new RecyclerView_Adapter(goalList,getContext(),this);
adapter = wrapperClass.adapter;
for(int counter =0; counter < goalList.size(); counter++){
System.out.println(goalList.get(counter).getWhich_List());
}
mRecyclerView.setAdapter(adapter);
fragmentManager= getFragmentManager();
//adapter goes here
final FloatingActionButton fab = (FloatingActionButton)view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment fragment = List_Fragment.this;
AddNew_PopUp popUp = AddNew_PopUp.newInstance(fragment);
popUp.setTargetFragment(fragment, 0);
popUp.show(fragmentManager, "PopUpDialog");
}
});
return view;
}
public class ReceiverThread extends Thread {
#Override
public void run() {
super.run();
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
goalList.clear();
ArrayList<Goal>newGoalList = mDatabaseController.getAllGoals();
goalList.addAll(newGoalList);
adapter.notifyDataSetChanged();
}
});
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
if(resultCode == Activity.RESULT_OK){
Goal newGoal = new Goal();
String GoalName = data.getStringExtra(ARG_GOALNAME);
if (GoalName.isEmpty()) {
Toast.makeText(getContext(), R.string.NoGoal, Toast.LENGTH_SHORT).show();
} else {
newGoal.setmIsImportant(data.getBooleanExtra(ARG_IMPORTANT, false));
newGoal.setmGoalName(GoalName);
newGoal.setWhich_List(ARG_TITLE);
mDatabaseController.insertGoal(newGoal);
}
}
}else{
if(requestCode == 1){
if(resultCode == Activity.RESULT_OK){
String id = data.getStringExtra(ARG_ID);
Toast.makeText(getContext(),R.string.flush, Toast.LENGTH_LONG).show();
mDatabaseController.deleteGoal(id);
}
}else{
if(requestCode == 2){
if(resultCode == Activity.RESULT_OK){
String goalName = data.getStringExtra(ARG_GOALNAME);
Boolean isImportant = data.getBooleanExtra(ARG_IMPORTANT, false);
UUID id = UUID.fromString(data.getStringExtra(ARG_ID));
Goal goal = mDatabaseController.getGoal(id.toString());
goal.setmIsImportant(isImportant);
goal.setmGoalName(goalName);
mDatabaseController.updateGoal(goal);
}
}
}
}
UiThread.run();
}
public ReceiverThread getUiThread() {
return UiThread;
}
}
This is the list fragment that is created for every list in the database. It calls methods and manipulates data based off the user's actions.
public class RecyclerView_Adapter{
private ArrayList<Goal> mGoals;
MyAdapter adapter;
Context context;
private Database_Controller mDatabaseController;
MediaPlayer mediaPlayer;
Fragment fragment;
public RecyclerView_Adapter(ArrayList<Goal> goals, Context cntxt, Fragment fragments) {
mGoals = goals;
adapter = new MyAdapter(mGoals);
context = cntxt;
mediaPlayer = new MediaPlayer();
fragment = fragments;
mDatabaseController = Database_Controller.get(context);
}
//////////////////////////////////ADAPTER\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
private class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
private List<Goal> mGoals;
public MyAdapter(List<Goal> goals) {
mGoals = goals;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.recycler_view_item_layout, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final Goal goal = mGoals.get(position);
holder.bindGoal(goal);
}
#Override
public int getItemCount() {
return mGoals.size();
}
}
////////////////////////////////////ADAPTER\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
///////////////////////////////////VIEW HOLDER\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView mTextView, importantTXT;
private CheckBox mCheckBox;
private Goal mGoal;
private ImageView imgView;
public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AppCompatActivity context = (AppCompatActivity) v.getContext();
FragmentManager manager = context.getSupportFragmentManager();
Edit_PopUp dialog = Edit_PopUp.newInstance(mGoal);
dialog.setTargetFragment(fragment, 2);
dialog.show(manager, "EditDialog");
}
});
itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//Add delete thingy
AppCompatActivity context = (AppCompatActivity) v.getContext();
FragmentManager manager = context.getSupportFragmentManager();
MoreOptions_PopUp dialog = MoreOptions_PopUp.newInstance(mGoal);
dialog.setTargetFragment(fragment, 1);
dialog.show(manager, "GoalDialog");
return true;
}
});
mTextView = (TextView) itemView.findViewById(R.id.text);
mCheckBox = (CheckBox) itemView.findViewById(R.id.checkbox);
imgView = (ImageView) itemView.findViewById(R.id.imageView);
importantTXT = (TextView) itemView.findViewById(R.id.Important_textView);
mCheckBox.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
mGoal.setFinished(mCheckBox.isChecked());
mDatabaseController.updateGoal(mGoal);
if (mCheckBox.isChecked() == true) {
boolean value = checkWinning(mGoals,context);
PlaySound(value, mGoal.ismIsImportant(), v);
}
}
});
}
public void bindGoal(Goal goal) {
mGoal = goal;
mCheckBox.setChecked(mGoal.isFinished());
mTextView.setText(mGoal.getmGoalName());
if (mGoal.ismIsImportant()) {
imgView.setImageResource(R.mipmap.ic_stars_pressed);
importantTXT.setText(R.string.isImportant);
}
}
public boolean checkWinning(ArrayList<Goal> goals, Context context) {
for (int i = 0; i < goals.size(); i++) {
if (goals.get(i).isFinished() == false) {
return false;
}
}
return true;
}
public void PlaySound(Boolean winningSound, boolean isImportant, View view) {
if(winningSound){
mediaPlayer = MediaPlayer.create(context,R.raw.victory);
Snackbar snackbar = Snackbar.make(view, R.string.completion, Snackbar.LENGTH_LONG);
snackbar.show();
mediaPlayer.start();
}else {
if (isImportant) {
mediaPlayer = MediaPlayer.create(context, R.raw.collect);
mediaPlayer.start();
Snackbar snackbar = Snackbar.make(view, R.string.congrats, Snackbar.LENGTH_SHORT);
snackbar.show();
} else {
mediaPlayer = MediaPlayer.create(context, R.raw.success);
Snackbar snackbar = Snackbar.make(view, R.string.lessCongrats, Snackbar.LENGTH_SHORT);
snackbar.show();
mediaPlayer.start();
}
}
}
}
////////////////////////////////////VIEW HOLDER\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public MyAdapter getAdapter() {
return adapter;
}
public Fragment getFragment() {
return fragment;
}
}
Here is my adapter class that plays sounds OnClick and binds the view to the Recyclerview.
Any help would be greatly appreciated!
The problem with the stars displayed in every row will be caused because you might be setting them when needed but not hiding them when not needed.
You will have to implement something like this in the getView() method of your adapter.
ImageView starView = (ImageView) v.findViewById(R.id.starView);
starView.setVisibility(goals.get(position).isSpecial() ? View.Visible : View.Gone);
I am working on an application that I expect to migrate to multiple platforms. To facilitate the porting, I'm using Fragments to streamline the process. So what I have right now is a single Activity and several fragments. In one case I have several instances of the same fragment, with different data. Obviously each of these instances of the same fragment will use the same interface functions.
I've added the parameter "tag" to the interface function to identify each instance of fragment (using the tag used in creating the fragment). I guess one could also use the resource ID.
My question: Is this a good way to do this? Is there a better or more accepted method?
Activity code snippet:
public class SailboatRaceActivity extends FragmentActivity
implements SR_CommandsFragment.OnSR_CommandsSelectedListener,
SR_SubCommands_Fragment.OnSR_SubCommandsSelectedListener,
FleetListFragment.FleetList_WidthChange,
FleetListFragment.FleetListChange,
FleetScoringFragment.OnFS_SelectedListener,
FleetSpinnerFragment.FleetSpinner_Selection {
// State Variables
private int mSubCommandPositon;
private int mCommandPosition;
private int mFleetSelection;
private int mFleetListColumnWidth = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mSubCommandPositon = savedInstanceState.getInt("mSubCommandPositon");
mCommandPosition = savedInstanceState.getInt("mCommandPosition");
mFleetSelection = savedInstanceState.getInt("mFleetSelection");
mFleetListColumnWidth = savedInstanceState.getInt("mFleetListColumnWidth");
}
setContentView(R.layout.sailboat_race_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
}
public void onSR_CommandSelected(int position) {
SR_SubCommands_Fragment SR_SubCFrag = (SR_SubCommands_Fragment) getFragmentManager().findFragmentById(R.id.subcommands);
mCommandPosition = position;
if (SR_SubCFrag != null) {
SR_SubCFrag.update_SR_subcommands(position);
SR_SubCFrag.ChangeListViewWidth(mFleetListColumnWidth);
}
}
public void onSR_SubCommandSelected(int position) {
mSubCommandPositon = position;
FleetScoringFragment FSFrag = (FleetScoringFragment) getFragmentManager().findFragmentById(R.id.fleet_scoring);
if (FSFrag != null) {
FSFrag.update_commands(mCommandPosition, mSubCommandPositon);
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt("mSubCommandPositon", mSubCommandPositon);
outState.putInt("mCommandPosition", mCommandPosition);
outState.putInt("mFleetSelection", mFleetSelection);
outState.putInt("mFleetListColumnWidth", mFleetListColumnWidth);
super.onSaveInstanceState(outState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void FleetList_WidthChange(int width) {
mFleetListColumnWidth = width;
// can we set width of other two lists
SR_SubCommands_Fragment SR_SubCFrag = (SR_SubCommands_Fragment) getFragmentManager().findFragmentById(R.id.subcommands);
if (SR_SubCFrag != null) {
SR_SubCFrag.ChangeListViewWidth(width);
}
SR_CommandsFragment SR_CFrag = (SR_CommandsFragment) getFragmentManager().findFragmentById(R.id.commands);
if (SR_CFrag != null) {
SR_CFrag.ChangeListViewWidth(width);
}
}
#Override
public void FleetListChange(int position) {
FleetScoringFragment FSFrag = (FleetScoringFragment) getFragmentManager().findFragmentById(R.id.fleet_scoring);
if (FSFrag != null) {
mFleetSelection = position;
FSFrag.update_FleetScoringList(position);
}
FleetScoringHeaderFragment FSHFrag = (FleetScoringHeaderFragment) getFragmentManager().findFragmentById(R.id.scoring_header);
if (FSHFrag != null) {
mFleetSelection = position;
FSHFrag.update_FleetScoringHeader(position);
}
}
#Override
public void onFS_Selected(int position) {
}
#Override
public void FSpinner_selection(int selection, String tag) {
int fs = selection;
String thistag =tag;
// do something
}
The FSpinner_selection interface is the one I'm working on. I set this implementation just to check if the data was valid.
FleetSpinner snippet:
public class FleetSpinnerFragment extends Fragment {
private int nPresentFleetPosition;
public ArrayList<FleetItem> mListArray = new ArrayList<FleetItem>();
FleetSpinnerAdapter adapter;
FleetSpinner_Selection mCallback;
Spinner spinner;
String tag;
public interface FleetSpinner_Selection {
public void FSpinner_selection(int selection, String tag);
}
public FleetSpinnerFragment() {
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (FleetSpinner_Selection) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement FSpinner_Selection");
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
nPresentFleetPosition = 0;
for (int i = 0; i<Fleet.fleetbit.length;i++) {
mListArray.add(Fleet.fleetbit[i]);
}
} else {
nPresentFleetPosition = savedInstanceState.getInt("nPresentFleetPosition");
ArrayList temp = savedInstanceState.getParcelableArrayList("thisFleetList");
mListArray.addAll(temp);
}
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("nPresentFleetPosition", nPresentFleetPosition);
ArrayList<FleetItem> temp = new ArrayList<FleetItem>();
for (int i =0 ; i< adapter.getCount();i++){
temp.add(adapter.getItem(i));
}
savedInstanceState.putParcelableArrayList("thisFleetList", temp);
super.onSaveInstanceState(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = new View(getActivity());
adapter = new FleetSpinnerAdapter(getActivity(), R.layout.fleet_list_item);
spinner = new Spinner(getActivity());
spinner.setAdapter(adapter);
adapter.addAll(Fleet.fleetbit);
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
tag = getTag();
spinner.setSelection(nPresentFleetPosition);
return spinner;
}
public void update_FleetScoringHeader(int position) {
nPresentFleetPosition = position;
FrameLayout FLScoringHeader = (FrameLayout) getActivity().findViewById(R.id.scoring_header);
TextView v1 = (TextView) FLScoringHeader.findViewById(R.id.fleet_list_item_textview);
v1.setText(Fleet.fleetbit[nPresentFleetPosition].name);
ImageView v0 = (ImageView) FLScoringHeader.findViewById(R.id.fleet_list_item_icon);
v0.setImageResource(Fleet.fleetbit[nPresentFleetPosition].id);
}
static class FleetHolder {
ImageView imgIcon;
TextView txtTitle;
}
public class CustomOnItemSelectedListener extends Activity implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
FleetItem temp = (FleetItem) parent.getItemAtPosition(position);
nPresentFleetPosition = position;
mCallback.FSpinner_selection(position, tag);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
I'm just starting to learn android programming, so this may be a really noob question.
I am attempting to enact the Parcelable pattern to save and restore my ListView data on orientation changes.
The problem is, the ListView will only update once I do an orientation change. It will not update otherwise. It used to work just fine, until I started implementing Parcelable.
All help is greatly appreciated!
My fragment class is copied below, but can also be accessed here on GitHub:
public class TrackActivityFragment extends Fragment {
private IconicAdapter trackResultListViewAdapter = null;
private String artistId = null;
private final String LOG_TAG = TrackActivityFragment.class.getSimpleName();
public TrackActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ArrayList<TrackParcelable> trackParcelables = null;
View rootView = inflater.inflate(R.layout.fragment_track, container, false);
Intent intent = getActivity().getIntent();
if(intent != null && intent.hasExtra(Intent.EXTRA_SHORTCUT_NAME)) {
artistId = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
}
if(savedInstanceState == null || !savedInstanceState.containsKey("tracks_key")) {
trackParcelables = new ArrayList<TrackParcelable>();
performSearch(artistId);
}
else {
trackParcelables = savedInstanceState.getParcelableArrayList("tracks_key");
}
ListView listView = (ListView) rootView.findViewById(R.id.listViewOfTopTracks);
trackResultListViewAdapter = new IconicAdapter(trackParcelables,
getTrackNamesFromParcelables(trackParcelables));
listView.setAdapter(trackResultListViewAdapter);
return rootView;
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putParcelableArrayList("tracks_key",
trackResultListViewAdapter.getTrackParcelables());
super.onSaveInstanceState(outState);
}
private ArrayList<String> getTrackNamesFromParcelables(ArrayList<TrackParcelable>
trackParcelables){
ArrayList<String> trackNames = new ArrayList<>();
for(TrackParcelable element : trackParcelables){
trackNames.add(element.name);
}
return trackNames;
}
private void performSearch(String artistId) {
SpotifyApi api = new SpotifyApi();
SpotifyService spotify = api.getService();
Map<String, Object> options = new HashMap<>();
options.put("country", "US");
spotify.getArtistTopTrack(artistId, options, new Callback<Tracks>() {
#Override
public void success(Tracks tracks, Response response) {
final ArrayList<TrackParcelable> trackParcelables =
new ArrayList<TrackParcelable>();
for (Track track : tracks.tracks) {
trackParcelables.add(new TrackParcelable(track.name,track.album.name,
track.album.images.get(0).url,track.preview_url));
}
trackResultListViewAdapter.swapItems(trackParcelables);
Log.d(LOG_TAG,trackParcelables.toString());
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
if(trackParcelables.size()==0){
Toast.makeText(getActivity(),
getString(R.string.no_tracks_found_toast),
Toast.LENGTH_SHORT).show();
}
trackResultListViewAdapter.notifyDataSetChanged();
}
});
}
#Override
public void failure(RetrofitError error) {
}
});
}
//This ViewHolder Pattern is from Busy Android Coder's Guide page 274 of book version 6.7
class ViewHolder {
ImageView icon=null;
TextView trackName=null;
TextView trackAlbum=null;
ViewHolder(View row) {
this.icon = (ImageView)row.findViewById(R.id.imageViewAlbum);
this.trackName = (TextView)row.findViewById(R.id.textViewTrackTitle);
this.trackAlbum = (TextView)row.findViewById(R.id.textViewTrackAlbum);
}
}
//This IconicAdapter Pattern is from Busy Android Coder's Guide page 272 of book version 6.7
class IconicAdapter extends ArrayAdapter<String> {
private ArrayList<TrackParcelable> trackParcelables;
public IconicAdapter(ArrayList<TrackParcelable> trackParcelables,
ArrayList<String> trackNames) {
super(getActivity(), R.layout.list_item_top_tracks, R.id.textViewTrackTitle
, trackNames);
this.trackParcelables = trackParcelables;
}
public void swapItems(ArrayList<TrackParcelable> trackParcelables) {
this.trackParcelables = trackParcelables;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
ViewHolder holder = (ViewHolder)row.getTag();
if (holder==null) {
holder=new ViewHolder(row);
row.setTag(holder);
}
Picasso.with(getActivity()).load(trackParcelables.get(position).albumImageUrl)
.into(holder.icon);
TextView trackAlbumTextView = (TextView)row.findViewById(R.id.textViewTrackAlbum);
trackAlbumTextView.setText(trackParcelables.get(position).albumName);
return row;
}
public ArrayList<TrackParcelable> getTrackParcelables(){
return trackParcelables;
}
}
The issue is that IconicAdapter is only tracking changes in trackNames, not in ArrayList <TrackParcelable> trackParcelables.
To fix this, I added a a member variable in my IconicAdapter for the trackNames and set this in the constructor like so:
private ArrayList<TrackParcelable> trackParcelables;
private ArrayList <String> trackNames;
public IconicAdapter(ArrayList<TrackParcelable> trackParcelables,
ArrayList<String> trackNames) {
super(getActivity(), R.layout.list_item_top_tracks, R.id.textViewTrackTitle
, trackNames);
this.trackNames = trackNames;
this.trackParcelables = trackParcelables;
}
Then, I edited my swapItems to update my ListView data like so:
public void swapItems(ArrayList<TrackParcelable> trackParcelables) {
this.trackParcelables = trackParcelables;
trackNames.clear();
trackNames.addAll(getTrackNamesFromParcelables(trackParcelables));
}