Update AChartEngine chart in fragment - android

When I change spinner value, chart is updated on TAB2 but not in TAB1 even if the TAB1 is active and showed on the screen.
Here is my code...
onCreateView:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_charts_line, container, false);
layoutChart = (LinearLayout) rootView.findViewById(R.id.chart);
...all renderer settings...
return rootView;
onViewCreated:
public void onViewCreated(View view, Bundle savedInstanceState){
Toolbar mToolbar = (Toolbar) getActivity().findViewById(R.id.toolbar_actionbar);
final Spinner spinner_nav = (Spinner) mToolbar.findViewById(R.id.spinner_nav);
spinner_nav.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
cSpinner = spinner_nav.getSelectedItem().toString();
showChart(cSpinner);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
cSpinner= spinner_nav.getSelectedItem().toString();
showChart(cSpinner);
}
showChart:
public void showChart(String account){
if (mChartView == null) {
LinearLayout layout = (LinearLayout) layoutChart.findViewById(R.id.chart);
mChartView = ChartFactory.getTimeChartView(layoutChart.getContext().getApplicationContext(), mDataset, mRenderer, "MMM yyyy");
layout.addView(mChartView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
} else {
mChartView.repaint();
}
}
What I want is to update all charts in every tab, or at least to update chart in current tab. Every variable name is different in every tab Fragment.

Related

How to make BottomSheetDialogFragment cover the full screen?

I'm using BottomSheetDialogFragment to show some data.But when I'm starting the fragment it's appearing 50% of the screen .So, my question is how to make it full screen when it shows.
BottomSheetDialogFragment Code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.bot_frag, container, false);
TextView tv = v.findViewById(R.id.textVi);
back=v.findViewById(R.id.back_of_bot);
back.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
}
);
return v;
}
You can use dialog fragments, plz refer this:
public class DialogFragments extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(R.layout.dialog_dialogfragment_layout, null);
getDialog().setTitle("Title");
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
getDialog().getWindow().setGravity(Gravity.BOTTOM);
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, (int) (metrics.heightPixels * 0.30));// here i have fragment height 30% of window's height you can set it as per your requirement
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimationUpDown;
}
and when you want to open,open Bottomsheet dialog like this way :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.bot_frag, container, false);
TextView tv = v.findViewById(R.id.textVi);
back=v.findViewById(R.id.back_of_bot);
back.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
DialogFragments dialogFragment = new DialogFragments(this);
dialogFragment.show(fm, "Bottomsheet Fragment");
}
}
);
return v;
}
You have to apply this style to you BottomSheetDialogFragment
android.R.style.Theme_Material_Light_NoActionBar_Fullscreen

communicate from dialog fragment to fragment

I have two fragments:AllItemsFragment, CreateItemDialogFragment
The AllItemsFragment displays a list of items in the sqlite,CreateItemDialogFragment is used to create the item to the SQLite.
Now I have a question is after I creating a item in createItemDialogFragment,the dialog dismiss, how to update the display of the items in AllItemsFragment
In AllItemsFragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(LIFETAG, "onCreateView");
View view = inflater.inflate(R.layout.fragment_all_items, container, false);
ButterKnife.bind(this, view);
tvTitle.setText("All Items");
DbHandler dbHandler = new DbHandler(getActivity(),null,null,1);
ArrayList<Item> items = dbHandler.getAllItems();
AllItemsAdapter adapter = new AllItemsAdapter(getActivity(),items);
lvItems.setAdapter(adapter);
return view;
}
In CreateItemDialogFragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
View view =inflater.inflate(R.layout.fragment_create_item_dialog,null);
ButterKnife.bind(this, view);
itemPrice.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
return view;
}
#OnClick(R.id.clear)
void clearDialog(){
dismiss();
}
//create item
#OnClick(R.id.save)
void saveItem() {
if(!itemName.getText().equals(null)||!itemPrice.getText().equals(null)){
String name = itemName.getText().toString();
String priceStr = itemPrice.getText().toString();
Double price = Double.parseDouble(priceStr);
Item item = new Item(name,price);
DbHandler dbHandler = new DbHandler(getActivity(),null,null,1);
dbHandler.addItems(item);
dismiss();
}
}
Try to update the data on onResume callback of the AllItemsFragment.
#Override
public void onResume(){
super.onResume();
// update data over here.
}

Refreshing RecyclerView Between Two Tabs

