简体   繁体   中英

Populating RecyclerView Using Volley in Fragments

I am using Volley to parse Json and populate my Recyclerview in a Fragment. I have found similar threads where solutions included placing adapter in the onResponse method or placing setAdapter before setLayoutManager which have not worked for me. I have tried logging the Json which worked so I am certain the issue is related to the adapter.

I have included the code in my fragment as well as the adapter class.

I have tried different emulators, setting the adapter in different methods and notifying adapter that dataset changed.

private RequestQueue mQueue;


@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mlayoutManager = new LinearLayoutManager(getContext());

    final RecyclerView.Adapter adapter = new WatchListAdapter(names, names, names, names, names);

    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(mlayoutManager);


    mQueue = Volley.newRequestQueue(getContext());
    String url = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=gbp";
    JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {



            for(int i = 0; i<response.length();i++){
                JSONObject obj = null;
                try {
                    obj = response.getJSONObject(i);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                String name = null;
                try {
                    name = obj.getString("id");
                    names.add(name);

                    adapter.notifyDataSetChanged();


                } catch (JSONException e) {
                    e.printStackTrace();
                }



            }


        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("Volley in Tab4", "not working");

        }
    });
    mQueue.add(request);

This is the adapter used.

public class WatchList_CustomRow extends ArrayAdapter {

ArrayList<String> ranks = new ArrayList<>();
ArrayList<String> names = new ArrayList<>();
ArrayList<String> prices = new ArrayList<>();
ArrayList<String> caps= new ArrayList<>();
ArrayList<String> changes = new ArrayList<>();

public WatchList_CustomRow(@NonNull Context context,ArrayList<String> rank1, ArrayList<String> names1, ArrayList<String> prices1, ArrayList<String> caps1, ArrayList<String> changes1) {
    super(context, R.layout.activity_watch_list__custom_row,rank1);
     names = names1;
    prices = prices1;
    caps=caps1;
    changes=changes1;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(getContext());
    View customView = inflater.inflate(R.layout.activity_watch_list__custom_row, parent, false);

    String singleRank = getItem(position);
    String singleName = names.get(position);
    String singlePrice = prices.get(position);
    String singleCap= caps.get(position);
    String singleChange = changes.get(position);


    TextView rankTextView = customView.findViewById(R.id.rankTextView);
    TextView nameTextView = customView.findViewById(R.id.WatchedCoinName);
    TextView priceTextView = customView.findViewById(R.id.WatchedCoinPrice);
    TextView capTextView = customView.findViewById(R.id.WatchedcoinMarket);
    TextView changeTextView = customView.findViewById(R.id.WatchCoin24Change);


    rankTextView.setText(singleRank);
    //bodyTextView.setText(singleBody);
    nameTextView.setText(singleName);
    priceTextView.setText(singlePrice);
    capTextView.setText(singleCap);
    changeTextView.setText(singleChange);


    return customView;    }

}

使用RecyclerView.Adapter以及相应的onCreateViewHolder和onBindViewHolder方法来绑定视图和数据

you are not even passing data to Adapter after getting response and you are calling notifyDatasetChanged . and for better JSON parsing use GSON and map your object on Model/Pojo class and get arrayList from of them and pass it to adapter.

kindly post JSON Response as well..then would be able to reply with better solution for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM