I am just new to android and I've been researching for this for weeks but I still cannot find a way to make it work. :(
I have 4 fragment tabs (created dynamically) in my Main Activity. I enter an amount from a prompt in Main Activity and save it to database. I want to update the value in the first tab after I press the OK button from the prompt to get the amount saved into the database and display it on the first tab fragment.
How can I refresh the values in the first tab?
in my fragment I have this code to display the data. But this only displays the data initially, not after I enter the amount from the Main Activity.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootview = inflater.inflate(R.layout.home_tab, container, false);
super.onCreate(savedInstanceState);
myDb = new DatabaseHelper(getContext());
viewBudget(rootview);
return rootview;
}
public View viewBudget(final View rootview) {
//get values from database and display in dynamic views
}
I really appreciate your answers. Thank you in advance.
EDIT: Here is my complete code for viewBudget:
public View viewBudget(final View rootview) { //display funds dynamically in config layout | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState
//Cursor res = myDb.getAllData();
//String[] textArray = {"One", "Two", "Three", "Four"};
Double budget, totalpct, temp_pct;
Cursor res2 = myDb.getConfigData();
res2.moveToFirst();
Cursor res3 = myDb.getLatestIncome();
res3.moveToFirst();
//showMessage("Number of rows",Integer.toString(res2.getCount()));
Cursor res4 = myDb.getTotalExpenes();
res4.moveToFirst();
//view contents of config table
if (res2.getCount() == 0) {
// show message
//showMessage("Error","Nothing found");
//return;
}
final ScrollView scrollView = (ScrollView) rootview.findViewById(R.id.scroll_budget);
LinearLayout linearLayout = (LinearLayout) rootview.findViewById(R.id.ll_budget);
LinearLayout llh_texts = new LinearLayout(getActivity());
llh_texts.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout llh_texts2 = new LinearLayout(getActivity());
llh_texts2.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout llh_texts3 = new LinearLayout(getActivity());
llh_texts3.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout llh_texts4 = new LinearLayout(getActivity());
llh_texts4.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams lp_llh_texts = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
llh_texts.setLayoutParams(lp_llh_texts);
//final View linearLayout = getActivity().findViewById(R.id.ll_config);
linearLayout.removeAllViews(); //clear layout first - LINE WITH ISSUE
linearLayout.setGravity(Gravity.CENTER);
//LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
//LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//compute for value of budget
budget = 0.0;
totalpct = 0.0;
linearLayout.addView(llh_texts); //add button h.linearlayout to parent linearlayout inside scrollview
TextView textBudget = new TextView(getActivity());
TextView labelBudget = new TextView(getActivity());
labelBudget.setText("Budget: ");
llh_texts.addView(labelBudget);
llh_texts.addView(textBudget);
TextView textIncome = new TextView(getActivity());
TextView labelIncome = new TextView(getActivity());
labelIncome.setText("Income: ");
linearLayout.addView(llh_texts2);
llh_texts2.addView(labelIncome);
llh_texts2.addView(textIncome);
TextView textTotalFunds = new TextView(getActivity());
TextView labelFunds = new TextView(getActivity());
labelFunds.setText("Total Savings Funds: ");
linearLayout.addView(llh_texts3);
llh_texts3.addView(labelFunds);
llh_texts3.addView(textTotalFunds);
TextView textTotalExpenses = new TextView(getActivity());
TextView labelExpenses = new TextView(getActivity());
labelExpenses.setText("Total Scheduled Expenses: ");
linearLayout.addView(llh_texts4);
llh_texts4.addView(labelExpenses);
llh_texts4.addView(textTotalExpenses);
//create dynamic objects inside scrollview and dynamic linear layout - horizontal
for (int i = 0; i < res2.getCount(); i++) {
LinearLayout llh = new LinearLayout(getActivity());
llh.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams lp_llh = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
llh.setLayoutParams(lp_llh);
//llh.setLayoutParams(lp4,llh.setOrientation(LinearLayout.HORIZONTAL); //for length and width
linearLayout.addView(llh);
//LinearLayout.LayoutParams lp_np = new LinearLayout.LayoutParams(70, LinearLayout.LayoutParams.WRAP_CONTENT);
temp_pct = (Double.valueOf(res2.getString(2)) / 100) * Double.valueOf(res3.getString(3));
EditText editText = new EditText(getActivity());
editText.setText(String.valueOf(temp_pct));
editText.setEnabled(false);
EditText editText2 = new EditText(getActivity());
editText2.setText(String.valueOf(res2.getString(2)) + "%");
editText2.setEnabled(false);
//get total of funds
totalpct = totalpct + temp_pct;
//showMessage("value",res2.getString(3));
TextView textView = new TextView(getActivity());
textView.setText(res2.getString(1));
llh.addView(textView);
llh.addView(editText);
llh.addView(editText2);
res2.moveToNext();
}
//return scrollView;
//create budget text and content
budget = Double.valueOf(res3.getString(3)) - (totalpct + res4.getDouble(0));
textBudget.setText(String.valueOf(budget));
textIncome.setText(String.valueOf(res3.getString(3)));
textTotalFunds.setText(String.valueOf(totalpct));
textTotalExpenses.setText(String.valueOf(res4.getDouble(0)));
return rootview;
}
EDIT: Added code to 3rd fragment tab based on Bhuvanesh's answer:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true); //Make sure you have this line of code.
setUserVisibleHint(false);
}
//@Override /*<-- this returned "method does not override method in its superclass" so I commented out*/
public void setUserVisibleHint(final View view, boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
//get data from database and refresh view.
viewFunds(view); //this method refreshes the rootview of the 3rd fragment
}
}
@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((Main2Activity) getActivity()).setPopupListener(new Main2Activity.PopupListener() {
@Override
public void onDialogClick(String value) {
//After clicking dialog ok button in Activity
// you will get value here.
//viewFunds(view);
setUserVisibleHint(view, true);
}
});
}