java – 从另一个片段中调用方法

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

这是带有该功能的MainFragment(我排除了其他功能,因为我看不出它们是如何贡献的):

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

这段代码对我来说效果最好.而且看起来很容易

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...