[英]Populating RecyclerView Using Volley in Fragments
I am using Volley to parse Json and populate my Recyclerview in a Fragment. 我正在使用Volley解析Json并将我的Recyclerview填充为片段。 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.
我发现类似的线程,其中解决方案包括将适配器放置在onResponse方法中或将setAdapter放置在setLayoutManager之前,这对我不起作用。 I have tried logging the Json which worked so I am certain the issue is related to the adapter.
我已经尝试记录了工作的Json,所以我确定问题与适配器有关。
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 { 公共类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方法来绑定视图和数据
you are not even passing data to Adapter after getting response and you are calling notifyDatasetChanged
. 您甚至没有在获得响应后将数据传递给Adapter,而是在调用
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. 为了更好地进行JSON解析,请使用GSON并将您的对象映射到Model / Pojo类上,并从中获取arrayList并将其传递给适配器。
kindly post JSON Response as well..then would be able to reply with better solution for you. 请也发布JSON响应。.那么您将可以使用更好的解决方案进行回复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.