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;
Related
This question already has answers here:
CardViews not showing in RecyclerView
(3 answers)
Closed 2 years ago.
activity_driver_list.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"
tools:context=".DriverListActivity">
<include
android:id="#+id/toolbarDL"
layout="#layout/toolbar"/>
<RelativeLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</LinearLayout>
fragment_driver_list.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".DLFragment.DriverListFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/driverList"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
raw_driver_list.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:elevation="5dp"
app:cardCornerRadius="4dp"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="95dp"
android:layout_height="95dp"
android:padding="10dp"
android:src="#drawable/ic_android"
app:civ_border_width="2dp"
app:civ_border_color="#color/quantum_lightgreen"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/carName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Suzuki Abcd"
android:text="SUZUKI Wagon r"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:textColor="#color/places_text_black_alpha_87"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/driverNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="0312-1234567"
android:text="0312-1234567"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:textColor="#color/cardview_dark_background"
android:textSize="15sp"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
DriverListAdapter.Java
public class DriverListAdapter extends RecyclerView.Adapter<DriverListAdapter.DriverListViewHolder> {
private ArrayList<Driver> mLst;
public class DriverListViewHolder extends RecyclerView.ViewHolder{
public CircleImageView mImage;
public TextView mCName, mDNumber;
public DriverListViewHolder(#NonNull View itemView) {
super(itemView);
mImage = itemView.findViewById(R.id.profile_image);
mCName = itemView.findViewById(R.id.carName);
mDNumber = itemView.findViewById(R.id.driverNumber);
}
}
#NonNull
#Override
public DriverListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.raw_driver_list, parent, false);
DriverListViewHolder driverListAdapter = new DriverListViewHolder(view);
return driverListAdapter;
}
#Override
public void onBindViewHolder(#NonNull DriverListViewHolder holder, int position) {
Driver driver = mLst.get(position);
holder.mImage.setImageResource(driver.getdImage());
holder.mCName.setText(driver.getdCName());
}
#Override
public int getItemCount()
{
return mLst.size();
}
public DriverListAdapter(ArrayList<Driver> lst) {
this.mLst = lst;
}
}
DriverListFragment.Java
public class DriverListFragment extends Fragment {
public static final String TAG = "DriverListFragment";
private ArrayList<Driver> driverLst;
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private DriverListAdapter mAdapter;
public DriverListFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_driver_list, container, false);
driverLst = new ArrayList<>();
driverLst.add(new Driver(R.drawable.ic_android,"Car One","0312-1234567"));
driverLst.add(new Driver(R.drawable.ic_android,"Car Two","0312-1234567"));
driverLst.add(new Driver(R.drawable.ic_android,"Car Three","0312-1234567"));
driverLst.add(new Driver(R.drawable.ic_android,"Car Four","0312-1234567"));
driverLst.add(new Driver(R.drawable.ic_android,"Car Five","0312-1234567"));
mRecyclerView = (RecyclerView) v.findViewById(R.id.driverList);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this.getActivity());
mAdapter = new DriverListAdapter(driverLst);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return v;
}
}
DriverListActivity.Java
public class DriverListActivity extends AppCompatActivity {
public static final String TAG = "DriverListActivity";
private TDD tdd = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_driver_list);
initToolbar();
getMyIntent();
driverList();
}
private void getMyIntent()
{
Intent i = getIntent();
tdd = (TDD) i.getSerializableExtra("tdd");
Log.d(TAG,tdd.toString());
}
private void initToolbar()
{
Toolbar toolbar = findViewById(R.id.toolbarDL);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Hello There..");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void driverList()
{
DriverListFragment driverListFragment = new DriverListFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer,driverListFragment);
fragmentTransaction.addToBackStack("driverListFragment");
fragmentTransaction.commit();
}
}
I want to show my RecyclerView list in fragment instead of an activity, when i call fragment in activity it simply showing me blank activity however, there's no error in code. How can i achieve this task or where i am doing a mistake?
How to build a Horizontal ListView with RecyclerView?
LinearLayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView myList = (RecyclerView) findViewById(R.id.my_recycler_view);
myList.setLayoutManager(layoutManager);
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.
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
I am using RecyclerView in a fragment, it is generate a NullPointerException and I cannot understand the reason.
Here is my fragment activity:
public class Recharges extends Fragment {
public RecyclerView recyclerView;
private List<GetRecharge> rechargeList = new ArrayList<>();
public RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
ImageView image1, image2;
#Nullable #Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
Toolbar myToolbar = (Toolbar) getActivity().findViewById(R.id.my_toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(myToolbar);
View rootView = inflater.inflate(R.layout.recharges, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview1);
recyclerView.setHasFixedSize(true);
final FragmentActivity c = getActivity();
layoutManager = new LinearLayoutManager(c);
recyclerView.setLayoutManager(layoutManager);
adapter = new Adapterrecharge(rechargeList);
recyclerView.setAdapter(adapter);
prepareRechargeData();
return rootView;
}
private void prepareRechargeData() {
GetRecharge recharge = new GetRecharge("Mad Max: Fury Road" );
rechargeList.add(recharge);
adapter.notifyDataSetChanged();
}
}
Here the adapter class:
public class Adapterrecharge extends RecyclerView.Adapter<Adapterrecharge.MyViewHolder> {
private List<GetRecharge> rechargeList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
ImageView image;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
}
}
public Adapterrecharge(List<GetRecharge> rechargeList) {
this.rechargeList = rechargeList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.rechargelist, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
GetRecharge recharge = rechargeList.get(position);
holder.title.setText(recharge.getTitle());
}
#Override
public int getItemCount() {
return rechargeList.size();
}
}
I seem to be inflating the correct layout but still getting the error.
here is the logcat error
java.lang.NullPointerException
at com.example.aadesh.walletuncle.Adapterrecharge.onBindViewHolder(Adapterrecharge.java:53)
at com.example.aadesh.walletuncle.Adapterrecharge.onBindViewHolder(Adapterrecharge.java:21)
here is the recyclerview item 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="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/text"/>
here is the main 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="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<TextView
android:text="Recharge"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:layout_weight="1" />
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:id="#+id/recyclerview1">
</android.support.v7.widget.RecyclerView>
there are couple of problems
you are populating prepareRechargeData() list data after you have set your adapter - so your list rechargelist doesn't have any data
in your onCreateViewHolder() your are using wrong layout R.layout.rechargelist
in your view Holder you are giving wrong id for textview R.id.title
try this:
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
Toolbar myToolbar = (Toolbar) getActivity().findViewById(R.id.my_toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(myToolbar);
View rootView = inflater.inflate(R.layout.recharges, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview1);
recyclerView.setHasFixedSize(true);
final FragmentActivity c = getActivity();
layoutManager = new LinearLayoutManager(c);
recyclerView.setLayoutManager(layoutManager);
/*recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter((RecyclerView.Adapter) adapter);*/
prepareRechargeData();
adapter = new Adapterrecharge(rechargeList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return rootView;
}
Change your adapter to:
public class Adapterrecharge extends RecyclerView.Adapter<Adapterrecharge.MyViewHolder> {
private List<GetRecharge> rechargeList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
ImageView image;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.text);
}
}
public Adapterrecharge(List<GetRecharge> rechargeList) {
this.rechargeList = rechargeList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_item_layout, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
GetRecharge recharge = rechargeList.get(position);
holder.title.setText(recharge.getTitle());
}
#Override
public int getItemCount() {
return rechargeList.size();
}
}
You are miss matching id of Textview.
Instead of
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/text"/>
</LinearLayout>
Use this
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/title"/>
</LinearLayout>
As you are using wrong id of Textview in MyViewHolder class.
The problem is due to mismatch ids. ID of TextView in xml ("text") and the one inflated in ViewHolder class ("title") are different.
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/text"/>
Adapter class
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
}
I am having problems displaying all the items in my recyclerview. My code only shows one item (Home. I have 3 items: Home, Browse Photos and My Travels)
fragment_navigation_drawer.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_navigation_drawer"
android:background="#EEE"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.traveldiaries.ztir.traveldiaries.NavigationDrawerFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="#16a085"
android:id="#+id/linearLayout">
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private LayoutInflater inflater;
List<MenuList> data = Collections.emptyList();
public RecyclerAdapter(Context context, List<MenuList> data){
inflater= LayoutInflater.from(context);
this.data = data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.itemlist,parent,false);
MyViewHolder holder= new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
MenuList current=data.get(position);
holder.title.setText(current.title);
holder.image.setImageResource(current.icon);
}
#Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.item_list);
image = (ImageView) itemView.findViewById(R.id.icons);
}
}
}
NavigationDrawerFragment.java (posted code only related to recyclerview)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
adapter = new RecyclerAdapter(getActivity(),getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<MenuList> getData(){
List<MenuList> data = new ArrayList<>();
String[] titles={"Home","My Travels","Browse Photos"};
int[] icons ={R.drawable.home,R.drawable.sunglasses,R.drawable.tooltip_image};
for(int i=0;i<3;i++){
MenuList current = new MenuList();
current.title=titles[i];
current.icon=icons[i];
Log.d("Data",titles[i]);
Log.d("Data",""+icons[i]);
data.add(current);
}
return data;
}
itemlist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:padding="8dp"
android:id="#+id/icons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="#+id/item_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dummy Text"
android:layout_gravity="center_vertical"
android:padding="8dp"
/>
</LinearLayout>
What I was able to do is to check all the passed data (all seems to be fine). Also tried to change width to wrap content (fragment_navigation_drawer.xml) in both my relative and linear layout but I just lose my background color instead and still only one item appear.
Change height of LinearLayout to wrap_content in itemlist.xml
android:layout_height="wrap_content"
Just change the height of your itemlist from "match_parent" to "wrap_content".