I have a dynamic list of several name. But, the RecyclerView displays the first item of the list only. What can be done to solve that problem?
For RecyclerView
routes_recyclerview.xml includes:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".ui.activities.RoutesAssignActivity" tools:showIn="@layout/activity_routes_assign" android:layout_marginStart="10dp" android:layout_marginEnd="10dp"> <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" android:layout_marginTop="10dp" /> </RelativeLayout> For displaying the list name,
routes_recycler_listview.xml includes:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:gravity="center_vertical" android:paddingEnd="0dp" android:paddingStart="10dp" android:textColor="@color/md_black_1000" android:textSize="20sp" /> </RelativeLayout> I includes header of the list. So, for header, I have
view_header.xml includes:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp" android:background="#001F3F" android:textSize="28sp" android:textStyle="bold" android:textColor="@android:color/white" tools:context=".ui.activities.RoutesAssignActivity" /> This getter and setter method for routes class:
Routes.jave includes:
public class Routes{ @SerializedName("assignDate") private String assignDate; @SerializedName("routeName") private String routeName; public String getAssignDate() { return assignDate; } public void setAssignDate(String assignDate) { this.assignDate = assignDate; } public String getRouteName() { return routeName; } public void setRouteName(String routeName) { this.routeName = routeName; } } To list the total routes, i have RoutesList.java. Here,
public class RoutesList implements Comparable<RoutesList>{ @SerializedName("date") private String date; @SerializedName("routes") private List<Routes> routes = new ArrayList<>(); public String getDateString() { return date; } public Date getDate() { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US); try { return format.parse(getDateString()); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public void setDate(String date) { this.date = date; } public List<Routes> getRoutes() { return routes; } public void setRoutes(List<Routes> routes) { this.routes = routes; } @Override public int compareTo(@NonNull RoutesList another) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US); try { Date date = format.parse(another.getDateString()); return date.compareTo(getDate()); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 0; } } In my RecyclerViewRecyclerAdapter.java, i have: At ViewHolder i use:
public TextView textView; public ViewHolder(View v) { super(v); textView = (TextView) v.findViewById(R.id.textView); } At onCreateViewHolder method, i use:
public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) { this.context = parent.getContext(); return new ViewHolder(LayoutInflater.from(context) .inflate(R.layout.routes_recycler_listview, parent, false)); } At onBindViewHolder method, i use:
public void onBindViewHolder(final ViewHolder holder, final int position) { holder.textView.setText(item.getRoutes().get(position).getRouteName()); } At RoutesAssignActivity.java class, i use :
recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build()); adapter = new StickyHeaderListView(routes,this); final StickyRecyclerHeadersDecoration headersDecor = new StickyRecyclerHeadersDecoration(adapter); recyclerView.addItemDecoration(headersDecor); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(adapter); After all, i only get this:
How do i get all name in list ?

No comments:
Post a Comment