从另一个片段中调用方法 [英] Calling a method in one fragment from another

查看:170
本文介绍了从另一个片段中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了一段时间(以及在这里搜索),以帮助我在MainFragment中刷新listView,当按下其他片段的按钮时。 sommeone能解释一下如何使它工作吗?

I've been trying for a while already (as well, searching in here) for something to help me to refresh listView in my MainFragment, when a button is pressed in other fragment. Could sommeone explain me how to make it work?

这里是MainFragment的功能(我排除了其他功能,因为我没有看到他们如何贡献):

Here's MainFragment with the function (I excluded other functions, since I don't see how they contribute):

public class MainFragment extends Fragment {
public void otherList() {
    SQLite db = new SQLite(getActivity());
    db.open();
    Calendar sCalendar = Calendar.getInstance();
    String day = sCalendar.getDisplayName(Calendar.DAY_OF_WEEK,
            Calendar.LONG, Locale.ENGLISH);
    Cursor c = db.getAllRecords(day);
    Calendar cal = Calendar.getInstance();
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    arrayOfBars.clear();
    if (c.moveToFirst()) {
        do {
            String country = c.getString(0);
            String city = c.getString(1);
            String bar = c.getString(2);
            String timeStart = c.getString(3);

            String[] h = timeStart.split(":");
            int times = Integer.parseInt(h[0]);

            String timeEnd = c.getString(4);
            String[] h2 = timeEnd.split(":");
            int timee = Integer.parseInt(h2[0]);

            String placeLaLo = c.getString(5);
            String description = c.getString(6);
            String likes = c.getString(7);
            String offer = c.getString(8);

            if (hour < (times + 1) || hour < (timee)) {
                getPrefs = PreferenceManager
                        .getDefaultSharedPreferences(MainFragment.this
                                .getActivity());
                boolean lay = getPrefs.getBoolean("boolean", false);
                if (lay == true) {
                    while (times < timee) {
                        timeStart = times + ":" + h[1];
                        times++;
                        timeEnd = times + ":" + h2[1];
                        arrayOfBars.add(new BarObject(country, city, bar,
                                timeStart, timeEnd, placeLaLo, description,
                                likes, offer, bar));
                    }
                } else {
                    arrayOfBars.add(new BarObject(country, city, bar,
                            timeStart, timeEnd, placeLaLo, description,
                            likes, offer, bar));
                }
            }

        } while (c.moveToNext());

        Collections.sort(arrayOfBars, new BarObject.CompST());

        lv2.setAdapter(null);
        adapter = new BarAdapter(getActivity(), arrayOfBars);
        lv2.setAdapter(adapter);
    } else {
        Toast.makeText(getActivity(), "No database found",
                Toast.LENGTH_LONG).show();
    }
    db.close();
}

}

这是另一个片段:

public class SocialMedia extends Fragment {
ImageButton facebook, twitter, layoutS;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.social_media, container, false);
    layoutS = (ImageButton) view.findViewById(R.id.ibLayout);

    layoutS.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences getPrefs = PreferenceManager
                    .getDefaultSharedPreferences(SocialMedia.this
                            .getActivity());
            boolean lay = getPrefs.getBoolean("boolean", false);
            SharedPreferences.Editor e = getPrefs.edit();
            lay = !lay;
            e.putBoolean("boolean", lay);
            e.commit();
                            //Here I want to call otherList() from MainFragment
        }
    });
    return view;
}

}

推荐答案

FragmentManager fm = getFragmentManager(); 
MainFragment fragm = (MainFragment)fm.findFragmentById(R.id.main_fragment); 
fragm.otherList(); 

此代码对我最有效。似乎很容易

This code worked best for me. And seems quite easy

这篇关于从另一个片段中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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