PagerAdapter notifyDataSetChanged()不更新片段

时间:2014-07-09 20:10:48

标签: android android-fragments notifydatasetchanged fragmentstatepageradapter

在我的Main活动中,我向数据库添加一个值,然后更新显示的片段的总值。当它添加值时,textview不会更新,直到我关闭应用程序/活动并重新打开它。

@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
    }

这是我的适配器

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;
    }
}

我已经阅读了几个关于让它工作的条目,但由于某种原因,我似乎无法在我的方案中实际工作。如果有人需要更多信息,请告诉我。

谢谢!

3 个答案:

答案 0 :(得分:0)

试试这个

@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);
}

答案 1 :(得分:0)

然后试试这个:

@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);

}

但是你需要声明寻呼机,并将你的fm声明为成员变量(在o​​nCreate方法中的类no声明它们)

答案 2 :(得分:0)

这对我有用:

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();