How to test a recycler view adapter in Android Studio - android

Can someone help me test this recycler view adapter? I wan't to make a local unit test, but doesn't really know how to begin?
Following is my recyclerViewAdapter class:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {
private List<TasksTask> task;
protected Context context;
public RecyclerViewAdapter(Context context, List<TasksTask> task) {
this.task = task;
this.context = context;
}
#Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerViewHolders viewHolder = null;
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.to_do_list, parent, false);
viewHolder = new RecyclerViewHolders(layoutView, task);
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
holder.categoryTitle.setText(task.get(position).getTask());
}
#Override
public int getItemCount() {
return this.task.size();
}}
And here is my recyclerViewHolder class:
public class RecyclerViewHolders extends RecyclerView.ViewHolder {
private static final String TAG = RecyclerViewHolders.class.getSimpleName();
public TextView categoryTitle;
public ImageView deleteIcon;
private List<TasksTask> taskObject;
private FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
private FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
private DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
public RecyclerViewHolders(final View itemView, final List<TasksTask> taskObject) {
super(itemView);
this.taskObject = taskObject;
categoryTitle = (TextView) itemView.findViewById(R.id.task_title);
deleteIcon = (ImageView) itemView.findViewById(R.id.task_delete);
deleteIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String taskTitle = taskObject.get(getAdapterPosition()).getTask();
mDatabase.child("Users").child(firebaseUser.getUid()).child("Tasks").child(taskTitle).removeValue();
}
});
}
}

I've been wondering that myself and have found this:
https://chelseatroy.com/2015/09/27/android-examples-a-test-driven-recyclerview/
there are example tests for each adapter method in there, hope that helps

Related

RecyclerView only showing first product from Firestore

I'm trying to create a small android app but i can't get this activity work.
This is the Dashboard activity where i expect to see all products but it shows only the first one, the section where they should be shown gets bigger but the other object are not present, anywhere i press on this area the activity related to the first product is started.
public class DashBoardActivity extends AppCompatActivity {
ActivityDashBoardBinding binding;
private ProductsAdapter productsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityDashBoardBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.myToolbar);
productsAdapter = new ProductsAdapter(this);
binding.productRecycler.setAdapter(productsAdapter);
binding.productRecycler.setLayoutManager(new LinearLayoutManager(this));
getProducts();
binding.cart.setOnClickListener(view -> {
startActivity(new Intent(DashBoardActivity.this, CartActivity.class));
});
}
private void getProducts(){
FirebaseFirestore.getInstance()
.collection("products")
.whereEqualTo("show", true)
.get()
.addOnSuccessListener(queryDocumentSnapshots -> {
List<DocumentSnapshot> dsList = queryDocumentSnapshots.getDocuments();
for(DocumentSnapshot ds:dsList){
ProductModel productModel = ds.toObject(ProductModel.class);
productsAdapter.addProduct(productModel);
}
});
}
}
public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.MyViewHolder>{
private Context context;
private List<ProductModel> productModelList;
public ProductsAdapter(Context context){
this.context = context;
productModelList = new ArrayList<ProductModel>();
}
public void addProduct(ProductModel productModel){
productModelList.add(productModel);
notifyDataSetChanged();
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_row, parent, false);
return new MyViewHolder(view);
}
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
ProductModel productModel = productModelList.get(position);
holder.title.setText(productModel.getTitle());
holder.description.setText(productModel.getDescription());
holder.price.setText(productModel.getPrice());
Glide.with(context).load(productModel.getImage())
.into(holder.img);
holder.itemView.setOnClickListener(view -> {
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("model", productModel);
context.startActivity(intent);
});
}
#Override
public int getItemCount() {
return productModelList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
private TextView title, description, price;
private ImageView img;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
description = itemView.findViewById(R.id.description);
price = itemView.findViewById(R.id.price);
img = itemView.findViewById(R.id.image);
}
}
}

How do I show info from two different models / adapters in one recyclerview

I have two Models (ModelChat and ModelUsers) and their class adapters (PostsAdapter and AllUsersAdapter).
I want to get and set user info such as name and email from the ModelUser and get and set post information from the ModelPost from Firebase database into the same recyclerview in my HomeActivity.
The codes are as shown below:
PostsAdapter.java
public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.MyHolder>{
Context context;
List<ModelPost> postList;
public PostsAdapter(Context context, List<ModelPost> postList) {
this.context = context;
this.postList = postList;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//inflate layout row_post.xml
View view = LayoutInflater.from(context).inflate(R.layout.row_posts, parent, false);
return new MyHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyHolder holder, int position) {
//get data
String userUID = postList.get(position).getUserUID();
String userEmail = postList.get(position).getUserEmail();
String userProfilePic = postList.get(position).getUserProfilePic();
String firstName = postList.get(position).getUserFirstName();
String lastName = postList.get(position).getUserLastName();
String userNickName = postList.get(position).getUserNickName();
String postTimeStamp = postList.get(position).getPostTime();
String userPost = postList.get(position).getUserPost();
//set data
//set data
Glide
.with(context)
.load(userProfilePic)
.apply(RequestOptions.circleCropTransform())
.into(holder.avatarTv);
holder.firstnameTv.setText(firstName);
holder.lastnameTv.setText(lastName);
holder.nicknameTv.setText(userNickName);
}
#Override
public int getItemCount() {
return postList.size();
}
//view holder class
class MyHolder extends RecyclerView.ViewHolder {
//views from row_posts layout
public MyHolder(#NonNull View itemView) {
super(itemView);
//init view
//my views here
}
}
}
AllUsersAdapter.java
public class AllUsersAdapter extends RecyclerView.Adapter<AllUsersAdapter.MyHolder>{
Context context;
List<ModelUser> userList;
//Constructor > Generate
public AllUsersAdapter(Context context, List<ModelUser> userList) {
this.context = context;
this.userList = userList;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//Inflate layout (row_user)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_all_users, parent, false);
return new MyHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyHolder holder, int position) {
//get data
String userUID = userList.get(position).getUid();
String userProfilePic = userList.get(position).getProfilepic();
String userName = userList.get(position).getFirstname() + " " + userList.get(position).getLastname();
String userNickname = userList.get(position).getNickname();
String userStatus = userList.get(position).getStatus();
//set data
Glide
.with(context)
.load(userProfilePic)
.apply(RequestOptions.circleCropTransform())
.into(holder.mAvatarIv);
holder.mNameTv.setText(userName);
holder.mNicknameTv.setText(userNickname);
holder.mStatusTv.setText(userStatus);
//Handle item click
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*Click user from user list to start conversation
*Start Activity by putting UID of receiver
*we will use that UID to identify the user we are gonna chat */
Intent intent = new Intent(context, MessageActivity.class);
intent.putExtra("userUid", userUID);
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return userList.size();
}
class MyHolder extends RecyclerView.ViewHolder {
public MyHolder(#NonNull View itemView) {
super(itemView);
//init my views here
}
}
}
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
FirebaseAuth firebaseAuth;
RecyclerView recyclerView;
List<ModelPost> postList;
PostsAdapter postsAdapter;
String mUID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
firebaseAuth = FirebaseAuth.getInstance();
//recyclerview and its properties
recyclerView = findViewById(R.id.posts_recyclerview);
//init post list
postList = new ArrayList<>();
loadPosts();
}
private void loadPosts() {
//path of all posts
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
//get all data from this reference
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
postList.clear();
for (DataSnapshot ds: snapshot.getChildren()) {
ModelPost modelPost = ds.getValue(ModelPost.class);
postList.add(modelPost);
//adapter
postsAdapter = new PostsAdapter(HomeActivity.this, postList);
//set adapter to recyclerview
recyclerView.setAdapter(postsAdapter);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
//in case of errors
//Toast.makeText(HomeActivity.this, ""+error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
Try using ConcatAdapter.
An RecyclerView.Adapter implementation that presents the contents of multiple adapters in sequence. More info
Concatenate adapters sequentially with ConcatAdapter:
https://medium.com/androiddevelopers/merge-adapters-sequentially-with-mergeadapter-294d2942127a
I think this will help
MyAdapter adapter1 = ...;
AnotherAdapter adapter2 = ...;
ConcatAdapter concatenated = new ConcatAdapter(adapter1, adapter2);
recyclerView.setAdapter(concatenated);

ItemView.setOnCLickListiner is Being Ignored and does not get clicked when it is clicked

please can anyone help me fix this my itemview is not being clicked wen i click it in my fragment after implementing my onItemClickListener interface in my adapter, i also called the adapter in my fragment but if i run the app and i click d cardview nothing happens and am using list adapter which extends recyclerView. please if anyone can help me i will b grateful
my adapter
public class Apiary_Adapter extends ListAdapter<RecordModel,Apiary_Adapter.ApiaryHolder> {
Apiaries apiaries;
Context context;
RecordOnClickListener listener;
public RecordViewModel record_View_model = Apiaries.record_View_model;
public Apiary_Adapter(Apiaries apiaries, Context context ) {
super(DIFF_CALLBACK);
this.apiaries = apiaries;
this.context = context;
}
private static final DiffUtil.ItemCallback<RecordModel> DIFF_CALLBACK = new DiffUtil.ItemCallback<RecordModel>() {
#Override
public boolean areItemsTheSame(#NonNull RecordModel oldItem, #NonNull RecordModel newItem) {
return oldItem.getId() == newItem.getId();
}
#Override
public boolean areContentsTheSame(#NonNull RecordModel oldItem, #NonNull RecordModel newItem) {
return oldItem.getAddress().equals(newItem.getAddress())&&oldItem.getApiaryNo().equals(newItem.getApiaryNo())
&&oldItem.getTown().equals(newItem.getTown())&&oldItem.getApiaryLongitude().equals(newItem.getApiaryLongitude())
&&oldItem.getApiaryLatitude().equals(newItem.getApiaryLatitude())&&oldItem.getDateOfHarvest().equals(newItem.getDateOfHarvest())
&&oldItem.getDateOfColonisation().equals(newItem.getDateOfColonisation())&&oldItem.getNoOfWaxHarvested().equals(newItem.getNoOfWaxHarvested())
&&oldItem.getQuantityOfHoneyHarvested().equals(newItem.getQuantityOfHoneyHarvested())&&oldItem.getNoOfPropolis().equals(newItem.getNoOfPropolis())
&&oldItem.getNoOfCombs().equals(newItem.getNoOfCombs())&&oldItem.getHiveNo().equals(newItem.getHiveNo())
&&oldItem.getHiveLongitude().equals(newItem.getHiveLongitude())&&oldItem.getHiveLatitude().equals(newItem.getHiveLatitude())
&&oldItem.getTime().equals(newItem.getTime())&&oldItem.getDate().equals(newItem.getDate());
}
};
public class ApiaryHolder extends RecyclerView.ViewHolder {
private TextView address;
private TextView apiary;
private TextView town;
private TextView LocationLatitude;
private TextView LocationLongitude;
private TextView HiveLatitude;
private TextView HiveLongitude;
private TextView date;
private TextView time;
private TextView hiveNo;
private TextView noOfComb;
private TextView dateOfHarvest;
private TextView dateOfColonisation;
private TextView waxHarvested;
private TextView quantityOfhoney;
private TextView noOfPropolis;
private CheckBox checkBox;
CardView cardView;
Apiaries apiaries;
public ApiaryHolder(#NonNull final View itemView, final Apiaries apiaries) {
super(itemView);
address = itemView.findViewById(R.id.customAddress);
apiary = itemView.findViewById(R.id.customApiaryNo);
town = itemView.findViewById(R.id.customTown);
LocationLongitude = itemView.findViewById(R.id.customLongitude);
LocationLatitude = itemView.findViewById(R.id.customLatitude);
checkBox = itemView.findViewById(R.id.customCheckbox);
hiveNo = itemView.findViewById(R.id.customHiveNo);
noOfComb = itemView.findViewById(R.id.customCombNo);
dateOfHarvest = itemView.findViewById(R.id.customDateOfHarvest);
dateOfColonisation = itemView.findViewById(R.id.customDateOfColonisation);
waxHarvested = itemView.findViewById(R.id.customNoOfWaxHarveted);
quantityOfhoney = itemView.findViewById(R.id.customQuantityOfHoney);
noOfPropolis = itemView.findViewById(R.id.customNoOfPropolis);
HiveLatitude = itemView.findViewById(R.id.customHiveLatitude);
HiveLongitude = itemView.findViewById(R.id.customHiveLongitude);
date = itemView.findViewById(R.id.customDate);
time = itemView.findViewById(R.id.customTime);
cardView = itemView.findViewById(R.id.Cardview);
this.apiaries = apiaries;
mainActivity = new MainActivity();
cardView.setOnLongClickListener(apiaries);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int Position = getAdapterPosition();
if (listener != null && Position != RecyclerView.NO_POSITION){
listener.OnItemClick(getItem(Position));
Toast.makeText(context, "Hello9", Toast.LENGTH_LONG).show();
notifyDataSetChanged();
}
}
});
}
#NonNull
#Override
public ApiaryHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.customlayout, parent, false);
final ApiaryHolder apiaryHolder= new ApiaryHolder(itemView, apiaries);
return apiaryHolder;
}
#Override
public void onBindViewHolder(#NonNull ApiaryHolder holder, final int position) {
RecordModel currentRecord = getItem(position);
holder.address.setText(currentRecord.getAddress());
holder.apiary.setText(currentRecord.getApiaryNo());
holder.town.setText(currentRecord.getTown());
holder.LocationLatitude.setText(currentRecord.getApiaryLatitude());
holder.LocationLongitude.setText(currentRecord.getApiaryLongitude());
holder.hiveNo.setText(currentRecord.getHiveNo());
holder.noOfComb.setText(currentRecord.getNoOfCombs());
holder.dateOfHarvest.setText(currentRecord.getDateOfHarvest());
holder.dateOfColonisation.setText(currentRecord.getDateOfColonisation());
holder.waxHarvested.setText(currentRecord.getNoOfWaxHarvested());
holder.quantityOfhoney.setText(currentRecord.getQuantityOfHoneyHarvested());
holder.noOfPropolis.setText(currentRecord.getNoOfPropolis());
holder.HiveLatitude.setText(currentRecord.getHiveLatitude());
holder.HiveLongitude.setText(currentRecord.getHiveLongitude());
holder.date.setText(currentRecord.getDate());
holder.time.setText(currentRecord.getTime());
}
public interface RecordOnClickListener{
void OnItemClick(RecordModel recordModel);
}
public void setOnItemClickListener(RecordOnClickListener listener){
this.listener = listener;
}
my fragment
public class Apiaries extends Fragment implements View.OnLongClickListener {
public static boolean is_in_contextualMode = false;
public static RecordViewModel record_View_model;
Apiary_Adapter apiary_adapter;
public static int counter = 0;
public static ArrayList<RecordModel> selectionList =new ArrayList<>();
private RecyclerView recyclerView;
public static SearchView searchView;
public static final int ADD_RECORD_REQUEST = 1;
public static final int EDIT_RECORD_REQUEST = 2;
com.github.clans.fab.FloatingActionButton floatingActionButton;
FirebaseAuth firebaseAuth;
DatabaseReference reference;
public static MainActivity mainActivity;
private Menu menu;
ArrayList<RecordModel> list = new ArrayList<>();
public Apiaries() {
// Required empty public constructor
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_apiaries, container, false);
floatingActionButton = (FloatingActionButton) view.findViewById(R.id.fab2);
Apiaries apiaries = new Apiaries();
apiary_adapter = new Apiary_Adapter(apiaries, getActivity());
firebaseAuth = FirebaseAuth.getInstance();
String currentUserId = firebaseAuth.getCurrentUser().getUid();
reference = FirebaseDatabase.getInstance().getReference().child("users").child(currentUserId);
recyclerView=(RecyclerView)view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(apiary_adapter);
recyclerView.setHasFixedSize(true);
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mainActivity = new MainActivity();
record_View_model = new ViewModelProvider(getActivity()).get(RecordViewModel.class);
record_View_model.getAllRecord().observe(getActivity(), new Observer<List<RecordModel>>() {
#Override
public void onChanged(#Nullable List<RecordModel> recordModels) {
apiary_adapter.submitList(recordModels);
}
});
apiary_adapter.setOnItemClickListener(new Apiary_Adapter.RecordOnClickListener() {
#Override
public void OnItemClick(RecordModel recordModel) {
Intent intent = new Intent(getContext(), EditApiaryAddActivity.class);
intent .putExtra(EditApiaryAddActivity.EXTRA_ID, recordModel.getId());
intent .putExtra(EditApiaryAddActivity.EXTRA_ADDRESS, recordModel.getAddress());
intent .putExtra(EditApiaryAddActivity.EXTRA_APIARYNAME, recordModel.getApiaryNo());
intent .putExtra(EditApiaryAddActivity.EXTRA_TOWN, recordModel.getTown());
intent .putExtra(EditApiaryAddActivity.EXTRA_LOCATION_LATITUDE, recordModel.getApiaryLatitude());
intent .putExtra(EditApiaryAddActivity.EXTRA_LOCATION_LONGITUDE, recordModel.getApiaryLongitude());
intent .putExtra(EditApiaryAddActivity.EXTRA_HIVE_LATITUDE, recordModel.getHiveLatitude());
intent .putExtra(EditApiaryAddActivity.EXTRA_HIVE_LONGITUDE, recordModel.getHiveLongitude());
intent.putExtra(EditApiaryAddActivity.EXTRA_HIVENO, recordModel.getHiveNo());
intent.putExtra(EditApiaryAddActivity.EXTRA_NOOFCOMBS, recordModel.getNoOfCombs());
intent.putExtra(EditApiaryAddActivity.EXTRA_DATEOFHARVEST, recordModel.getNoOfWaxHarvested());
intent.putExtra(EditApiaryAddActivity.EXTRA_DATEOFCOLONISATION, recordModel.getDateOfColonisation());
intent.putExtra(EditApiaryAddActivity.EXTRA_WAXHARVESTED, recordModel.getNoOfWaxHarvested());
intent.putExtra(EditApiaryAddActivity.EXTRA_QUANTITYOFHONEY, recordModel.getQuantityOfHoneyHarvested());
intent.putExtra(EditApiaryAddActivity.EXTRA_NOOFPROPOLIS, recordModel.getNoOfPropolis());
startActivityForResult(intent,EDIT_RECORD_REQUEST);
Apiaries.is_in_contextualMode = false;
mainActivity.clearActionMode();
}
});
}
#Override
public boolean onLongClick(View v) {
MainActivity.toolbar.getMenu().clear();
MainActivity.toolbar.inflateMenu(R.menu.menu_record);
MainActivity.textView1.setVisibility(View.GONE);
MainActivity.itemCountTextView.setVisibility(View.VISIBLE);
MainActivity.edit.setVisibility(View.VISIBLE);
is_in_contextualMode = true;
return true;
}
You should handle Clicks in the onCreateViewHolder method
public ApiaryHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.customlayout, parent, false);
final ApiaryHolder apiaryHolder= new ApiaryHolder(itemView, apiaries);
ApiaryHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//handle the clicks here
}
}
});
return apiaryHolder;
}
You have implemented onLongClickListener Instead of onClickListener
Add the onClickListener Inside on bindviewholder Like holder.itemView.setOnClickLstener

RecyclerView In Fragment Not Working (Unable to add data to it)

Here is my fragment
public class TransactionsFragment extends Fragment {
private FirebaseAuth mAuth;
private DatabaseReference mDBref;
FirebaseUser Fuser;
private String UID;
private RecyclerView recyclerView;
private RecyclerView.Adapter trpAdapter;
private List<TransGetterSetter> DataList;
String trans_id;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_transactions, container, false);
DataList = new ArrayList<>();
recyclerView = (RecyclerView) v.findViewById(R.id.RVList);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
TransactionRVAdapter trvAdapter = new TransactionRVAdapter(DataList, getContext());
recyclerView.setAdapter(trvAdapter);
mAuth = FirebaseAuth.getInstance();
mDBref = FirebaseDatabase.getInstance().getReference();
Fuser = mAuth.getInstance().getCurrentUser();
UID = Fuser.getUid();
mDBref.child("transactions").child(UID).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot chidSnap : dataSnapshot.getChildren()) {
trans_id = chidSnap.getKey();
DataList.add(new TransGetterSetter("" + trans_id));
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
trvAdapter.notifyDataSetChanged();
return v;
}
}
If i toast "trans_id" string im getting expected result.
Here is My Adapter class
public class TransactionRVAdapter extends RecyclerView.Adapter<TransactionRVAdapter.rvViewHolder> {
Context mContext;
List<TransGetterSetter> DataList = new ArrayList<>();
public class rvViewHolder extends RecyclerView.ViewHolder{
private TextView TransId;
public rvViewHolder(#NonNull View itemView) {
super(itemView);
TransId = (TextView) itemView.findViewById(R.id.TransIdTxt);
}
}
public TransactionRVAdapter( List<TransGetterSetter> DataList,Context mContext){
this.DataList = DataList;
this.mContext = mContext;
}
#NonNull
#Override
public rvViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.fragment_transactions_item, viewGroup, false);
return new rvViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull rvViewHolder rvViewHolder, int i) {
rvViewHolder.TransId.setText(DataList.get(i).getTrans_id());
}
#Override
public int getItemCount() {
return DataList.size();
}
}
Here is my GetterSetter class
public class TransGetterSetter {
private String trans_id;
public TransGetterSetter() {
}
public TransGetterSetter(String trans_id) {
this.trans_id = trans_id;
}
public String getTrans_id() {
return trans_id;
}
public void setTrans_id(String trans_id) {
this.trans_id = trans_id;
}
}
Im able to get Data from firebase, but when i try to add it to recycler view it dosen't show any data. Recycler view is showing empty. I checked my code many time but haven't found solution.
You should call :
trvAdapter.notifyDataSetChanged();
After the for loop inside the (onDataChange) event listener.
Try to called ..
DataList.add(new TransGetterSetter("" + trans_id));
after this line called ..
trvAdapter.notifyDataSetChanged();

RecyclerView is showing nothing

I have some items in my adapter but nothing is shown in the RecyclerView.
Adapter
public class WorkOrderAdapter extends RecyclerView.Adapter<WorkOrderViewHolder> {
private List<WorkOrder> orders = new LinkedList<>();
public void setData(List<WorkOrder> orders) {
this.orders = orders;
}
#Override
public WorkOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_workorder, parent, false);
return new WorkOrderViewHolder(view);
}
#Override
public void onBindViewHolder(WorkOrderViewHolder holder, int position) {
WorkOrder order = orders.get(position);
holder.bind(order);
}
#Override
public int getItemCount() {
return orders.size();
}
}
ViewHolder
public class WorkOrderViewHolder extends RecyclerView.ViewHolder {
private TextView title;
private TextView description;
private TextView date;
public WorkOrderViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title_textview);
description = (TextView) view.findViewById(R.id.description_textview);
date = (TextView) view.findViewById(R.id.date_textview);
}
public void bind(WorkOrder order) {
title.setText("Test");
description.setText("Test");
date.setText("Test");
}
}
Activity (Using AndroidAnnotations)
#EActivity(R.layout.activity_workorders)
#OptionsMenu(R.menu.activity_workorders)
public class WorkOrdersActivity extends ToolbarActivity {
#ViewById(R.id.orders_recyclerview)
RecyclerView ordersList;
List<WorkOrder> orders = new LinkedList<>();
private WorkOrderAdapter adapter;
{
adapter = new WorkOrderAdapter();
orders.add(new WorkOrder());
orders.add(new WorkOrder());
orders.add(new WorkOrder());
adapter.setData(orders);
}
#AfterViews
public void initViews() {
ordersList.setAdapter(adapter);
}
}
Please add the LayoutManager to the RecyclerView and try again
ordersList.setLayoutManager(new LinearLayoutManager(this));
ordersList.setAdapter(adapter);

Categories

Resources