Android | RecyclerView doesn't work - android

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!!..

Related

Unable to show list in RecyclerView

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.

multiple recyclerviews?

I want to make my homepage with two row with recyclerview
with data of books and the events which are display independent of each other how can i accomplish it I want to add event to the my code how can i do it?
MoviesAdapter.java
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
private List<Movie> moviesList;
public Context context;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public ImageView thumbnail;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
thumbnail = (ImageView) view.findViewById(R.id.book);
}
}//End of MyViewHolder class
public MoviesAdapter(List<Movie> moviesList) {
this.moviesList = moviesList;
}
//display different items in the data set
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.movie_list_row, parent, false);
return new MyViewHolder(itemView);
}
//display data at specified location
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.title.setText(moviesList.get(position).getTitle());
holder.thumbnail.setImageResource(moviesList.get(position).getImage());
}
#Override
public int getItemCount() {
return moviesList.size();
}
}
I want to make my page like this
It should be fine to use a LinearLayout with 3 RecyclerViews in it and create an Adapter for each of them. Nested RecyclerViews would only make sense if you had a lot of categories.
I done it with the three recyclerview and their adapter class here is the code of only MainActivity.java and activity.xml
MainActivity.java
public class MainActivity extends AppCompatActivity {
private List<Book> bookList = new ArrayList<>();
private List<Author> authorList = new ArrayList<>();
private List<Event> eventList = new ArrayList<>();
private RecyclerView recyclerView1;
private RecyclerView recyclerView2;
private RecyclerView recyclerView3;
private BookAdapter bAdapter;
private AuthorAdapter aAdapter;
private EventAdapter eAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView1 = findViewById(R.id.recycler_book);
bAdapter = new BookAdapter(this,bookList);
recyclerView1.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView1.setLayoutManager(mLayoutManager);
recyclerView1.setAdapter(bAdapter);
prepareBookData();
//second recycler
recyclerView2 = findViewById(R.id.recycler_author);
aAdapter = new AuthorAdapter(this,authorList);
recyclerView2.setHasFixedSize(true);
RecyclerView.LayoutManager aLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView2.setLayoutManager(aLayoutManager);
recyclerView2.setAdapter(aAdapter);
prepareAuthorData();
//third recycler
recyclerView3 = (RecyclerView) findViewById(R.id.recycler_event);
eAdapter = new EventAdapter(this,eventList);
recyclerView3.setHasFixedSize(true);
RecyclerView.LayoutManager eLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
// RecyclerView.LayoutManager eLayoutManager = new GridLayoutManager(this,1,GridLayoutManager.HORIZONTAL,false);
recyclerView3.setLayoutManager(eLayoutManager);
recyclerView3.setAdapter(eAdapter);
prepareEventData();
}
private void prepareBookData(){
int[] drawableArray = {R.drawable.youcanwin, R.drawable.halfgirl};
String[] nameArray = {"You Can Win", "Half Girlfriend"};
Book a=new Book(nameArray[0],drawableArray[0]);
bookList.add(a);
Book b=new Book(nameArray[1],drawableArray[1]);
bookList.add(b);
Book c=new Book(nameArray[0],drawableArray[0]);
bookList.add(c);
bAdapter.notifyDataSetChanged();
}
private void prepareAuthorData(){
int[] drawableArray = {R.drawable.youcanwin, R.drawable.halfgirl};
String[] nameArray = {"You Can Win", "Half Girlfriend"};
Author a=new Author(nameArray[0],drawableArray[0]);
authorList.add(a);
Author b=new Author(nameArray[1],drawableArray[1]);
authorList.add(b);
Author c=new Author(nameArray[0],drawableArray[0]);
authorList.add(c);
Author d=new Author(nameArray[1],drawableArray[1]);
authorList.add(d);
bAdapter.notifyDataSetChanged();
}
private void prepareEventData(){
String[] desArray = {"new","old"};
String[] nameArray = {"You Can Win", "Half Girlfriend"};
Event a=new Event(nameArray[0],desArray[0]);
eventList.add(a);
Event b=new Event(nameArray[0],desArray[0]);
eventList.add(b);
Event c=new Event(nameArray[0],desArray[0]);
eventList.add(c);
bAdapter.notifyDataSetChanged();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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="fill_parent"
android:layout_height="wrap_content"
tools:context="com.example.hardik.threerecycler.MainActivity"
android:background="#e6edec">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
app:cardCornerRadius="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Books"
android:textSize="20sp"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_book"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="20dp" />
</android.support.v7.widget.CardView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Authors"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginTop="10dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_author"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="#+id/recycler_book"
android:layout_marginTop="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Events"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginTop="10dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_event"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="#+id/recycler_author"
android:layout_marginTop="5dp"/>
</LinearLayout>

error RecyclerView: No adapter attached; skipping layout when running program

I have tried answer at stackoverflow that relate to my problem. I wanna to show a data from firebase in imageview and textview. But there's error RecyclerView: No adapter attached; skipping layout. This is my coding :
MenuManajemenKendaraanFragment.java
public class MenuManajemenKendaraanFragment extends Fragment {
private RecyclerView recyclerView;
private MenuManajemenKendaraanAdapter adapter;
private DatabaseReference mDatabase;
//private ProgressDialog progressDialog;
private List<DataKendaraan> dataKendaraan;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getActivity().setTitle("Manajemen Kendaraan");
View v = inflater.inflate(R.layout.fragment_menu_manajemen_kendaraan, container, false);
FloatingActionButton fab_tambah_kendaraan = (FloatingActionButton) v.findViewById(R.id.fab);
mDatabase = FirebaseDatabase.getInstance().getReference();
recyclerView = (RecyclerView) v.findViewById(R.id.listViewKendaraan);
recyclerView.setHasFixedSize(true);
final FragmentActivity c = getActivity();
LinearLayoutManager layoutManager = new LinearLayoutManager(c);
recyclerView.setLayoutManager(layoutManager);
dataKendaraan = new ArrayList<>();
mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//dismissing the progress dialog
//iterating through all the values in database
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
DataKendaraan upload = postSnapshot.getValue(DataKendaraan.class);
dataKendaraan.add(upload);
}
adapter = new MenuManajemenKendaraanAdapter(getActivity(), dataKendaraan);
//adding adapter to recyclerview
recyclerView.setAdapter(adapter);
}
fragment_menu_manajemen_kendaraan.xml
<FrameLayout 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.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:clipToPadding="false"
android:padding="5dp"
android:scrollbars="vertical"
android:id="#+id/listViewKendaraan">
</android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
app:fabSize="normal"
android:src="#drawable/ic_add_fab"
android:layout_gravity="end|bottom"/>
</FrameLayout>
MenuManajemenKendraanAdapter.java
public class MenuManajemenKendaraanAdapter extends RecyclerView.Adapter<MenuManajemenKendaraanAdapter.ViewHolder> {
private Context context;
private List<DataKendaraan> dataKendaraan;
public MenuManajemenKendaraanAdapter(Context context, List<DataKendaraan> dataKendaraan) {
this.dataKendaraan = dataKendaraan;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.adapter_menu_manajemen_kendaraan, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
DataKendaraan data = dataKendaraan.get(position);
holder.tipeKendaraan.setText(data.getTipeKendaraan());
Glide.with(context).load(data.getUriFotoKendaraan()).into(holder.fotoKendaraan);
}
#Override
public int getItemCount() {
return dataKendaraan.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView tipeKendaraan;
public ImageView fotoKendaraan;
public ViewHolder(View itemView) {
super(itemView);
tipeKendaraan = (TextView)itemView.findViewById(R.id.tipe_kendaraan);
}
}
adapter_menu_manajemen_kendaraan.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_margin="5dp">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:id="#+id/imageViewFotoKendaraan"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:layout_marginRight="10dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="5">
<TextView
android:text="Tipe Kendaraan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-black"
android:textColor="#color/colorAccent"
android:paddingBottom="2dp"
android:id="#+id/tipe_kendaraan"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>
Thank you so much for your help.
View v = LayoutInflater.from(context) .inflate(R.layout.adapter_menu_manajemen_kendaraan, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;

Using RecyclerView Adapter - Error E/RecyclerView: No adapter attached; skipping layout

I am using a custom recycler adapter to display a text_view.xmlinto a fragment_list.xml. However, when I customise my fragment_list.xmladding a RelativeLayout the text_view is not displayed anymore.
I am getting this error message: E/RecyclerView: No adapter attached; skipping layout
This is my_text_view.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="normal|bold"
android:textColor="#color/black"
android:textAlignment="center"
android:text="This is my #string/cast_intro_overlay_button_text">
</TextView>
My fragment_item_list.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"
android:background="#color/background_color">
<Button
android:text="Back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttonBack"
android:layout_weight="0.08"
android:textSize="24sp"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_marginBottom="14dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#color/black"
android:textAlignment="center"
android:id="#+id/myTextView"
android:text="Best Scores"
android:textStyle="normal|bold"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp">
</TextView>
<android.support.v7.widget.RecyclerView android:id="#+id/list"
android:name="com.example.lucadigiammarino.biogame.ItemFragment"
android:layout_width="match_parent"
android:layout_height="250dp"
app:layoutManager="LinearLayoutManager"
tools:context="com.example.lucadigiammarino.biogame.ItemFragment"
tools:listitem="#layout/my_text_view"
android:isScrollContainer="false"
android:addStatesFromChildren="true"
android:layout_below="#+id/myTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="37dp">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
This is my MyPlayerAdapter class
class MyPlayerAdapter extends RecyclerView.Adapter{
ArrayList<Player> players;
/**
* Constructor for MyPlayerAdapter class
* #param players
*/
public MyPlayerAdapter(ArrayList<Player> players) {
this.players = players;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_text_view, parent, false);
MyViewHolder vh = new MyViewHolder((TextView) v);
return vh;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyViewHolder vh = (MyViewHolder)holder;
vh.mTextView.setText(players.get(position).name+", "+players.get(position).score);
}
#Override
public int getItemCount() {
return players.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
public TextView mTextView;
public MyViewHolder(TextView v) {
super(v);
mTextView = v;
}
}
}
And this is my OnCreateView() of my FragmentItem class
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new MyPlayerAdapter(Score.getInstance().getPlayers()));
}
return view;
}
I really don't know where to put my hands anymore. Appreciate help guys.
Thank you in advance
It's because your view object is not instanceof RecyclerView, but instance of RelativeLayout. So code inside your if-statement is never executed. Replace your code with this:
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list);
Context context = view.getContext();
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new MyPlayerAdapter(Score.getInstance().getPlayers()));
Here you get your recyclerView instance from the layout you inflated into view instance.

