Updating the ListView in a Fragment from a Dialog in FragmentActivity(using a ViewPager)

风格不统一 提交于 2019-12-24 08:56:23

问题


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:


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


来源:https://stackoverflow.com/questions/13729451/updating-the-listview-in-a-fragment-from-a-dialog-in-fragmentactivityusing-a-vi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!