I am trying to implement a list with a RecyclerView
whose adapter is powered by a cursor. I do not understand why when executing the app, the list shows only the first element although in the table of the database I have 7 records. I checked everything I could and the only thing I found was that in the layout
of the item the TextView
had the property layout_height="match_parent"
and I changed it to wrap_content
but the behavior did not change.
Any comment will be welcome. Thanks.
RecyclerView XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/lista"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</LinearLayout>
XML of the Item
<TextView
android:textSize="30sp"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hola mundo"
android:padding="10dp"
android:textColor="@color/black"
/>
<TextView
android:textSize="30sp"
android:id="@+id/listanombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:padding="10dp"
/>
<View style="@style/Divider"/>
Adapter
public class ListaAdapter extends RecyclerView.Adapter<ListaAdapter.ViewHolder>{
Context myContext;
CursorAdapter myCursorAdapter;
public ListaAdapter(Context context, Cursor cursor){
myContext=context;
myCursorAdapter = new CursorAdapter(myContext,cursor,0) {
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.item,parent,false);
return retView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView textView = (TextView) view.findViewById(R.id.title);
TextView listanombre = (TextView) view.findViewById(R.id.listanombre);
textView.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
listanombre.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));
}
};
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
myCursorAdapter.getCursor().moveToPosition(position);
myCursorAdapter.bindView(holder.itemView,myContext,myCursorAdapter.getCursor());
}
@Override
public int getItemCount() {
return myCursorAdapter.getCount();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView textView,listanombre;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.title);
listanombre = (TextView)itemView.findViewById(R.id.listanombre);
}
}
}