我正在开发公交车时间表Android应用程序。我有三个片段。
在第一个片段中,我有两个单选按钮,即From MalegaonTo Malegaon。 (Malegaon是地点的名称)。

如果选择From Malegaon单选按钮,则将文本设置为sourceEditText作为Malegaon。如果选择To Malegaon单选按钮,则将文本设置为destinationEditText作为Malegaon。

第一次访问片段时,此条件很好,但是如果我重新访问片段,则已选择From Malegaon单选按钮,sourceEditText为空白,并且destinationEditText的文本为Malegaon。

这是我的快照和第一个片段的代码。

java - 重新访问 fragment 后,在 fragment 内设置文本不起作用-LMLPHP

选择Malegaon单选按钮后。

java - 重新访问 fragment 后,在 fragment 内设置文本不起作用-LMLPHP

我只是在更改布局的可见性。 (源edittext,目标edittext,搜索按钮是一种布局)


  OldStandFragment.java


public class OldStandFragment extends Fragment {

public static OldStandFragment fragment ;
private static final String ARG_POSITIONS = "position";
private int positions;
private View myFragmentViewOld;
private LinearLayout fromOldMalegoanView, toOldMalegoanView;
Button selectRouteButton;
public static final String required_dest = "Please Enter Destination";
public static final String required_source = "Please Enter Source";
String language = "";
DbHelper helper;
private String sourceId = "", destinationId = "";
private ArrayList<Route> myArrayList;
private RouteAdapter routeAdapter;
private ListView routeListView;
private EditText sourceEditTextFromMalegoanOld;
private EditText destinationEditTextFromMalegoanOld;
private ImageButton searchFromMalegoanButtonOld;
private EditText sourceEditTextToMalegoanOld;
private EditText destinationEditTextToMalegoanOld;
private ImageButton searchToMalegoanButtonOld;
private RadioButton fromOldMalegoanRadioButton, toOldMalegoanRadioButton;

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

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    positions = getArguments().getInt(ARG_POSITIONS);

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    myFragmentViewOld = inflater.inflate(R.layout.fragment_old_stand, container, false);

        selectRouteButton = (Button) myFragmentViewOld.findViewById(R.id.selectRouteButton);

        fromOldMalegoanRadioButton = (RadioButton) myFragmentViewOld.findViewById(R.id.fromOldMalegoanRadioButton);
        toOldMalegoanRadioButton = (RadioButton) myFragmentViewOld.findViewById(R.id.toOldMalegoanRadioButton);

        fromOldMalegoanView = (LinearLayout) myFragmentViewOld.findViewById(R.id.fromOldMalegoanView);
        toOldMalegoanView = (LinearLayout) myFragmentViewOld.findViewById(R.id.toOldMalegoanView);

        sourceEditTextFromMalegoanOld = (EditText) fromOldMalegoanView.findViewById(R.id.sourceEditText);
        destinationEditTextFromMalegoanOld = (EditText) fromOldMalegoanView.findViewById(R.id.destinationEditText);
        searchFromMalegoanButtonOld = (ImageButton) fromOldMalegoanView.findViewById(R.id.searchResultButton);

        sourceEditTextToMalegoanOld = (EditText) toOldMalegoanView.findViewById(R.id.sourceEditText);
        destinationEditTextToMalegoanOld = (EditText) toOldMalegoanView.findViewById(R.id.destinationEditText);
        searchToMalegoanButtonOld = (ImageButton) toOldMalegoanView.findViewById(R.id.searchResultButton);

        SharedPreferences prefs = getContext().getSharedPreferences("MyPrefsFile", Context.MODE_PRIVATE);
        int a = prefs.getInt("LangValue", 0);
        if (a == 0) {
            language = "English";
        } else {
            language = "मराठी";
        }

        helper = new DbHelper(getContext());
        fromOldMalegoanRadioButton.setChecked(true);
        toOldMalegoanRadioButton.setChecked(false);
        fromOldMalegoanView.setVisibility(View.VISIBLE);
        toOldMalegoanView.setVisibility(View.GONE);

        String stopValue = helper.getStopName("1", language);
        sourceEditTextFromMalegoanOld.setText(stopValue);

        fromOldMalegoanRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (fromOldMalegoanRadioButton.isChecked()) {

                    toOldMalegoanRadioButton.setChecked(false);
                    fromOldMalegoanView.setVisibility(View.VISIBLE);
                    toOldMalegoanView.setVisibility(View.GONE);

                    helper = new DbHelper(getContext());
                    String stopValue1 = helper.getStopName("1", language);
                    sourceEditTextFromMalegoanOld.setText(stopValue1);
                    destinationEditTextFromMalegoanOld.setText("");
                }
            }
        });

        toOldMalegoanRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (toOldMalegoanRadioButton.isChecked()) {

                    fromOldMalegoanRadioButton.setChecked(false);
                    fromOldMalegoanView.setVisibility(View.GONE);
                    toOldMalegoanView.setVisibility(View.VISIBLE);

                    helper = new DbHelper(getContext());
                    String stopValue2 = helper.getStopName("1", language);
                    destinationEditTextToMalegoanOld.setText(stopValue2);
                    sourceEditTextToMalegoanOld.setText("");
                }
            }
        });

        searchFromMalegoanButtonOld.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //search result code.
            }
        });

        searchToMalegoanButtonOld.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //search result code.
            }
        });
    return myFragmentViewOld;
}

public static OldStandFragment newInstance(int position) {
    if(fragment == null) {
        fragment = new OldStandFragment();
    }

    Bundle bundle = new Bundle();
    bundle.putInt(ARG_POSITIONS, position);
    fragment.setArguments(bundle);
    return fragment;
    }
}



  fragment_old_stand.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000"
android:scrollbars="vertical"
tools:context="com.ashishkudale.malegoanagar.Fragments.OldStandFragment">


<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Select Direction or Route"
                android:gravity="center"
                android:textColor="#FFFFFF"
                android:id="@+id/Note"
                android:textStyle="bold"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:layout_margin="15dp" />
        </LinearLayout>


        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RadioGroup
                android:id="@+id/rg_ContainerOld"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_marginTop="15dp">

                <RadioButton
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="From Malegaon"
                    android:id="@+id/fromOldMalegoanRadioButton"
                    android:layout_marginLeft="5dp"
                    android:checked="false" />

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp">

                    <include
                        android:id="@+id/fromOldMalegoanView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        layout="@layout/source_destination"
                        android:layout_margin="5dp" />

                </LinearLayout>

                <RadioButton
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="To Malegaon"
                    android:id="@+id/toOldMalegoanRadioButton"
                    android:layout_marginLeft="5dp" />

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp">

                    <include
                        android:id="@+id/toOldMalegoanView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        layout="@layout/source_destination"
                        android:layout_margin="5dp" />

                </LinearLayout>

            </RadioGroup>
        </LinearLayout>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="  Select Route  "
            android:id="@+id/selectRouteButton"
            android:background="@drawable/button_effect"
            android:layout_gravity="center_horizontal"
            android:layout_margin="5dp" />

    </LinearLayout>

</ScrollView>




这是调用片段的适配器。


  MyPagerAdapter


public class MyPagerAdapter extends FragmentPagerAdapter {

    private final String[] TITLES = {"Old Stand","New Stand", "Fare"};

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position];
    }

    @Override
    public int getCount() {
        return TITLES.length;
    }

    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        android.support.v4.app.Fragment fragment = null;
        if(position ==0) {
            fragment = OldStandFragment.newInstance(position);
        }else if(position ==1 ){
            fragment = NewStandFragment.newInstance(position);
        } else if (position == 2) {
            fragment = MapFragment.newInstance(position);
        }
        return  fragment;
    }
}


重新访问OldStandFragment之后,它看起来像这样。

java - 重新访问 fragment 后,在 fragment 内设置文本不起作用-LMLPHP

我检查了在所有可能的地方添加日志。我发现重新访问OldStandFragment后,将调用toOldMalegoanRadioButton.setOnClickListner()方法。

现在,我想在重新访问或以其他任何方式解决此问题时刷新片段。

最佳答案

您必须使用SharedPreferences保存复选框的状态,请尝试以下代码

    public class StackOne extends AppCompatActivity {

    SharedPreferences prefs;
    private RadioButton rButton1, rButton2;
    private RadioGroup rg_ContainerOld;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stack_one);

        prefs = PreferenceManager.getDefaultSharedPreferences(StackOne.this);

        rButton1 = (RadioButton) findViewById(R.id.fromOldMalegoanRadioButton);
        rButton2 = (RadioButton) findViewById(R.id.toOldMalegoanRadioButton);
        rg_ContainerOld = (RadioGroup) findViewById(R.id.rg_ContainerOld);

        GetSelectedRadioButton();

        int k = prefs.getInt("rb1", 0);
        if (k == 1) {
            rButton1.setChecked(true);
            sourceEditTextFromMalegoanOld.setText("");
        } else if (k == 2) {
            rButton2.setChecked(true);
            sourceEditTextToMalegoanOld.setText("");
        }
    }

    private void GetSelectedRadioButton() {

        rg_ContainerOld.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (group.getCheckedRadioButtonId()) {
                    case R.id.fromOldMalegoanRadioButton:
                        prefs.edit().putInt("rb1", 1).commit();
                        break;
                    case R.id.toOldMalegoanRadioButton:
                        prefs.edit().putInt("rb1", 2).commit();
                        break;
                }
            }
        });
    }
}

09-11 09:31