Tablayout / Viewpager:在片段

时间:2015-12-18 14:16:50

标签: android listview android-fragments android-listview android-viewpager

我在tablayout / viewpager中的片段之间传递数据时遇到问题。我有一个带有四个选项卡的Tablayout,并使用viewpager启用切换。我试图从FirstTabFragment,SecondTabFragment和ThirdTabFragment保存数据,并在最后一个选项卡= ReviewFragment中检索它们。我已经将数据保存到SharedPreferences中,这适用于第一个和第二个片段,第三个片段中的数据未保存,并且在打开ReviewFragment时显示为空。

FirstFragment是Listview,保存所选listview项目中的字符串。 SecondFragment是一个editText,当按下下一个按钮时,会保存输入的值。 ThirdFragment也是一个Listview,FirstFragment中的代码与此片段中的代码相同,但不能按预期工作。

我的意思是不按预期工作: ThirdFragment中的值实际上是保存到SharedPreferences中但不是 检索。我知道这一点,因为当我关闭应用程序并重新打开它时,它会显示SharedPreferences中保存的值。

以下是我在ThirdFragment和ReviewFragment中的代码

ThirdFragment

  private SharedPreferences memory1;
    private SharedPreferences.Editor edt1;

    private ListView listView;
    private ArrayAdapter<String> listAdapter;

    public MtnPaymentMtdFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.payment_method, container, false);



        // Find the ListView resource.
        listView = (ListView) v.findViewById(R.id.mtn_plv);

        // Create and populate a List
        String[] p_method = new String[]{"ATM - MasterCard - Visa - Verve", "Online Bank Transfer", "Bank Deposit", "Recharge Card", "Mobile Banking"};
        ArrayList<String> p_methods = new ArrayList<String>();
        p_methods.addAll(Arrays.asList(p_method));

        // Create ArrayAdapter using the list.
        listAdapter = new ArrayAdapter<String>(this.getActivity(), R.layout.item_row3, p_methods);

        // Set the ArrayAdapter as the ListView's adapter.
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String selectedFromList = (listView.getItemAtPosition(i).toString().trim());

                switch (i) {
                    case 0:
                        MtnReviewFragment fragment = new MtnReviewFragment();
                        Bundle bundler = new Bundle();
                        bundler.putString("payment_mtd", selectedFromList);
                        fragment.setArguments(bundler);
//                        memory1 = getActivity().getPreferences(0);
//                        edt1 = memory1.edit();
//                        edt1.putString("payment_mtd", selectedFromList);
//                        edt1.apply();
                        ((MtnActivity) getActivity()).setCurrentItem(3, true);
                        break;
                    case 1:
//                        String selectedFromList1 = (listView.getItemAtPosition(i).toString().trim());
                        memory1 = getActivity().getPreferences(0);
                        edt1 = memory1.edit();
                        edt1.putString("payment_mtd", selectedFromList);
                        edt1.apply();
                        ((MtnActivity) getActivity()).setCurrentItem(3, true);
                        break;
                    case 2:
//                        String selectedFromList2 = (listView.getItemAtPosition(i).toString().trim());
                        memory1 = getActivity().getPreferences(0);
                        edt1 = memory1.edit();
                        edt1.putString("payment_mtd", selectedFromList);
                        edt1.apply();
                        ((MtnActivity) getActivity()).setCurrentItem(3, true);
                        break;
                    case 3:
//                        String selectedFromList3 = (listView.getItemAtPosition(i).toString().trim());
                        memory1 = getActivity().getPreferences(0);
                        edt1 = memory1.edit();
                        edt1.putString("payment_mtd", selectedFromList);
                        edt1.apply();
                        ((MtnActivity) getActivity()).setCurrentItem(3, true);
                        break;
                    case 4:
//                        String selectedFromList4 = (listView.getItemAtPosition(i).toString().trim());
                        memory1 = getActivity().getPreferences(0);
                        edt1 = memory1.edit();
                        edt1.putString("payment_mtd", selectedFromList);
                        edt1.apply();
                        ((MtnActivity) getActivity()).setCurrentItem(3, true);
                }
            }
        });
        return v;
    }
}

ReviewFragment

private SharedPreferences memory;
private SharedPreferences.Editor edt;
private TextView rev_bundle_result;
private TextView rev_mob_result;
private TextView rev_pmtd_result;
public static SharedPreferences savedSharedPreference;
public static String filename = "nameValue";
private static final String REGISTER_URL = "http://zzzzzzzzz.com/xxxxx/register.php";


public MtnReviewFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.review_layout, container, false);
    rev_bundle_result = (TextView) v.findViewById(R.id.rev_bundle_result);
    rev_mob_result = (TextView) v.findViewById(R.id.rev_mob_result);
    rev_pmtd_result = (TextView) v.findViewById(R.id.rev_pmtd_result);
    Button make_payment = (Button) v.findViewById(R.id.make_payment_but);

    memory = getActivity().getPreferences(0);
    String bundles = memory.getString("bundle_name", "empty");
    String mob_number = memory.getString("mob_number", "empty");
    String payment_mtds = memory.getString("payment_mtd", "empty");
    String p_amount = memory.getString("amount", "empty");

    rev_bundle_result.setText(bundles);
    rev_mob_result.setText(mob_number);
    rev_pmtd_result.setText(payment_mtds);

4 个答案:

答案 0 :(得分:1)

这有点旧了,您可能已经找到了解决方案,但万一其他人会在这里结束并需要答案......

我认为您的问题可能是因为,在您的ReviewFragment中,您在onCreateView()方法中从SharedPreferences获取数据。

滚动到ThirdFragment时,可能正在创建ReviewFragment。 ViewPagers通常会提前创建一些片段,以提高用户滚动时的性能。因此,当调用ReviewFragment.onCreateView()时,ThirdFagment尚未将其数据保存到SharedPreferences。在ReviewFragment.onCreateView()中添加一个断点,并检查它是否发生了什么。

我认为您必须向ViewPager添加OnPageChangeListener,并使ReviewFragment仅在可见时才从SharedPreferences获取数据。另外,请查看Fragment.setUserVisibleHint()。以前它与ViewPagers无法正常工作,但我听说它已被修复。

答案 1 :(得分:0)

如果您想确保在继续之前保存更改,请尝试commit()使用apply() SharedPreferences

apply()异步进行更改,而commit()同步保存更改。 https://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply()

此外,我已经看到了你的代码并且你调用并转换了容器活动,你应该使用android建议的监听器模式。阅读指南或从这篇文章开始:Send data between fragments in a pagerAdapter

答案 2 :(得分:0)

viewpager getview方法的逻辑只创建没有自身的左侧和右侧片段。您可以尝试设置页面限制setOffscreenPageLimit()

但加载所有片段并没有用。也许这可以解决你的问题。

答案 3 :(得分:0)

Here是从片段向tablayout / viewpagers片段发送数据的解决方案。