在片段中使用Volley填充RecyclerView

时间:2019-01-14 14:14:14

标签: java android android-fragments android-recyclerview android-volley

我正在使用Volley解析Json,并在Fragment中填充我的Recyclerview。我发现类似的线程,其中解决方案包括将适配器放置在onResponse方法中或将setAdapter放置在setLayoutManager之前,这对我不起作用。我已经尝试记录了有效的Json,所以我确定问题与适配器有关。

我已将代码以及适配器类包括在片段中。

我尝试了不同的仿真器,以不同的方法设置适配器,并通知适配器数据集已更改。

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);

这是使用的适配器。

公共类WatchList_CustomRow扩展了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;    }

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

获得响应后,您甚至都没有将数据传递到适配器,而您正在呼叫notifyDatasetChanged。为了更好地进行JSON解析,请使用GSON并将对象映射到 Model / Pojo 类上,并​​从中获取arrayList并将其传递给适配器。

也请张贴JSON响应。.那么便可以为您提供更好的解决方案。