簡體   English   中英

在片段中使用Volley填充RecyclerView

[英]Populating RecyclerView Using Volley in Fragments

我正在使用Volley解析Json並將我的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;    }

}

使用RecyclerView.Adapter以及相應的onCreateViewHolder和onBindViewHolder方法來綁定視圖和數據

您甚至沒有在獲得響應后將數據傳遞給Adapter,而是在調用notifyDatasetChanged 為了更好地進行JSON解析,請使用GSON並將您的對象映射到Model / Pojo類上,並從中獲取arrayList並將其傳遞給適配器。

請也發布JSON響應。.那么您將可以使用更好的解決方案進行回復。

暫無
暫無

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

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