Android access Listview inside RecyclerView

I want to make a ListView inside a RecyclerView that should display some data in a list form. I currently have a basic Adapter and RecyclerView with no ListView inside of it, so can someone tell me how to modify my code to include a listview inside the RecyclerView?
Adapter:
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.AdapterViewHolder> {
public static class AdapterViewHolder extends RecyclerView.ViewHolder {
TextView title;
TextView summary;
AdapterViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.recycler_list_title);
summary = (TextView) itemView.findViewById(R.id.recycler_list_summary);
}
}
List<Items> items;
public RVAdapter(List<Items> items){
this.items = items;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public AdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_recycler, viewGroup, false);
AdapterViewHolder pvh = new AdapterViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(AdapterViewHolder adapterViewHolder, int i) {
adapterViewHolder.title.setText(item.get(i).title);
adapterViewHolder.summary.setText(item.get(i).summary);
}
#Override
public int getItemCount() {
return item.size();
}
}
Items:
public class Items {
String title;
String summary;
public Items(String title, String summary) {
this.title = title;
this.summary = summary;
}
}
Layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_marginTop="0dp" />
</LinearLayout>
list_recycler:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="6dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp">
<TextView
android:id="#+id/recycler_list_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/textColor"
android:textSize="16sp"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/recycler_list_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/textColorSub"
android:textSize="14sp"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
How I'm creating the ListView:
private List item;
private RecyclerView rv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
rv = (RecyclerView) ((Activity) MainActivity.context).findViewById(R.id.recycler);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(this));
initializeData();
initializeAdapter();
}
private void initializeData(){
item = new ArrayList<>();
item.add(new Items("Title", "Summary"));
}
private void initializeAdapter() {
RVAdapter adapter = new RVAdapter(item);
rv.setAdapter(adapter);
}
}
ListView listview = (ListView) findViewById(R.id.listview);
ListView parentListView=(ListView)listview.getParent();
you can get parent listView by this way

Categories

Resources