简体   繁体   English

从活动中更新viewpager中片段中的列表视图

[英]update a listview in a fragment in a viewpager from activity

I understand that this is quite a common issue, and I have referred to many different other questions but I still can't get this to work. 我知道这是一个非常普遍的问题,我已经提到了许多不同的其他问题,但我仍然无法使其发挥作用。

My activity implements a view pager with two tabs and in each tab is a listview. 我的活动实现了一个带有两个选项卡的视图寻呼机,每个选项卡中都有一个列表视图。 I have a adapter for my view pager which links the two fragments and in each fragment, a adapter to link the data to the listview. 我有一个适用于我的视图寻呼机的适配器,它连接两个片段和每个片段,一个适配器,用于将数据链接到列表视图。

In my activity menu, I have a menu which creates an edittext in an alertdialog for me to input new fields into one of the listview in one of the fragment. 在我的活动菜单中,我有一个菜单,它在alertdialog中创建一个edittext,以便我将新字段输入到其中一个片段的listview中。

My activity (contains a viewpager) 我的活动(包含一个viewpager)

protected void onCreate(Bundle savedInstanceState)
{
    ...
    subAdapter = new SubAdapter(getSupportFragmentManager(), data);
    ((ViewPager) findViewById(R.id.viewPager)).setAdapter(subGroupAdapter);
}

My viewpager adapter 我的viewpager适配器

public class SubAdapter extends FragmentPagerAdapter
{
    public SubGroupAdapter(FragmentManager fm, data data)
    {
        super(fm);
    }

@Override
public Fragment getItem(int position)
{
    Bundle bundle = new Bundle();
    bundle.putString("data", data);

    switch (position)
    {
        case 0:
            Fragment1 frag1 = new Fragment1();
            frag1.setArguments(bundle);
            return frag1;

        case 1:
            Fragment2 frag2 = new Fragment2();
            frag2.setArguments(bundle);
            return frag2;
    }

    return null;
}//some other methods below

Fragment1 / Fragment2 (both fragments have a listview) Fragment1 / Fragment2(两个片段都有一个listview)

public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);

    frag1Adapter = new frag1Adapter(this, data);
    ((ListView) getView().findViewById(R.id.listView)).setAdapter(frag1Adapter);
}

Custom listview adapter for both listviews in both fragments 自定义列表视图适配器,用于两个片段中的列表视图

public class ExpenseAdapter extends BaseAdapter implements OnClickListener
{ ... }

As I mentioned earlier, I can input a new entry into either listview from the activity action bar button. 正如我之前提到的,我可以从活动操作栏按钮向列表视图中输入一个新条目。 However, the listview does not get updated and I can't reference the listview adapter to call notifydatasetchanged() from the activity. 但是,listview没有更新,我无法引用listview适配器从活动中调用notifydatasetchanged()。

What is the best way I can proceed from here onwards? 从这里开始,最好的方法是什么? thank you so much! 非常感谢!

I have tried exploring using interfaces, tags but can't get it to work currently. 我尝试使用接口,标签进行探索,但目前无法使其正常工作。

What you should do is create a public method for your Fragment1 and Fragment2 like so: 你应该做的是为你的Fragment1和Fragment2创建一个公共方法,如下所示:

define in your Activity: 在您的活动中定义:

Fragment1 frag1; Fragment2 frag2;

then your ViewPager : 那么你的ViewPager

public class SubAdapter extends FragmentPagerAdapter
 {
    public SubGroupAdapter(FragmentManager fm, data data)
   {
      super(fm);
   }

 @Override
 public Fragment getItem(int position)
 {
    Bundle bundle = new Bundle();
    bundle.putString("data", data);

    switch (position)
    {
       case 0:
           frag1 = new Fragment1();
           frag1.setArguments(bundle);
           return frag1;

       case 1:
           frag2 = new Fragment2();
           frag2.setArguments(bundle);
           return frag2;
       }

     return null;
     }
}

Put this method in Fragment1.java : 将此方法放在Fragment1.java中:

public void updateFragment1ListView(){
   if(adapter != null){
       adapter.notifyDataSetChanged();
   }
}

and from your activity call: 并从您的活动电话:

  if(frag1 != null){
     frag1.updateFragment1ListView();
  }

obviously change the names for your adapter if its not called adapter... 显然更改适配器的名称,如果它不称为适配器...

Just do the same for Fragment2.java as well 也可以为Fragment2.java做同样的事情

I would recommend creating an interface. 我建议创建一个界面。 Here's an Example: 这是一个例子:

public interface UpdateListFragmentInterface {

    void updateList();
}

Then in the Activity, add the delegate as a property: 然后在Activity中,将委托添加为属性:

UpdateListFragmentInterface yourDelegate;

Wherever you create the Fragment within the Activity, assign the Fragment as the delegate: 无论您在Activity中创建Fragment,都可以将Fragment指定为委托:

// Set up and create your fragment.

(if yourFragment instanceof UpdateListFragmentInterface) {

    yourDelegate = yourFragment;
}

Then in the Fragment, implement the interface: 然后在Fragment中,实现接口:

public class YourListFragment extends android.support.v4.app.ListFragment implements UpdateListFragmentInterface {

    // Constructors and other methods for the ListFragment up here.

    // Override the interface method
    public void updateList() {

        //Update the list here.
    }
}

Then finally, from within your Activity, when you need to update the list, simply call the delegate: 最后,在您的Activity中,当您需要更新列表时,只需调用委托:

yourDelegate.updateList();

EDIT: 编辑:

Sorry, I think you actually need to create a public method in your activity that is something like: 抱歉,我认为您确实需要在活动中创建一个公共方法,例如:

public void setUpdateListFragmentDelegate(UpdateListFragmentDelegate delegate) {

    this.delegate = delegate
}

Then in the on attach of the List Fragment you will assign it there: 然后在列表片段的附加中,您将在那里分配:

if (context instance of YourActivity) {

    ((YourActivity)context.setUpdateListFragmentDelegate(this);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM