在FragmentActivity从对话框更新的片段在ListView(使用ViewPager) [英] Updating the ListView in a Fragment from a Dialog in FragmentActivity(using a ViewPager)

查看:129
本文介绍了在FragmentActivity从对话框更新的片段在ListView(使用ViewPager)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 FragmentActivity (主)创造3 片段,也是一个菜单。 pretty直线前进,并在Android SDK中的例子。

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

从主活动菜单我都用EDITTEXT的对话框。从此文本字段的值是假设被存储在数据库中,其工作得很好,并且在片段(未我ListFragment,但在它列表视图的片段)也弹出在列表视图。最简单的方法是调用notifyDataSetChanged()在ListView适配器上。不过,我不能这样做。

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.

这是ListView的片段:

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

这里是我想要更新的ListView,虽然这不会起作用。

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

帮助,请。

推荐答案

您code不起作用,因为该行:

Your code doesn't work because the line:

mSectionsPagerAdapter.getItem(2)

在从 ViewPager ,它的创建片段 >新的片段的这一立场,并呼吁该片段实例更新方法显然不会做任何改变,因为这片段未连接到你的活动(不可见的)。

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).

尝试使用 FragmentManager 象下面片段来寻找这一点,看看它是如何去:

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

这篇关于在FragmentActivity从对话框更新的片段在ListView(使用ViewPager)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