簡體   English   中英

重新訪問片段后,在片段內設置文本不起作用

[英]Set text inside fragment not working after revisiting fragment

我正在開發公交車時間表Android應用程序。 我有三個片段。 在第一個片段中,我有兩個單選按鈕,即From MalegaonTo Malegaon Malegaon是地點的名稱)。

如果我選擇From Malegaon單選按鈕,則將文本設置為sourceEditText作為Malegaon 如果我選擇To Malegaon單選按鈕,則將文本設置為destinationEditText作為Malegaon

第一次訪問片段時,這種情況很好,但是如果我重新訪問片段,則已經選擇了From Malegaon單選按鈕,則sourceEditText為空白, destinationEditText文本為Malegaon

這是我的快照和第一個片段的代碼。

從Malegaon單選按鈕被選中

選擇Malegaon單選按鈕后。

在此處輸入圖片說明

我只是在更改布局的可見性。 (源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之后,它看起來像這樣。

在此處輸入圖片說明

我檢查了在所有可能的地方添加日志。 我發現,在重新OldStandFragment toOldMalegoanRadioButton.setOnClickListner()之后,將調用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;
                }
            }
        });
    }
}

在您的代碼中,您正在動態設置單選按鈕的選中狀態

fromOldMalegoanRadioButton.setChecked(true);
toOldMalegoanRadioButton.setChecked(false);

這就是為什么您的單選按鈕的oncheckedchanged方法將被調用的原因。 那就是為什么要在其中編寫代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM