我一直试图解决这个问题很长一段时间,但我无法弄清问题是什么。目的是向Songkick api(即json)提出请求,该请求将返回即将发生的艺术家事件并将其填充到片段上。该请求工作正常,但它不会使用recyclerView在屏幕上显示。
这是我的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.artispot.fragments.FragmentEvents">
<android.support.v7.widget.RecyclerView
android:id="@+id/List"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
这是适配器:
public class AdapterEvents extends RecyclerView.Adapter<AdapterEvents.ViewHolderEvents> {
private LayoutInflater layoutInflater;
private String cls = "TAG";
private ArrayList<Events> listEvents = new ArrayList<>();
public AdapterEvents(Context context){
layoutInflater = LayoutInflater.from(context);
}
public void setListEvents(ArrayList<Events> listEvents){
this.listEvents = listEvents;
notifyItemRangeChanged(0,listEvents.size());
}
@Override
public ViewHolderEvents onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.custom_events,parent,false);
ViewHolderEvents viewHolder =new ViewHolderEvents(view);
return viewHolder;
}
//create an object for each textview
@Override
public void onBindViewHolder(ViewHolderEvents holder, int position) {
Events currentEvent = listEvents.get(position);
holder.DateTextView.setText(currentEvent.getStartDate());
// + "--"+ currentEvent.getEndDate()
holder.displayName.setText(currentEvent.getDisplayName());
holder.Venue.setText(currentEvent.getVenue());
}
@Override
public int getItemCount() {
return listEvents.size();
}
static class ViewHolderEvents extends RecyclerView.ViewHolder{
private TextView DateTextView;
private TextView displayName;
private TextView Venue;
public ViewHolderEvents(View itemView){
super(itemView);
DateTextView = (TextView) itemView.findViewById(R.id.DateTextView);
displayName = (TextView) itemView.findViewById(R.id.displayName);
Venue = (TextView) itemView.findViewById(R.id.Venue);
}
}
}
这部分来自我的片段类:
public void sendRequest(){
// Formulate the request and handle the response.
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
getRequestUrl(),(String)null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse( JSONObject response) {
listEvents= parseJSONResponse(response);
adapterEvents.setListEvents(listEvents);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
});
requestQueue.add(request);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_events, container,false);
recyclerView = (RecyclerView)view.findViewById(R.id.List);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapterEvents = new AdapterEvents(getActivity());
recyclerView.setAdapter(adapterEvents);
return view;
}
我已经在这几天了,这有点令人沮丧,因为这是我的项目将在一周内完成。任何形式的帮助将不胜感激。提前谢谢。