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

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

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

下面是我来自ThirdFragment和ReviewFragment的代码

第三片段

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


评论片段

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

最佳答案

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

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

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

我认为您仅在可见时才需要向ViewPager添加OnPageChangeListener并使ReviewFragment从SharedPreferences中获取数据。另外,请检出Fragment.setUserVisibleHint()。它曾经不能与ViewPagers一起正常使用,但是我已经知道该问题已得到解决。

08-18 17:13