Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8942179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:27:34+00:00 2026-06-15T11:27:34+00:00

I have a FragmentActivity (main) which creates 3 Fragments and also a menu. Pretty

  • 0

I have a FragmentActivity (main) which creates 3 Fragments and also a menu. Pretty straight forward, and from the examples in the Android SDK.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dbh = new DBHelper(this);

    // Create the adapter that will return a fragment for each of the
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
}

// code //


@Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        switch(position){
            case 0:
                fragment = new DaySectionFragment(Main.this, dbh);
                break;
            case 1:
                fragment = new WeekSectionFragment(Main.this, dbh);
                break;
            case 2:
                fragment = new FoodListSectionFragment(Main.this, dbh);
                break;
            case 3:
                fragment = new AboutSectionFragment();
                break;
        }

        return fragment;
    }
//more code

From the menu in the main activity I have a dialog with an editText. The value from this textfield is suppose to be stored in a database, which works fine, and also pop up in the listview in the fragment (not i ListFragment, but a fragment with a listview in it). The simple way would be to call notifyDataSetChanged() on the ListView adapter. However I can’t do that.

This is the fragment with the ListView:

public class FoodListSectionFragment extends Fragment {
private Context context;
private DBHelper dbh;
private ArrayList<FoodData> listItems = new ArrayList<FoodData>();
private FoodAdapter adapterFoodList;
private AdapterView.AdapterContextMenuInfo adapterInfo;
private ListView lvItems;

public FoodListSectionFragment() {
}
public FoodListSectionFragment(Context context, DBHelper dbh) {
    this.context = context;
    this.dbh = dbh;
    //setTag("FoodListFragment");
}
public void updateList(){
    adapterFoodList.notifyDataSetChanged();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View myView = getLayoutInflater(null).inflate(R.layout.food_list, null);

    listItems.clear();
    listItems = (ArrayList<FoodData>) dbh.selectAllFood();

    adapterFoodList = new FoodAdapter(context, R.layout.list_row_food, listItems);

    lvItems = (ListView)myView.findViewById(R.id.listFood);
    lvItems.setAdapter(adapterFoodList);
    adapterFoodList.notifyDataSetChanged();

    return myView;
}
}

Here is where I’m trying to update the ListView, although this won’t work.

dialogAddFood = new Dialog(Main.this);
            dialogAddFood.setContentView(R.layout.dialog_add_food);
            dialogAddFood.setTitle(R.string.menu_add_food);
            dialogAddFood.setCancelable(true);

            Button btnSave = (Button) dialogAddFood.findViewById(R.id.btnSave);
            btnSave.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText edtFood = (EditText)dialogAddFood.findViewById(R.id.edtFood);
                    RatingBar ratingGrade = (RatingBar)dialogAddFood.findViewById(R.id.ratingGrade);
                    RatingBar ratingDiff = (RatingBar)dialogAddFood.findViewById(R.id.ratingDiff);

                    if(edtFood.getText().toString().length() > 0){
                        dbh.insertFood(edtFood.getText().toString(), (int)ratingGrade.getRating(), (int)ratingDiff.getRating());
                        Toast.makeText(Main.this, "Maträtt tillagd", Toast.LENGTH_LONG).show();

                        //FoodListSectionFragment f = (FoodListSectionFragment)Main.this.getSupportFragmentManager().findFragmentByTag("FoodList");
                        //f.updateList();

                        ListView l = (ListView)mSectionsPagerAdapter.getItem(2).getView().findViewById(R.id.listFood);
                        FoodAdapter a = (FoodAdapter)l.getAdapter();
                        a.notifyDataSetChanged();

                    }

                    dialogAddFood.cancel();
                }
            });
            Button btnCancel = (Button) dialogAddFood.findViewById(R.id.btnCancel);
            btnCancel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialogAddFood.cancel();
                }
            });
            dialogAddFood.show();

Help, please.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:27:34+00:00Added an answer on June 15, 2026 at 11:27 am

    Your code doesn’t work because the line:

    mSectionsPagerAdapter.getItem(2)
    

    doesn’t return the Fragment at position 2 from the ViewPager, it creates a new Fragment for that position and calling update methods on this Fragment instance will obviously not make any changes as this Fragment isn’t attached to your Activity(is not the visible one).

    Try to look for that Fragment using the FragmentManager like below and see how it goes:

    // ...
    Toast.makeText(Main.this, "Maträtt tillagd", Toast.LENGTH_LONG).show();
    
    //FoodListSectionFragment f = (FoodListSectionFragment)Main.this.getSupportFragmentManager().findFragmentByTag("FoodList");
    //f.updateList();
    FoodListSectionFragment fr = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.theIdOfTheViewPager + ":2");
    if (fr != null && fr.getView() != null) {
        fr.updateList();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using fragments to show images/pages.I have one Activity(Main) which contains all the
I have a FragmentActivity which sets a main layout with 3 tabs. Each tab
I have a FragmentActivity using a ViewPager to serve several fragments. Each is a
I have a parent FragmentActivity with three child Tab Fragments. When a Submit button
I have a fragment where I wish to call a method from the FragmentActivity
I have a FragmentActivity with a FragmentPagerAdapter which holds a number of Fragment objects
I have a FragmentActivity as my main activity. I am trying to call up
In my app, I have only one Activity which hosts several fragments . The
I have a FragmentActivity support v4 class which implements two side by side (kind
I have a tabhost with 5 tabs which are FragmentActivity . One of them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.