I am trying to show the list in RecyclerView.
but after setting adapter only Adapter constructor getting called after that nothing happens
Below is the code of Adapter
public class ChildNameAdpator extends RecyclerView.Adapter<ChildNameAdpator.ViewHolder> {
List<ChildDatum> arrayList;
public ChildNameAdpator(List<ChildDatum> arrayListChildName) {
arrayList = new ArrayList<>();
arrayList = arrayListChildName;
}
#NonNull
#Override
public ChildNameAdpator.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LinearLayout layoutRowPermissionCount = (LinearLayout) LayoutInflater.from(parent.getContext()).inflate(R.layout.row_child_name_layout, parent, false);
return new ViewHolder(layoutRowPermissionCount);
}
#Override
public void onBindViewHolder(#NonNull ChildNameAdpator.ViewHolder holder, int position) {
holder.mTvChildName.setText(arrayList.get(position).getChildName());
}
#Override
public int getItemCount() {
return arrayList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
AutoResizeTextView mTvChildName;
LinearLayout mLinearLayoutChild;
AutoResizeTextView imageButtonDeleteChildRow;
ViewHolder(#NonNull View itemView) {
super(itemView);
Log.d(TAG, "ViewHolder: ");
mTvChildName = itemView.findViewById(R.id.tv_row_child_name);
mLinearLayoutChild = itemView.findViewById(R.id.ll_row_child_name);
}
}
}
I am calling this adapter with below code
mRecyclerviewChildName = findViewById(R.id.rv_child_name_parental_control);
mRecyclerviewChildName.setLayoutManager(new LinearLayoutManager(this));
mCurrentChildsList.add(new ChildDatum("11", "ty8902", "3333", "3333", "333", "2222", "2222", "1222"));
mCurrentChildsList.add(new ChildDatum("12", "ty8902", "3333", "3333", "333", "2222", "2222", "1222"));
mCurrentChildsList.add(new ChildDatum("13", "ty8902", "3333", "3333", "333", "2222", "2222", "1222"));
childNameAdpator = new ChildNameAdpator(mCurrentChildsList);
mRecyclerviewChildName.setAdapter(childNameAdpator);
Below is row layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/tv_row_child_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
EDIT: Added Recyclerview's Parent Layout
<?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" >
<androidx.cardview.widget.CardView
android:id="#+id/card_view_display_child_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="16dp"
android:padding="8dp"
android:visibility="gone"
app:cardBackgroundColor="#color/colorPrimaryDark"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_child_name_parental_control"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="vertical">
</androidx.recyclerview.widget.RecyclerView>
<com.lb.auto_fit_textview.AutoResizeTextView
android:id="#+id/ib_add_new_child_name"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:background="#drawable/ic_add_circle_black_24dp"
android:padding="8dp"
android:textAlignment="center"
android:textColor="#color/colorGreyLight"
android:textStyle="bold"
android:visibility="gone" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
After setAdapter only constructor getting called nothing else that's it.
I thought it is due to my Gradle issue and tried on another laptop, the same issue occurred there.
Looking for help
Thanks in advance.
Change Adapter Constructor and try this
public ChildNameAdpator(List<ChildDatum> arrayListChildName) {
arrayList = new ArrayList<>();
arrayList.addAll(arrayList);
}
or
public ChildNameAdpator(List<ChildDatum> arrayListChildName) {
this.arrayList = arrayListChildName;
}
Try this way:
rView=findViewById(R.id.rView);
rView.setHasFixedSize(true);
lManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
rView.setLayoutManager(lManager);
Make sure the following things you have done:
Declared your adapter class as public
Initialised
'mCurrentChildsList' before adding items.
Initialise: mCurrentChildsList = new ArrayList()
You have skipped the LayoutManager.
A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user.
Set the LayoutManager once you initialized the recycler view.
mRecyclerviewChildName.setLayoutManager(new LinearLayoutManager(this));
Reference : LayoutManager
Adapter Code:
public class ChildNameAdpator extends RecyclerView.Adapter<ChildNameAdpator.ViewHolder> {
private List<ChildDatum> arrayList;
public ChildNameAdpator(List<ChildDatum> arrayListChildName) {
arrayList = arrayListChildName;
}
}
This how your adapter class looks like.
Main Activity.
public class DemoTestActivity extends AppCompatActivity {
RecyclerView mRecyclerviewChildName;
ChildNameAdpator childNameAdpator;
List<ChildDatum> mCurrentChildsList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo_test);
mCurrentChildsList = new ArrayList<>();
mRecyclerviewChildName=(RecyclerView)findViewById(R.id.rv_child_name_parental_control);
mCurrentChildsList.add(new ChildDatum("Mehul"));
mCurrentChildsList.add(new ChildDatum("Mehul 1"));
mCurrentChildsList.add(new ChildDatum("Mehul 2"));
childNameAdpator = new ChildNameAdpator(mCurrentChildsList);
mRecyclerviewChildName.setLayoutManager(new LinearLayoutManager(DemoTestActivity.this, LinearLayoutManager.VERTICAL, false));
mRecyclerviewChildName.addItemDecoration(new DividerItemDecoration(mRecyclerviewChildName.getContext(), DividerItemDecoration.VERTICAL));
mRecyclerviewChildName.setAdapter(childNameAdpator);
}}
Main Layout file
<?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="match_parent"
tools:context=".DemoTestActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_child_name_parental_control"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="vertical">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
Adapter
public class ChildNameAdpator extends
RecyclerView.Adapter<ChildNameAdpator.ViewHolder> {
List<ChildDatum> arrayList;
public ChildNameAdpator(List<ChildDatum> arrayListChildName) {
arrayList = new ArrayList<>();
arrayList = arrayListChildName;
}
#NonNull
#Override
public ChildNameAdpator.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LinearLayout layoutRowPermissionCount = (LinearLayout) LayoutInflater.from(parent.getContext()).inflate(R.layout.row_child_name_layout, parent, false);
return new ViewHolder(layoutRowPermissionCount);
}
#Override
public void onBindViewHolder(#NonNull ChildNameAdpator.ViewHolder holder, int position) {
holder.mTvChildName.setText(arrayList.get(position).getChildName());
}
#Override
public int getItemCount() {
return arrayList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView mTvChildName;
ViewHolder(#NonNull View itemView) {
super(itemView);
mTvChildName = itemView.findViewById(R.id.tv_row_child_name);
}
}}
Model class
public class ChildDatum {
String ChildName;
public ChildDatum(String sChildName) {
this.ChildName = sChildName;
}
public String getChildName() {
return ChildName;
}
public void setChildName(String childName) {
ChildName = childName;
}}
Finally, I got my mistake.
android:visibility="gone"
VISIBILITY of parent Cardview was GONE due to this onBindViewHolder was not getting called.
When I changed the visibility to VISIBLE it Worked.
Thank you all for your efforts.
Related
I am having a bit trouble with my RecyclerView. If my adapter list have more than 20 items, the recyclerview shows nothing. Anything <20 is ok, and the items are displayed. Also, I can't scroll on the recyclerView.
RecyclerView creation in Fragment:
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
myAdapter = new MyAdapter();
myAdapter.setList(myList); //ArrayList<MyObject>
recyclerView.setAdapter(myAdapter);
My Adapter:
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
private final ArrayList<MyObject> myList = new ArrayList<MyObject>();
public MyAdapter() {}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_item, parent, false);
return new MyAdapter(v);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.bind(myList.get(position));
}
#Override
public int getItemCount() {
return myList.size();
}
public void setList(ArrayList<MyObject> newList) {
myList.clear();
myList.addAll(newList);
notifyDataSetChanged();
}
}
and here is my viewholder:
public class CountryViewHolder extends RecyclerView.ViewHolder {
private final TextView textView;
private MyObject object;
public CountryViewHolder(#NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.my_holder_textview);
}
public void bind(#Nullable MyObject object){
this.object = object;
if(pbject == null)
textView.setText("Error. Could not load name.");
else
textView.setText(object.getName());
}
}
myitem.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/my_holder_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_marginBottom="4dp"
android:textSize="24sp"
android:gravity="center"/>
</LinearLayout>
Fragment layout where I have the recyclerView:
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Any suggestions?
EDIT:
Changing the layout from ConstraintLayout to LinearLayout, the recyclerview will run fine as it should. So the culprit here is the Constraintlayout. I would like to keep the constraintlayout, but I can't figure out how to make it work yet.
Instead of:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
Try:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
I can see that you have an error in onCreateViewHolder, it should return MyViewHolder and not a MyAdapter.
Also the setHasFixedSize(true) could be an issue here because your children might not change in size if this has to be true(find more information on SO).
I have created RecyclerView and showing data from JSON.
Issue I'm facing is, while Toast data is showing correctly, but in RecyclerView same data is not appear.
Here is code:
public class MainActivity extends AppCompatActivity {
private static final int NUM_LIST_ITEMS = 100;
private static final String TOKEN =
"71cf2d3dec294394e267fbb0bf28916f4198f8d6";
private CuloAdapter culoAdapter;
List<Hotel> lh = new ArrayList<>();
RecyclerView recyclerView;
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rv_tiketapi);
LinearLayoutManager layout = new LinearLayoutManager(this);
layout.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layout);
CuloAdapter ar = new CuloAdapter(lh);
recyclerView.setAdapter(ar);
loadHotelLocation();
}
private void loadHotelLocation() {
final search apiService = ApiService.getService(search.class);
retrofit2.Call<SingleResult<Hotel>> call = apiService.findHotel(TOKEN,
"json");
call.enqueue(new Callback<SingleResult<Hotel>>() {
#Override
public void onResponse(retrofit2.Call<SingleResult<Hotel>> call,
Response<SingleResult<Hotel>> response) {
if (response.body().getDiagnostic().isSuccess()) {
//SingleResult.ResultList list =
response.body().getResults();
List<Hotel> mHotels = (List<Hotel>)
response.body().getResults().getResult();
lh.addAll(mHotels);
Toast.makeText(getApplicationContext(), "OK" +lh,
Toast.LENGTH_LONG).show();
}
}
#Override
public void onFailure(retrofit2.Call<SingleResult<Hotel>> call,
Throwable t) {
}
});
}
RecyclerView Adapter :
public class CuloAdapter extends
RecyclerView.Adapter<CuloAdapter.ViewHolder> {
public CuloAdapter(List<Hotel> lh) {
this.items = lh;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView txtTitle;
public TextView txtSubTitle;
public ImageView imgIcon;
public ViewHolder(final View container) {
super(container);
txtTitle = (TextView) container.findViewById(R.id.airportName);
txtSubTitle = (TextView) container.findViewById(R.id.airportCode);
}
}
private List<Hotel> items;
public CuloAdapter(final Activity activity, List<Hotel> items) {
this.items = items;
}
#Override
public int getItemCount() {
return items.size();
}
#Override
public void onBindViewHolder(CuloAdapter.ViewHolder holder, int position) {
Hotel item = items.get(position);
holder.txtTitle.setText(item.getLabel());
holder.txtSubTitle.setText(item.getId());
}
#Override
public CuloAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_flight, parent, false);
return new ViewHolder(v);
}
}
MainActivity 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.admin.exampletiketapi.MainActivity">
<EditText
android:id="#+id/et_find"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_tiketapi"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Item List XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/airportsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/airportName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="Soekarno Hatta" />
<TextView
android:id="#+id/airportCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="20" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:background="#DDD"
android:visibility="visible" />
</LinearLayout>
In your code you are trying to set adapter before loading data.
CuloAdapter ar = new CuloAdapter(lh);
recyclerView.setAdapter(ar);
loadHotelLocation();
What i found is You are getting data in loadHotelLocation(), but trying to set adpter before that,
Call notifyDatasetChanged after lh.addAll(mHotels). And check for null's else you are heading for crash in case search results are zero/null
I am using fragment and recyclerview together. I also have database and I want to query results coming from the database and display the results inside the activity.
However every time I try to run the application and switch to the part to get and view the results, I don't seem to get anything. No results at all just blank. I don't know why it's not showing up.
This is my full code
Sample class
public class Sample extends Fragment {
private View rootView;
public Sample() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
rootView = inflater.inflate(sample, container, false);
RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView_sample);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setHasFixedSize(true);
DatabaseHandler db= new DatabaseHandler(getActivity());
List<SampleModel> list = db.getResults();
SampleAdapter sampleAdapter = new SampleAdapter(list);
mRecyclerView.setAdapter(sampleAdapter);
return rootView;
}
}
Adapter Class
public class SampleAdapter extends RecyclerView.Adapter <SampleAdapter.ViewHolder> {
private List<SampleModel> list;
private Context mContext;
public SampleAdapter (List<SampleModel> list) {
list = list;
}
#Override
public SampleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.sample_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(SampleAdapter.ViewHolder holder, int position) {
SampleModel sample = list.get(holder.getAdapterPosition());
holder.title.setText(sample.getTitle())
}
#Override
public int getItemCount() {
return (list != null? list.size():0);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
}
}
}
XML
sample.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true">
<include layout="#layout/sample_recyclerview" />
</android.support.design.widget.CoordinatorLayout>
sample_recyclerview.xml
<?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="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_sample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"/>
</RelativeLayout>
sample_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:focusable="true"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardBackgroundColor="#808080">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff" />
</LinearLayout>
</android.support.v7.widget.CardView>
In your SampleAdapter change
list = list;
to
this.list = list;
Your Adapter constructor is wrong. You are not passing the dataSource to your adapter. Change it to
public SampleAdapter (List<SampleModel> list) {
this.list = list;
}
Furthermore you have to notify the adapter that you changed your dataSource. You do that by calling the method notifyDataSetChanged
SampleAdapter sampleAdapter = new SampleAdapter(list);
mRecyclerView.setAdapter(sampleAdapter);
sampleAdapter.notifyDataSetChanged();
RecyclerViews will return nothing if the size is not rightly returned(e.g using : return 0;)
change your code from :
public int getItemCount() {
return (list != null? list.size():0);
To:
public int getItemCount() {
return list.size();
I want to create a RecyclerView with a few CardViews in it. For this I start a Thread in the onCreate of my Activity. There I get the data from my server and put this in a list. Then I create the Adapter for the RecyclerView, but it doesn't work. Here is my code:
Here I create the adapter and it should fill all CardViews in:
ListAdapter adapter = new ListAdapter(posts);
RecyclerView rv = (RecyclerView)findViewById(R.id.post_list);
rv.setAdapter(adapter);
Here is my adapter class
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.PostViewHolder> {
List<Post> posts;
public ListAdapter(List<Post> posts) {
Log.d("ListAdapter", "");
this.posts = posts;
}
#Override
public int getItemCount() {
Log.d("getItemCount", "");
return posts.size();
}
#Override
public PostViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
Log.d("onCreateView", "");
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.insert_layout, viewGroup, false);
PostViewHolder pvh = new PostViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(PostViewHolder postViewHolder, int i) {
Log.d("onBindView", "");
postViewHolder.username.setText(posts.get(i).getUsername());
postViewHolder.text.setText(posts.get(i).getText());
postViewHolder.time.setText(Long.toString(posts.get(i).getTime()));
postViewHolder.postPhoto.setImageResource(posts.get(i).returnIMG());
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
Log.d("onAttached", "");
super.onAttachedToRecyclerView(recyclerView);
}
public static class PostViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView username;
TextView time;
TextView text;
ImageView postPhoto;
PostViewHolder(View itemView) {
super(itemView);
Log.d("PostViewHolder", "");
cv = (CardView) itemView.findViewById(R.id.cv);
username = (TextView) itemView.findViewById(R.id.usernameText);
time = (TextView) itemView.findViewById(R.id.timeText);
text = (TextView) itemView.findViewById(R.id.textText);
postPhoto = (ImageView) itemView.findViewById(R.id.postPhoto);
}
}
Here is my recyclerview:
<android.support.v7.widget.RecyclerView
android:id="#+id/post_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
and my cardview:
<android.support.v7.widget.CardView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/postPhoto"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textText"
android:layout_alignParentTop="true"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/usernameText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/timeText"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
And here the 2 errors:
E/RecyclerView: No adapter attached; skipping layout E/RecyclerView:
No layout manager attached; skipping layout
But I do both, dont't I?
set the LayoutManager first.
From your code :
ListAdapter adapter = new ListAdapter(posts);
RecyclerView rv = (RecyclerView)findViewById(R.id.post_list);
rv.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(layoutManager);
rv.setAdapter(adapter);
Hope this will help you!!..
I m trying to display nested recyclerview but the child items does not display.
I want to display different items in all child view.
I don't get a error, but the view is not refreshed.
Here is my code can any one help.
Thanks
public class MainActivity extends ActionBarActivity {
RecyclerView recyclerView;
RootAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new RootAdapter(this);
recyclerView = (RecyclerView) findViewById(R.id.recyclerRoot);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
private class RootAdapter extends RecyclerView.Adapter<RootAdapter.RootViewHolder> {
private final LayoutInflater inflater;
String[] _items = new String[]{"ITEM 1", "ITEM 2", "ITEM 3", "ITEM 4"};
public RootAdapter(Context context)
{
inflater = LayoutInflater.from(context);
}
#Override
public RootViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = inflater.inflate(R.layout.root_row, viewGroup, false);
RootViewHolder rvi = new RootViewHolder(view);
return rvi;
}
#Override
public void onBindViewHolder(RootViewHolder rootViewHolder, int i) {
rootViewHolder.txtRootLine.setText(_items[i]);
rootViewHolder.recyclerViewChild.setLayoutManager(new LinearLayoutManager(inflater.getContext()));
rootViewHolder.recyclerViewChild.setAdapter(new ChildAdapter(inflater));
}
#Override
public int getItemCount() {
return _items.length;
}
class RootViewHolder extends RecyclerView.ViewHolder {
TextView txtRootLine;
RecyclerView recyclerViewChild;
public RootViewHolder(View itemView) {
super(itemView);
txtRootLine = (TextView) itemView.findViewById(R.id.txtRootLine);
recyclerViewChild = (RecyclerView) itemView.findViewById(R.id.recyclerChild);
}
}
}
private class ChildAdapter extends RecyclerView.Adapter<ChildAdapter.ChildViewHolder> {
private LayoutInflater _inflater;
String[] _childItems = new String[]{"child 1", "child 2", "child 2"};
public ChildAdapter(LayoutInflater inflater) {
_inflater = inflater;
}
#Override
public ChildViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = _inflater.inflate(R.layout.child_row, viewGroup, false);
ChildViewHolder rvi = new ChildViewHolder(view);
return rvi;
}
#Override
public void onBindViewHolder(ChildViewHolder childViewHolder, int i) {
childViewHolder.txtChildLine.setText(_childItems[i]);
}
#Override
public int getItemCount() {
return _childItems.length;
}
public class ChildViewHolder extends RecyclerView.ViewHolder {
TextView txtChildLine;
public ChildViewHolder(View itemView) {
super(itemView);
txtChildLine = (TextView) itemView.findViewById(R.id.txtChildLine);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="main text"/>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/recyclerRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
root_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtRootLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/recyclerChild"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
child_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtChildLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Existing layout manager does not support wrap content yet.
Test it by assigning a fixed height to your recyclerChild and the view would appear.
As a solution to this problem you can create a new LayoutManager that extends the existing one and overrides onMeasure method to measure for wrap content.
By Android Support Library 23.2 of a support library version 23.2.0. So all WRAP_CONTENT should work correctly.
Please update version of a library in gradle file.
compile 'com.android.support:recyclerview-v7:23.2.0'
RecyclerView does not support wrap_content.Set some value in nested recycler view like 200dp and your item will shows.
More discussion available here