1

我有一个FragmentActivity(主)创建 3Fragments和一个菜单。非常简单,来自 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

从主要活动的菜单中,我有一个带有editText的对话框。假设该文本字段的值存储在数据库中,该数据库运行良好,并且还会在片段的列表视图中弹出(不是我的 ListFragment,而是其中包含列表视图的片段)。简单的方法是在 ListView 适配器上调用 notifyDataSetChanged()。但是我不能那样做。

这是带有 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;
}
}

这是我尝试更新 ListView 的地方,尽管这不起作用。

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

请帮忙。

4

1 回答 1

2

您的代码不起作用,因为该行:

mSectionsPagerAdapter.getItem(2)

不会Fragment从 2 返回 at 位置ViewPager,它会为该位置创建一个新的,并且在此实例上调用 update 方法显然不会进行任何更改,因为它没有附加到您的(不是可见的)。FragmentFragmentFragmentActivity

尝试Fragment使用FragmentManager下面的方法查找它,看看它是如何进行的:

// ...
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();
}
于 2012-12-05T18:31:32.977 回答