почему radioGroup.check (R.id.radioButton) не работает внутри метода onPostExecute () - PullRequest
1 голос
/ 29 января 2020

morningRG.check (R.id.morningHappyRBtn) очень хорошо работает вне класса, расширяет AsyncTask <>, но не работает внутри метода onPostExecute ().

Если тип представления - TextView, onPostExecute view.setText (...) работает очень хорошо, но если его RadioButton не работает.

Я надеюсь на вашу помощь.

 if (moodM.equals(reportRadioIndexFull))
        morningRG.check(R.id.morningHappyRBtn);
    if (moodM.equals(reportRadioIndexHalf))
        morningRG.check(R.id.morningNeutralRBtn);
    if (moodM.equals(reportRadioIndexEmpty))
        morningRG.check(R.id.morningSadRBtn);
    if (moodM.equals(reportRadioIndexNA))
        morningRG.check(R.id.morningNARBtn);

DailyReportFragment. java

public class DailyReportFragment extends Fragment {


        private DailyReportViewModel dailyReportViewModel;

        private static final String KEY_SUCCESS = "success";
        private static final String KEY_DATA = "data";
        private static final String KEY_DATE = "date";
        private static final String KEY_CHILD_ID = "child_id";
        private static final String KEY_MOOD_MORNING = "mood_morning";
        private String date;
        private String childID;
        private String moodM;
        private int success;
        private ProgressDialog pDialog;
        private String reportRadioIndexFull = "2";
        private String reportRadioIndexHalf = "1";
        private String reportRadioIndexEmpty = "0";
        private String reportRadioIndexNA = "3";

        public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
            dailyReportViewModel =
                ViewModelProviders.of(this).get(DailyReportViewModel.class);
            View root = inflater.inflate(R.layout.fragment_daily_report, container, false);



            final RadioGroup morningRG = root.findViewById(R.id.morningRG);
            final TextView currentDayTV = root.findViewById(R.id.currentDayTV);

            date = currentDayTV.getText().toString();
            childID = "1";

            /*
            * Fetches single report details from the server
            */
            class FetchReportDetailsAsyncTask extends AsyncTask<String, String, String> {
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    //Display progress bar
                    pDialog = new ProgressDialog(getContext());
                    pDialog.setMessage("Loading Report Details. Please wait...");
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(false);
                    pDialog.show();
                }

                @Override
                protected String doInBackground(String... params) {
                    HttpJsonParser httpJsonParser = new HttpJsonParser();
                    Map<String, String> httpParams = new HashMap<>();
                    httpParams.put(KEY_DATE, date);
                    httpParams.put(KEY_CHILD_ID, childID);
                    JSONObject jsonObject = httpJsonParser.makeHttpRequest(
                        AccessVariable.getBaseUrl() + "get_daily_report.php", "GET", httpParams);
                    try {
                        int success = jsonObject.getInt(KEY_SUCCESS);
                        JSONObject report;
                        if (success == 1) {
                            //Parse the JSON response
                            report = jsonObject.getJSONObject(KEY_DATA);
                            moodM = report.getString(KEY_MOOD_MORNING);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    return null;
                }

                protected void onPostExecute(String result) {
                    pDialog.dismiss();
                    //Populate the Views once the network activity is finished executing
                    if (moodM.equals(reportRadioIndexFull))
                        morningRG.check(R.id.morningHappyRBtn);
                    if (moodM.equals(reportRadioIndexHalf))
                        morningRG.check(R.id.morningNeutralRBtn);
                    if (moodM.equals(reportRadioIndexEmpty))
                        morningRG.check(R.id.morningSadRBtn);
                    if (moodM.equals(reportRadioIndexNA))
                        morningRG.check(R.id.morningNARBtn);

                }

            }
            new AddReportAsyncTask().execute();
            return root;
        }
    }

main. xml

<RadioGroup
        android:id="@+id/morningRG"
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:padding="5dp"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="7.0">
        <RadioButton
            android:id="@+id/morningHappyRBtn"
            android:background="@drawable/happy_face_states"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="2.0" />
        <RadioButton 
            android:id="@+id/morningNeutralRBtn"
            android:background="@drawable/neutral_face_states"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="2.0" />
        <RadioButton
            android:id="@+id/morningSadRBtn"
            android:background="@drawable/sad_face_states"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="2.0" />
        <RadioButton
            android:id="@+id/morningNARBtn"
            android:background="@drawable/na_states"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.5" />
    </RadioGroup>

happy_face_states. xml (то же самое для нейтральных_лиц. Состояний. xml, sad_face_states. xml, на_статы. xml)

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/happyface_on" />
        <item android:state_focused="false" android:state_checked="true" android:drawable="@drawable/happyface_on" />
        <item android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/happyface" />
        <item android:state_focused="false" android:state_checked="false" android:drawable="@drawable/happyface" />
    </selector>
...