I have two tabs in my app. So when I delete a list from the RecyclerView of one tab it should be added to the RecyclerView. I have deleted the list from the 1st tab and added it to the list of 2nd tab's recyclerview. But, the recyclerview doesnot take the change. When I go to next activity and come back to the tabs, then it shows the change.
In short I just need to update the data sent to the adapter.
I have already used
notifyDataSetChanged() and swapAdapter()
but did not work.
Do anyone know who to make it work ?
Thank you
!
Tab 1 Fragment :
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_active_patients, container, false);
activePatientsList = (RecyclerView) view.findViewById(R.id.active_patients_recyclerview);
activePatientsList.setLayoutManager(new LinearLayoutManager(getActivity()));
final ActivePatientRecyclerAdapter activePatientRecyclerAdapter = new ActivePatientRecyclerAdapter(getActivity()
,activePatientList);
activePatientRecyclerAdapter.setOptionsCLickedListener(new ActivePatientRecyclerAdapter.OptionsCLickedListener() {
#Override
public void popUpClicked(final View view, final int position) {
PopupMenu popupMenu = new PopupMenu(getActivity(), view);
MenuInflater menuInflater = popupMenu.getMenuInflater();
menuInflater.inflate(R.menu.menu_active_patients_additional_options,
popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
int id = menuItem.getItemId();
if (id == R.id.action_discharge) {
SQLiteDBUse db = new SQLiteDBUse(getActivity());
long result = db.updateName("4", in_patients_name[position]);
if (result > -1) {
Snackbar.make(view, in_patients_name[position] + " Removed", Snackbar.LENGTH_SHORT).show();
activePatientList.remove(position);
activePatientRecyclerAdapter.notifyItemRemoved(position);
}
return false;
}
return false;
}
;
});
popupMenu.show();
}
});
activePatientsList.setAdapter(activePatientRecyclerAdapter);
return view;
}
Tab 2 Fragment :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_out_patient, container, false);
outPatientList = (RecyclerView) view.findViewById(R.id.out_patients_recyclerview);
outPatientList.setLayoutManager(new LinearLayoutManager(getActivity()));
patientListRecyclerAdapter = new PatientListRecyclerAdapter(getActivity()
, patientList);
}
});
outPatientList.setAdapter(patientListRecyclerAdapter);
return view;
}
#Override
public void onResume() {
super.onResume();
patientListRecyclerAdapter.notifyDataSetChanged();
}

How do I replace current fragment after clicking a Gridview inside in tabhost of another Fragment?

I have this problem. I have a Fragment with a ViewFlipper and a TabHost with inside three tabs and in the first tab I have a Gridview with Items. I want to replace the current fragment with another by clicking an item of the gridView.
The relevant sample code that I written:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.princ_fragment_2, container,
false);
//mTabHost = rootView.getTabHost();
mTabHost = (TabHost)rootView.findViewById(R.id.tabhost);
mTabHost.setup();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Turnario").setContent(R.id.gridView));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("CarSharing").setContent(R.id.textview2));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("Altro").setContent(R.id.textview3));
mTabHost.setCurrentTab(0);
mContext = this.getActivity();
mViewFlipper = (ViewFlipper) rootView.findViewById(R.id.ViewFlipper01);
mViewFlipper.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(final View view, final MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
});
**gridView = (GridView) rootView.findViewById(R.id.gridView);
customGridAdapter = new GridViewAdapter(this.getActivity(), R.layout.row_grid, getData());
gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new GridView.OnItemClickListener() {
***public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getActivity(),null, Toast.LENGTH_SHORT).show();
Log.v(TAG,"GRIDVIEW ITEM TOUCH");
}
});***
return rootView;
}**
I debugging this piece of code, but the event onItemClick() is not execute.
Thanks for answer.
if (position == Constants.GRID_POSITION_ZERO) {
FragmentHelper fragmentHelper = FragmentHelper.getInstance();
fragmentHelper.replaceFragment(R.id.fragment_holder, new LiveEventFragment(), ((HomeActivity) getActivity()), "Live_Event");
}
fragment helper is a class where u can mange all fragment operations

add a header to fragmentlist android

I'm adding a header to my ListFragment but header doesn't appear. I tried add in onCreate and onStart but I already set my adapter. I found here this Best place to addHeaderView in ListFragment but it is not valid.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = getActivity().getLayoutInflater().inflate(R.layout.header,
null);
container.addView(v,0);
rla = new RowListAdapter(getActivity().getLayoutInflater(),
R.layout.itemlayout, rl);
setListAdapter(rla);
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onStart() {
super.onStart();
getListView().setBackgroundResource(R.color.tostadoclaro);
getListView()
.setDivider(getResources().getDrawable(R.drawable.divider));
getListView().setDividerHeight(4);
}
¡¡¡SOLUTION!!!
#Override
public void onActivityCreated(Bundle savedInstanceState) {
View v = getActivity().getLayoutInflater().inflate(R.layout.header,
null);
this.getListView().addHeaderView(v);
rla = new RowListAdapter(getActivity().getLayoutInflater(),
R.layout.itemlayout, rl);
setListAdapter(rla);
super.onActivityCreated(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){// Empty. Use original.}
Don't add the view to the container. Use addHeaderView(v).
The complete code (adapted from here):
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView tv = new TextView(getActivity());
tv.setText("Hello");
getListView().addHeaderView(tv);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
new String[] { "Hello world" }));
}
#Override
public void onDestroyView() {
setListAdapter(null);
super.onDestroyView();
}
See also: Best place to addHeaderView in ListFragment

Categories

Resources