In my Main activity I am adding a value to the database which then updates the total value on the fragment displayed. When it adds the value, the textview is not updating until I close the application/activity and reopen it.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Typeface tf = Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf");
pager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
pagerAdapter = new MyFragmentPagerAdapter(fm, getApplicationContext());
pagerAdapter.setCount(setTabCount());
pagerAdapter.setNames(setTabNames());
pager.setAdapter(pagerAdapter);
tvWithdraw = (TextView) findViewById(R.id.tvWithdraw);
tvDeposit = (TextView) findViewById(R.id.tvDeposit);
tvWithdraw.setTypeface(tf);
tvDeposit.setTypeface(tf);
tvWithdraw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tabName = "[" + pagerAdapter.getPageTitle(pager.getCurrentItem()).toString() + "]";
Intent i = new Intent(v.getContext(), AmountDialogActivity.class);
i.putExtra("type", "debit");
i.putExtra("table", tabName);
startActivityForResult(i, RESULT_OK);
}
});
tvDeposit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tabName = "[" + pagerAdapter.getPageTitle(pager.getCurrentItem()).toString() + "]";
Intent i = new Intent(v.getContext(), AmountDialogActivity.class);
i.putExtra("type", "credit");
i.putExtra("table", tabName);
startActivityForResult(i, RESULT_OK);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
//changeFragmentTextView(Integer.toString(db.getSum()));
}
if (resultCode == RESULT_CANCELED) {
}
pagerAdapter.notifyDataSetChanged(); // This is not working
}
Here is my adapter
public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
int listSize;
String[] tabNames;
String tabName;
protected Context mContext;
/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm, Context c) {
super(fm);
mContext = c;
}
/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {
MainFragment mainFragment = new MainFragment();
final DatabaseHelper db = new DatabaseHelper(mContext);
tabName = "[" + getPageTitle(arg0) + "]";
Double balance = db.getSum(tabName);
Bundle data = new Bundle();
data.putInt("current_page", arg0 + 1);
data.putString("tabName", tabName);
data.putDouble("balance", balance);
mainFragment.setArguments(data);
return mainFragment;
}
/** Returns the number of pages */
@Override
public int getCount() {
return listSize;
}
@Override
public CharSequence getPageTitle(int position) {
return tabNames[position];
}
public void setCount(int count){
listSize = count;
}
public void setNames(String[] names) {
tabNames = names;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
I have read several entries on getting this to work but for some reason I cannot seem to get any to actually work on my scenario. If anyone needs more information, please let me know.
Thanks!