FirebaseRecyclerAdapter - is just showing a blank screen, I get no errors when compiling and no errors while scrolling around. I've added Adapter().startListening(); to the onStart, I've added data to firebase in the orders child. I'd be greatful for any help.
Here is my FirebaseRecyclerAdapter,
private FirebaseRecyclerAdapter<Orders, OrderViewHolder> Adapter()
{
Query query = mDatabaseReference.limitToLast(50);
final FirebaseRecyclerOptions<Orders> options =
new FirebaseRecyclerOptions.Builder<Orders>()
.setQuery(query, Orders.class)
.build();
return new FirebaseRecyclerAdapter<Orders, OrderViewHolder>(options) {
#Override
public OrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.order_single_layout, parent, false);
return new OrderViewHolder(view);
}
#Override
protected void onBindViewHolder(OrderViewHolder ViewHolder, int position, Orders orders) {
ViewHolder.setOrderID(orders.getOrder_id());
ViewHolder.setProductName(orders.getProduct_name());
ViewHolder.setStatus(orders.getStatus());
}
};
}
My Viewholder
public static class OrderViewHolder extends RecyclerView.ViewHolder
{
View mView;
private OrderViewHolder(View itemView)
{
super(itemView);
mView = itemView;
}
public void setOrderID(String orderID)
{
TextView orderIdTextView = (TextView) mView.findViewById(R.id.orderSingle_OrderIDTextView);
orderIdTextView.setText(orderID);
}
public void setStatus(String status)
{
TextView statusTextView = (TextView) mView.findViewById(R.id.orderSingle_StatusTextView);
statusTextView.setText(status);
}
public void setProductName(String productName)
{
TextView productNameTextView = (TextView) mView.findViewById(R.id.orderSingle_ProductNameTextView);
productNameTextView.setText(productName);
}
}
on Create
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orders);
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("orders");
adapter = Adapter();
mOrderList = (RecyclerView) findViewById(R.id.order_RecyclerView);
mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mOrderList.setHasFixedSize(true);
mOrderList.setLayoutManager(mLayoutManager);
mOrderList.setAdapter(adapter);
}
activityOrder.xml
<RelativeLayout
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.chat.OrdersActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/order_RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_alignParentEnd="true">
</android.support.v7.widget.RecyclerView>
The messageView for each message intended to go into the RecyclerView
RelativeLayout
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">
<TextView
android:id="#+id/orderSingle_ProductNameTextView"
style="#style/textView_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:textColor="#000000"
android:text="Product"
android:textSize="18sp" />
<TextView
android:id="#+id/orderSingle_StatusTextView"
style="#style/textView_style"
android:text="Status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/orderSingle_ProductNameTextView"
android:layout_below="#+id/orderSingle_ProductNameTextView"
android:layout_marginTop="12dp"
android:textColor="#000000"/>
<TextView
android:id="#+id/orderSingle_OrderIDTextView"
style="#style/textView_style"
android:text="order_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/orderSingle_ProductNameTextView"
android:layout_below="#+id/orderSingle_StatusTextView"
android:layout_marginTop="12dp"
android:textColor="#000000" />
Got it to start working by adding .setLifecycleOwner(this) to
final FirebaseRecyclerOptions<Orders> options =
new FirebaseRecyclerOptions.Builder<Orders>()
.setQuery(query, Orders.class)
.build();
No idea why it works now but it does
Related
Child RecyclerView inside Parent RecyclerView is showing at End of Parent RecyclerView ?
I Want To Show Categroy in Parent RecyclerView & show its Sub Category in Child RecyclerView
But When I Click Parent RecyclerView To Show That Sub Category the Child RecyclerView is showing at The Full End Of Parent Category Please Check Where Is I'm Wrong?
Here is My Results:
First Item Clicked
.
Second Item Clicked
My Code:
This Is My Main 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".USER.Category">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Category"/>
</LinearLayout>
This is my Main java File:
public class Category extends AppCompatActivity {
static Toast Toasts;
RecyclerView Category;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
Category = findViewById(R.id.Category);
Category.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
Category_View_request();
}
public void Category_View_request() {
RequestQueue requestQueue = Volley.newRequestQueue(Category.this);
String url = Constant.Category;
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
GridView_listItems[] Category_View_response = gson.fromJson(response, GridView_listItems[].class);
Category.setAdapter(new CategoryView_Adapter(Category.this, Category_View_response));
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toasts = Toast.makeText(Category.this, "ERROR: Please Check Internet & Try Again", Toast.LENGTH_SHORT);
Toasts.show();
}
});
requestQueue.add(stringRequest);
}
}
this is Parent Layout:
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="5dp"
android:id="#+id/layout">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:id="#+id/nameLayout">
<ImageView
android:id="#+id/categoryimage"
android:layout_width="120dp"
android:layout_height="120dp"
android:adjustViewBounds="true"
android:background="#fff"
android:contentDescription="#string/todo"
android:padding="8dp"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:paddingRight="8dp"
android:paddingBottom="8dp">
<Space
android:layout_width="112dp"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:paddingRight="8dp"
android:paddingBottom="8dp">
<TextView
android:id="#+id/categoryname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="1dp"
android:singleLine="true"
android:text="#string/test_product_name"
android:textColor="#color/Black"
android:textSize="13sp"
tools:ignore="RtlSymmetry" />
<TextView
android:id="#+id/categorydec"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="1dp"
android:paddingTop="3dp"
android:singleLine="true"
android:text="#string/test_product_description"
android:textColor="#color/common_google_signin_btn_text_light_default"
android:textSize="12sp"
tools:ignore="RtlSymmetry" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/click">
</RelativeLayout>
</RelativeLayout>
<androidx.cardview.widget.CardView
android:id="#+id/subview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="#color/White"
android:elevation="8dp"
android:paddingTop="10dp"
>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/subCatview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</androidx.cardview.widget.CardView>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
This is Parent Adapter:
public class CategoryView_Adapter extends RecyclerView.Adapter<CategoryView_Adapter.CategoryViewHolder> {
Toast Toasts;
private static Context context;
private static GridView_listItems[] data;
public CategoryView_Adapter(Context context, GridView_listItems[] data) {
this.context = context;
this.data = data;
}
#NonNull
#Override
public CategoryViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.category_view, parent, false);
return new CategoryViewHolder(view);
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(#NonNull final CategoryViewHolder holder, int position) {
final GridView_listItems CategoryList = data[position];
holder.categoryname.setText(CategoryList.getName());
holder.categorydec.setText(CategoryList.getId());
Glide.with(holder.categoryimage.getContext())
.applyDefaultRequestOptions(new RequestOptions()
.placeholder(R.drawable.product_loading)
.error(R.drawable.product_loading_failed))
.load(Constant.MAINImagUrl + CategoryList.getCategoryLogo().substring(2))
.into(holder.categoryimage);
holder.categoryname.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.layout.setBackgroundColor(ContextCompat.getColor(context, R.color.common_google_signin_btn_text_light_default));
Grid_View_request(CategoryList.getName());
}
});
}
#Override
public int getItemCount() {
return data.length;
}
public static class CategoryViewHolder extends RecyclerView.ViewHolder {
ImageView categoryimage;
TextView categoryname, categorydec;
CardView subview;
RelativeLayout click;
LinearLayout layout;
public static RecyclerView subCatview;
public CategoryViewHolder(#NonNull View itemView) {
super(itemView);
categoryimage = (ImageView) itemView.findViewById(R.id.categoryimage);
categoryname = (TextView) itemView.findViewById(R.id.categoryname);
categorydec = (TextView) itemView.findViewById(R.id.categorydec);
click = (RelativeLayout) itemView.findViewById(R.id.click);
subview = (CardView) itemView.findViewById(R.id.subview);
layout = (LinearLayout) itemView.findViewById(R.id.layout);
subCatview = (RecyclerView) itemView.findViewById(R.id.subCatview);
GridLayoutManager gridLayoutManager = new GridLayoutManager(MyApplication.getAppContext(), 3, GridLayoutManager.VERTICAL, false);
subCatview.setLayoutManager(gridLayoutManager);
}
}
public static void Grid_View_request(final String category) {
RequestQueue requestQueue = Volley.newRequestQueue(MyApplication.getAppContext());
String url = Constant.Sub_Category;
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if (response.contains("CityGroceryEmptyResponse")) {
Toast.makeText(MyApplication.getAppContext(), "No Sub Category Found", Toast.LENGTH_SHORT).show();
} else {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Sub_Category_list[] Sub_Category_list = gson.fromJson(response, Sub_Category_list[].class);
subCatview.setAdapter(new Sub_Category_Adapter(MyApplication.getAppContext(), Sub_Category_list));
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MyApplication.getAppContext(), "ERROR: Please Check Internet & Try Again", Toast.LENGTH_SHORT).show();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("category", category);
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
requestQueue.add(stringRequest);
}
}
This is My Child Layout:
<androidx.cardview.widget.CardView 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="120dp"
android:layout_gravity="center"
android:layout_margin="2dp"
android:orientation="horizontal"
android:elevation="8dp"
app:cardCornerRadius="1dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:paddingRight="8dp"
android:paddingBottom="8dp">
<ImageView
android:id="#+id/subCatNameimage"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/subCatName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/subCatNameimage"
android:layout_marginTop="4dp"
android:gravity="center"
android:maxLength="30"
android:text="menu name"
android:textColor="#color/colorPrimary"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/subCatNameimage" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
This is My Child Adapter:
public class Sub_Category_Adapter extends RecyclerView.Adapter<Sub_Category_Adapter.CategoryViewHolder> {
Toast Toasts;
private static Context context;
private static Sub_Category_list[] data;
public Sub_Category_Adapter(Context context, Sub_Category_list[] data) {
this.context = context;
this.data = data;
final Sub_Category_list SubCategoryList = data[0];
}
#NonNull
#Override
public CategoryViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.gridview_items, parent, false);
return new CategoryViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final CategoryViewHolder holder, final int position) {
final Sub_Category_list SubCategoryList = data[position];
holder.subCatName.setText(SubCategoryList.getSubCategoryName());
Glide.with(holder.subCatNameimage.getContext())
.applyDefaultRequestOptions(new RequestOptions()
.placeholder(R.drawable.product_loading)
.error(R.drawable.product_loading_failed))
.load(Constant.MAINImagUrl + SubCategoryList.getSubCategoryLogo().substring(2))
.into(holder.subCatNameimage);
}
#Override
public int getItemCount() {
return data.length;
}
public static class CategoryViewHolder extends RecyclerView.ViewHolder {
ImageView subCatNameimage;
TextView subCatName;
public CategoryViewHolder(#NonNull View itemView) {
super(itemView);
subCatName = (TextView) itemView.findViewById(R.id.subCatName);
subCatNameimage = (ImageView) itemView.findViewById(R.id.subCatNameimage);
}
}
}
Please Correct Me Where is doing Mistake
Thanks In Advance
You can achieve that requirement by using an expandable recycler view.
There are a lot of libraries you can use to achieve this.
You can use this library.
Or follow this post to achieve this on your own, you might need to make some changes to get the output as per your requirement.
I'am achieve My requirement by using an expandable recycler view
HERE
I tried the solutions on the other similar posts that I found here but they did not help me. I hope someone can help.
I get data from Unsplash's API and the ArrayList contains 10 items once I get a response, that's why I think It has to be something with the way the view is inflated.
This is code:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "unsplash";
private final String CLIENT_ID = "...myclientID...";
// testing keyword used on the API to search for pictures
private final String KEYWORD = "stackOverflow";
private final String urlBase = "https://api.unsplash.com/";
private Retrofit retrofit;
private RecyclerView recyclerView;
private RecyclerViewAdapter recyclerViewAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
retrofit = new Retrofit.Builder()
.baseUrl(urlBase)
.addConverterFactory(GsonConverterFactory.create())
.build();
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
recyclerViewAdapter = new RecyclerViewAdapter();
recyclerView.setAdapter(recyclerViewAdapter);
getData();
}
private void getData() {
PictureService service = retrofit.create(PictureService.class);
Call<PictureResponse> pictureResponseCall = service.getPictureList(KEYWORD, CLIENT_ID);
pictureResponseCall.enqueue(new Callback<PictureResponse>() {
#Override
public void onResponse(Call<PictureResponse> call, Response<PictureResponse> response) {
if (response.isSuccessful()){
PictureResponse pictureResponse = response.body();
ArrayList<UnsplashPic> pictureList = pictureResponse.getResults();
recyclerViewAdapter.addPictures(pictureList);
}else{
Log.e (TAG, " onResponse " + response.errorBody());
}
}
#Override
public void onFailure(Call<PictureResponse> call, Throwable t) {
Log.e (TAG, " onFailure " + t.getMessage());
}
});
}
}
My adapter class:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private ArrayList<UnsplashPic> pictureList;
public RecyclerViewAdapter (){
pictureList = new ArrayList<>();
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_view_holder, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
UnsplashPic pic = pictureList.get(i);
viewHolder.artistName.setText(pic.user.getUsername());
viewHolder.unsplash.setText("Unsplash");
}
#Override
public int getItemCount() {
return pictureList.size();
}
public void addPictures(ArrayList<UnsplashPic> pictureList) {
pictureList.addAll(pictureList);
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView artistName;
private TextView unsplash;
public ViewHolder (View itemView){
super(itemView);
imageView = itemView.findViewById(R.id.imageview);
artistName = itemView.findViewById(R.id.artist_name);
unsplash = itemView.findViewById(R.id.unsplash_name);
}
}
}
item_view_holder.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="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageview"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#ababab"
android:contentDescription="unsplash picture" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Photo by "/>
<TextView
android:id="#+id/artist_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="on "/>
<TextView
android:id="#+id/unsplash_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true" />
</LinearLayout>
activity_main.xml
<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=".MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Search for pictures"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerview">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Here:
public void addPictures(ArrayList<UnsplashPic> pictureList) {
pictureList.addAll(pictureList);
notifyDataSetChanged();
}
you are actually adding the content of the pictureList you send as parameter of the function to that same list, your list from adapter is not updated.
You should do like this instead:
public void addPictures(ArrayList<UnsplashPic> pictureList) {
this.pictureList.addAll(pictureList);
notifyDataSetChanged();
}
I'm trying to adapt a recyclerview to display a list of objects. But actually I have some kind of wierd bug. My object have ID and Name data to display, but in the list, at first, I only can see the Id, and when I scroll down, only the rows going completely out of the screen are completely displayed...
This is my ListPresentActivity.
public class ListPresentActivity extends AppCompatActivity implements ViewInterface {
private static final String EXTRA_PRESENT_ID = "EXTRA_PRESENT_ID";
private static final String EXTRA_PRESENT_NAME = "EXTRA_PRESENT_NAME";
private List<PresentItem> listOfPresent;
private LayoutInflater layoutInflater;
private RecyclerView recyclerView;
private CustomAdapter adapter;
private PresentController presentController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_present);
recyclerView = (RecyclerView) findViewById(R.id.rec_list_activity);
recyclerView.setHasFixedSize(true);
layoutInflater = getLayoutInflater();
presentController = new PresentController(this, new FakePresentModel());
}
#Override
public void startDetailActivity(int id, String name, String info, String target, String advice, String price) {
//public void startDetailActivity(int id) {
Intent i = new Intent(this,PresentDetailActivity.class);
i.putExtra(EXTRA_PRESENT_ID, id);
i.putExtra(EXTRA_PRESENT_NAME, name);
startActivity(i);
}
#Override
public void setUpAdapterAndView(List<PresentItem> listOfPresent) {
this.listOfPresent = listOfPresent;
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new CustomAdapter();
recyclerView.setAdapter(adapter);
}
private class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder>{
#Override
public CustomAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = layoutInflater.inflate(R.layout.item_present, parent, false);
return new CustomViewHolder(v);
}
#Override
public void onBindViewHolder(CustomAdapter.CustomViewHolder holder, int position) {
PresentItem currentPresent = listOfPresent.get(position);
holder.id.setText(
Integer.toString(currentPresent.getId())
);
holder.name.setText(
currentPresent.getName()
);
}
#Override
public int getItemCount() {
return listOfPresent.size();
}
class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView id;
private TextView name;
private ViewGroup container;
public CustomViewHolder(View itemView){
super(itemView);
this.id = (TextView) itemView.findViewById(R.id.texv_item_id);
this.name = (TextView) itemView.findViewById(R.id.texv_item_name);
this.container = (ViewGroup) itemView.findViewById(R.id.root_list_present);
this.container.setOnClickListener(this);
}
#Override
public void onClick(View v) {
PresentItem presentItem = listOfPresent.get(
this.getAdapterPosition()
);
presentController.onListItemClick(presentItem);
}
}
}
}
The layout item_present.xml
<android.support.constraint.ConstraintLayout 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="88dp"
android:id="#+id/root_list_present"
xmlns:tools="http://schemas.android.com/tools">
<TextView
android:id="#+id/texv_item_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:textSize="48sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="#id" />
<TextView
android:id="#+id/texv_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="84dp"
android:layout_marginTop="8dp"
android:ellipsize="end"
android:maxLines="1"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="collier" />
</android.support.constraint.ConstraintLayout>
And my list layout
<android.support.constraint.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"
tools:context="net.lubbee.bruh.view.ListPresentActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/rec_list_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
I have search for a long time now, and the only concording result I have fount seems to be fixed with the recyclerView.setHasFixedSize(true); which is not my case...
Thanks for your time!
PS : If there is some code parts needed missing, just say the word! :]
Just after the first loading.
After one scroll to the bottom and back to the top of the screen
<LinearLayout android:layout_width="match_parent"
android:layout_height="88dp"
android:weightSum="1"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_gravity="center_vertical"
android:layout_weight="0.2"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:layout_gravity="center_vertical"
android:layout_weight="0.8"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
Try this simple LinearLayout it should work
You can also add other properties, but for now try it out
I am not getting any view from the recycler, I did some debug and the constructor of the adapter is called with items on its list, also the method
getItemCount()
but none of the rest seem to be executed.
I´ve been wandering around stack looking for this problem and i found different approaches to a solution, problem is none worked out for me, maybe I am just missing something i can´t see.
I read somewhere that it could be because the recycler view is inside an scroll view, but the solution given did not worked for me
my custom adapter:
public class CircumstancesAdapter extends RecyclerView.Adapter<CircumstancesAdapter.CircumstancesViewHolder> {
List<CatalogRespModel> circumList;
Context mContext;
public CircumstancesAdapter(List<CatalogRespModel> list, Context context) {
this.circumList = list;
this.mContext = context;
}
#Override
public CircumstancesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.row_item_circumstances, parent, false);
return new CircumstancesViewHolder(view);
}
#Override
public void onBindViewHolder(CircumstancesViewHolder holder, int position) {
holder.tvCircum.setText(circumList.get(position).get_Descripcion());
}
#Override
public int getItemCount() {
if(circumList != null) return circumList.size();
else return 0;
}
public class CircumstancesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
RadioButton rbCircum;
TextView tvCircum;
private Context context;
public CircumstancesViewHolder(View itemView) {
super(itemView);
rbCircum = (RadioButton) itemView.findViewById(R.id.rb_circ);
rbCircum.setOnClickListener(this);
tvCircum = (TextView) itemView.findViewById(R.id.tv_circ);
this.context = itemView.getContext();
}
#Override
public void onClick(View v) {
}
}
}
my container layout:
<FrameLayout 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">
<!-- TODO: Update blank fragment layout -->
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:id="#+id/text2"
android:text="#string/text_circ"
android:textSize="20sp"
android:textColor="#color/colorPrimaryshadow"/>
<RelativeLayout
android:id="#+id/placa"
android:layout_width="180dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:background="#drawable/placa">
<TextView
android:id="#+id/tv_plate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:layout_centerInParent="true"
android:text="#string/dummy"
android:textSize="40sp"
android:textColor="#android:color/black"/>
</RelativeLayout>
<ImageView
android:id="#+id/iv_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/conductora" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rview_circumstances"
android:layout_width="match_parent"
android:background="#fff"
android:orientation="vertical"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/boton_continuar_circustanciasa"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/continuar"
android:layout_marginBottom="10dp"
android:elevation="30dp"
android:layout_gravity="center"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</FrameLayout>
my row layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#fff"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/rb_circ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#color/colorAccentDark"
android:gravity="center_vertical"
android:textSize="14sp" />
<TextView
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:id="#+id/tv_circ"
android:textSize="14sp"
android:text="texto de prueba"
android:textColor="#color/colorAccentDark"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
and in my onCreateView inside the fragment:
recyclerView = (RecyclerView) rootView.findViewById(R.id.rview_circumstances);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(true);
if(Catalogs.circumstancesArray.size() > 0){
adapter = new CircumstancesAdapter(Catalogs.circumstancesArray, getContext());
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(adapter);
}
Note: I checked the List size and is greater than 0
Fragment code as requested:
public class CircumstancesFragment extends Fragment {
Button btnContinue;
TextView tvPlate;
private ImageView ivIndicator;
private static int iNumDevices;
IFragmentListener iFragmentListener;
private RecyclerView recyclerView;
RecyclerView.Adapter adapter;
private final static String TAGMap = CircumstancesFragment.class.getSimpleName();
public static final String TAG = "CircumstancesFragment.java";
public static CircumstancesFragment getInstance(Bundle bundle){
if(bundle != null){
iNumDevices = bundle.getInt("numDevices");
}
return new CircumstancesFragment();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if(context != null){
if(context instanceof IFragmentListener){
iFragmentListener = (IFragmentListener) context;
} else
throw new RuntimeException("el contexto no esta implementando la interfaz");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootView = inflater.inflate(R.layout.fragment_circustancias_, container, false);
btnContinue = (Button) rootView.findViewById(R.id.boton_continuar_circustanciasa);
tvPlate = (TextView) rootView.findViewById(R.id.tv_plate);
recyclerView = (RecyclerView) rootView.findViewById(R.id.rview_circumstances);
ivIndicator = (ImageView) rootView.findViewById(R.id.iv_indicator);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(true);
if(Catalogs.circumstancesArray.size() > 0){
adapter = new CircumstancesAdapter(Catalogs.circumstancesArray, getContext());
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
btnContinue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
try
{
iFragmentListener.notify(null, TAG);
} catch (Exception ex)
{
Log.d("error", ex.getMessage());
}
}
});
setLayout(iNumDevices);
return rootView;
}
private void setLayout(int i){
tvPlate.setText(FdccoreConstants.insuredArray.get(0).get_Placa_Vehiculo());
if(i== FdccoreConstants.ONE_DEVICE){
ivIndicator.setImageResource(R.drawable.conductora);
}else if(i== FdccoreConstants.TWO_DEVICE){
ivIndicator.setImageResource(R.drawable.only_conductor);
}
}
private void resetView(){
}
}
Please provide more code because Your layout and adapter is working fine.
I only changed Your model list to String.
Maybe in place when You checked list size and list.size() > 0 add
adapter.notifyDataSetChanged();
=====
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rview_circumstances);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(true);
list = new ArrayList<>();
list.add("asd");
list.add("sasf");
list.add("asasfasf");
list.add("asgagsad");
list.add("asgas");
list.add("asagasgd");
list.add("asreyeryd");
list.add("asgsheyd");
CircumstancesAdapter adapter = new CircumstancesAdapter(list, this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
solution: somehow in order to work with the scroll view i must add this line
compile 'com.android.support:recyclerview-v7:25.3.1'
compiling only appcompat-v7 wont work
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;