0

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!

Rob
  • 1,162
  • 2
  • 18
  • 43

3 Answers3

0

Try this

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {


    if (resultCode == RESULT_OK) {
        //changeFragmentTextView(Integer.toString(db.getSum()));
    }

    if (resultCode == RESULT_CANCELED) {

    }
    pagerAdapter.notifyDataSetChanged(); // This is not working
    super.onActivityResult(requestCode, resultCode, data);
}
Punit
  • 667
  • 1
  • 7
  • 17
  • No luck same thing. When I change the value, it doesnt actually update until i close and reopen. – Rob Jul 09 '14 at 20:33
0

Then try this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {


if (resultCode == RESULT_OK) {
    //changeFragmentTextView(Integer.toString(db.getSum()));
}

if (resultCode == RESULT_CANCELED) {

}
pagerAdapter = new MyFragmentPagerAdapter(fm, getApplicationContext());
    pagerAdapter.setCount(setTabCount());
    pagerAdapter.setNames(setTabNames());

    pager.setAdapter(pagerAdapter);

}

But you will need to declare the pager, and your fm as member variables (declare them on class no inside onCreate method)

Ringo
  • 850
  • 9
  • 24
  • setTabCount() and setTabNames() dont actually set the data in the fragment. Those just set the ViewPager tab information. You still want me to try that? The actual data in the fragment is set within the pageradapter and then sent to the fragment during onCreate – Rob Jul 09 '14 at 21:05
  • what happens if you call onResume under notify data set changed? what may be happening is a conflict between the lifecycle methods called on your activity and those that are under the fragment. try calling getActivity().onResume too, if possible, havent tried that myself – Ringo Jul 09 '14 at 21:14
0

This worked for me:

holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder((Activity) v.getContext());
                alertDialog.setMessage("Are you sure you want to delete?")
                        .setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener(){
                            public void onClick(DialogInterface dialog, int id){
                                db.deleteTransaction(table, deleteId);
                                cursor = db.getTransactions(table);
                                notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener(){
                            public void onClick(DialogInterface dialog, int id){
                                dialog.cancel();
                            }
                        });
                AlertDialog alert = alertDialog.create();
                alert.show();
Rob
  • 1,162
  • 2
  • 18
  • 43