-3

I am using RecyclerView and CardView. I want the sysytem to choose which ImageView to display by circumstances from the JSON. How should I do it? I implemented the if statement but it seems like the statement is not working. Could someone give me an advice. This is my first time writing a parsing program. I don't know much about it.

package com.morimoku.location_activity;

    import android.app.DownloadManager;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.TextView;

    import androidx.annotation.Nullable;
    import androidx.fragment.app.Fragment;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;


    import com.android.volley.Request;
    import com.android.volley.RequestQueue;
    import com.android.volley.Response;
    import com.android.volley.VolleyError;
    import com.android.volley.toolbox.JsonObjectRequest;
    import com.android.volley.toolbox.Volley;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import java.util.ArrayList;

    public class favaoritesFragment extends Fragment {
        public static TextView data;
        private RecyclerView.Adapter mAdapter;
        private RequestQueue mQueue;
        private RecyclerView.LayoutManager mLayoutManager;
        int i1;
        int i2;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
            View view = inflater.inflate(R.layout.fragment_favorites,container,false);
            mQueue = Volley.newRequestQueue(getActivity());

            ArrayList<Status> statusArrayList = new ArrayList<>();
            statusArrayList.add(new Status(R.drawable.ic_action_name,"Line 1", (i1 == 0)?
                    R.drawable.ic_check_box_black_24dp: R.drawable.ic_warning_black_24dp));
            statusArrayList.add(new Status(R.drawable.ic_action_name,"Line 2", (i2 == 0)?
                    R.drawable.ic_check_box_black_24dp: R.drawable.ic_warning_black_24dp));
            mAdapter = new RecyclerAdapter(statusArrayList);
            RecyclerView  mRecyclerView = view.findViewById(R.id.recyclerView);
            mRecyclerView.setHasFixedSize(true);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));//listView.setAdapter(adapter);
            mRecyclerView.setAdapter(mAdapter);




            return view;
        }


    private void jsonParse() {
        String url = ("URL");
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArray = response.getJSONArray("odpt.TrainInformation:Line 1");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject negishi = jsonArray.getJSONObject(i);

                        String negishis = negishi.getString("odpt:trainInformationText");

                        if (negishis.equals( "operating normally")) {
                            i1 = 0;
                        } else {
                            i1 = 1;
                        }
                    }
                    JSONArray jsonArray1 = response.getJSONArray("odpt.TrainInformation:Line 2");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject negishi2 = jsonArray.getJSONObject(i);

                        String negishis2 = negishi2.getString("odpt:trainInformationText");

                        if (negishis2 .equals( "operating normally")) {
                            i2 = 0;
                        } else {
                            i2 = 1;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
    }}

1 Answers1

-1
statusArrayList.add(new Status(R.drawable.ic_action_name,"keihin",
       (i1 == 0? R.drawable.common_google_signin_btn_icon_dark_focused:
            R.drawable.common_google_signin_btn_icon_dark_focused));
EricHo
  • 183
  • 10
  • Thanks for the reply. It didn't work. – morimokuseijin Nov 11 '19 at 06:27
  • You original question was asking how to replace an if statement in your parameter. And now you changed your code with my answer and ask another question? – EricHo Nov 11 '19 at 09:30
  • Even if you found you have another question, you could have just edit it, there is no need to downvote – EricHo Nov 11 '19 at 09:31
  • I've edited this question because some guy said it is a duplicated question. The another question is completely different from this! And I didn't put the down vote! I repeat I didn't. Thanks for your advice. – morimokuseijin Nov 18 '19 at 06:42
  • By the way, this issue is not an issue of the if statement. I've changed some places to .equals but it does not function. I think there is an issue on the parsing part. Thanks for your advice. – morimokuseijin Nov 18 '19 at 06:53