I would like to create a custom ListView populated by a custom ArrayAdapter. The code works fine, but the ListView is not showing data immediately. In order to have the data shown I need to click on a random EditText of the same page, close the Keyboard and magically the ListView shows data.
I don't know if it this can have an impact but the adapter is used in a fragment,which retrieve the data from Firebase during the OnCreate method.
Below my code, I removed everything not necessary for this topic and simplified the array.
This is my Class of elements inside the array:
public class ListItem {
private String Description;
private String Price;
public ListItem (String Description, String Price) {
this.Description = Description;
this.Price = Price;
}
public String getDescription() { return Description; }
public String getPrice() {
return Price;
}
public void setDescription(String description) {
Description = description;
}
public void setPrice(String price) {
Price = price;
}
}
The adapter is the following:
public class CustomListAdapter extends ArrayAdapter<ListItem> {
private Context mContext;
private Integer mResource;
private static class ViewHolder {
TextView txt_description_item;
TextView txt_price_item;
}
public CustomListAdapter(#NonNull Context context, int resource, #NonNull ArrayList<ListItem> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
String Description = getItem(position).getDescription();
String Price = getItem(position).getPrice();
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.txt_description_item = convertView.findViewById(R.id.txt_description_item);
viewHolder.txt_price_item = convertView.findViewById(R.id.txt_price_item);
viewHolder.txt_description_item.setText(Description);
viewHolder.txt_price_item.setText(Price);
return convertView;
}
}
Finally this is the Fragment code:
public class MyFragment extends Fragment {
private ListView view_list_items;
private FirebaseDatabase database;
private DatabaseReference refDatabase;
private static final String USER = "user";
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_myprofile, container, false);
view_list_items = v.findViewById(R.id.list_items);
database = FirebaseDatabase.getInstance();
refDatabase = database.getReference(USER);
refDatabase.child("items").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
final ArrayList<ListItem> arr_items = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.child("itemlist1").getChildren()) {
final String description = ds.getKey();
final String price = ds.getValue(String.class);
ListItem listItem = new ListItem(description,price);
arr_items.add(listItem);
}
CustomListAdapter adapter = new CustomListAdapter(getContext(),R.layout.layout_myItemList, arr_items);
view_list_items.setAdapter(adapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
EDIT:
I solved the issue by setting the adapter inside the 'for' cycle. Maybe it's not the most elegant solution but it works fine
for (DataSnapshot ds : dataSnapshot.child("itemlist1").getChildren()) {
final String description = ds.getKey();
final String price = ds.getValue(String.class);
ListItem listItem = new ListItem(description,price);
arr_items.add(listItem);
CustomListAdapter adapter = new
CustomListAdapter(getContext(),R.layout.layout_myItemList, arr_items);
view_list_items.setAdapter(adapter);
}
The firebase database is asynchronous i.e, the compiler doesn't wait for the code inside onDataChange() or onCancelled() to return a value. It would return all the values apart from those inside those 2 methods immediately, while for those 2 methods it would return the value when the value is available from the database. Below code might help you in better understanding
refDatabase.child("items").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
System.out.println("Inside onDataChange method");
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
System.out.println ("Outside onDataChange method");
The output will be like:
Outside onDataChange method
Inside onDataChange method
Notice that comes Outside onDataChange method first. This is the reason why your listView doesn't show data immediately.
What you can do to keep the user engaged is, add a spinner in your view which will be visible till the data is fetched from the database. To do this, try this code.
database = FirebaseDatabase.getInstance();
refDatabase = database.getReference(USER);
spinner.setVisibility(View.VISIBLE);
refDatabase.child("items").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
spinner.setVisibility(View.INVISIBLE);
//Your code goes here
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
//Error code goes here
}
});
Related
I have passed data from my Activity to My Adapter. When I debug, I can see the correct data has successfully passed to my adapter, but when I attempt to use it as a string ( for example, if I want to set the text as the data I just passed), it shows as null.
On the line that says " this.uniquesharedIds = uniquesharedId;" - the "uniqiuesharedIds" is showing as null.
"uniquesharedId" shows has the successfully passed data.
I need to be able to use the string of "uniqiuesharedIds."
Sorry if this is a silly question. Sending data from Activities to Adapters always confuses me and Im not able to find a ton of documentation/videos on the topic. Thank you.
My Activity In the On Create Method
myadapter = new Invite_Contributors_Adapter(contributorInviteList, getIntent().getStringExtra("uniquesharedId"));
The Adapter
public class Invite_Contributors_Adapter extends RecyclerView.Adapter<Invite_Contributors_Adapter.myviewholder> {
private ArrayList<Model_Invite_Contributors_List> model_invite_contributors_lists = new ArrayList<>();
FirebaseAuth mAuth;
private FirebaseUser currentuser;
private DatabaseReference UsersReference;
Context context;
String uniquesharedIds;
private InviteContributorsInterface inviteContributorsInterface;
public Invite_Contributors_Adapter() {
}
public void updateInviteList (ArrayList list) {
model_invite_contributors_lists .clear();
model_invite_contributors_lists .addAll(list);
notifyDataSetChanged();
}
public Invite_Contributors_Adapter(ArrayList<Model_Invite_Contributors_List>model_invite_contributors_lists, String uniquesharedId) {
this.model_invite_contributors_lists = model_invite_contributors_lists;
this.uniquesharedIds = uniquesharedId;
}
#NonNull
#Override
public Invite_Contributors_Adapter.myviewholder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout_invite_contributors_list, parent, false);
return new myviewholder(view);
}
#Override
public void onBindViewHolder(#NonNull myviewholder holder, int position) {
holder.setData(model_invite_contributors_lists.get(position));
mAuth = FirebaseAuth.getInstance();
holder.Name.setText(uniquesharedIds);
}
#Override
public int getItemCount() {
return model_invite_contributors_lists.size();
}
static class myviewholder extends RecyclerView.ViewHolder implements DialogInterface.OnClickListener {
TextView Name;
CircleImageView profileImageView;
public myviewholder(#NonNull View itemView) {
super(itemView);
Name = itemView.findViewById(R.id.contributor_name);
profileImageView = itemView.findViewById(R.id.member_profile_picture);
}
#Override
public void onClick(DialogInterface dialog, int which) {
}
public void setData(Model_Invite_Contributors_List model) {
FirebaseUser currentuser;
currentuser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference NameRef = FirebaseDatabase.getInstance().getReference(Strings.UsersReference);
NameRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
//this is very important. this says to not show the current user in the list of people to invite as a contributor.//
if (currentuser.getUid().equals(model.getUser_Id())){
ViewGroup.LayoutParams params = itemView.getLayoutParams();
params.height = 0;
itemView.setLayoutParams(params);
} else {
for(DataSnapshot ds : dataSnapshot.getChildren())
{
itemView.setVisibility(View.VISIBLE);
String profileImageString;
profileImageString = model.getProfileimage();
Glide.with(profileImageView.getContext()) //pulling in image and telling the image which imageview to go to once it comes in from the database
.load(profileImageString)
.placeholder(R.drawable.circle_placeholder)
.error(R.drawable.circle_placeholder)
.into(profileImageView);
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
public void setInterface (Invite_Contributors_Adapter.InviteContributorsInterface inviteContributorsInterface) {
this.inviteContributorsInterface = inviteContributorsInterface;
}
public interface InviteContributorsInterface{
}
}
Are you using any primary constructor of the adapter like this?
myadapter = new Invite_Contributors_Adapter();
If yes, then that's where the problem is. If you're initializing object with two different constructors then you'll get the value of the object which you initialized later.
Make sure to check the adapter object & then proceed.
I have been getting certain issues with the onDataChange method when I am calling from Firebase Realtime Database. These issues would include data disappearing outside of the DataChange method. I tried a solution from this link,
How to return DataSnapshot value as a result of a method?, it has worked, however, when I tried passing it to a fragment bundle within the callback method, it says the values are null and there isn't any data in it. Is there any workaround for this problem that I am facing? Help is really appreciated, thanks!
import edu.ntu.ssp4_rzdns_outhink.R;
import edu.ntu.ssp4_rzdns_outhink.modals.Attraction;
public class MostPopularRecyclerViewAdapter extends RecyclerView.Adapter<MostPopularRecyclerViewAdapter.ViewHolder>{
private static final String TAG = "MostPopularRecyclerViewAdapter";
private static final String SHARED_PREFS = "attractionsFile";
private ArrayList<Attraction> pops;
private Attraction attraction;
private Context mContext;
private FragmentManager fm;
private Bundle bundle;
SharedPreferences.Editor editor;
SharedPreferences attractionFile;
public MostPopularRecyclerViewAdapter(ArrayList<Attraction> pops, Context mContext, FragmentManager fm, Bundle bundle) {
this.pops = pops;
this.mContext = mContext;
this.fm = fm;
this.bundle = bundle;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d(TAG, "OnCreateViewHolder Called");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_more_list, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder Called");
Glide.with(mContext).asBitmap().load(pops.get(position).photo_url).into(holder.attractionImage);
holder.locationName.setText(pops.get(position).att_name);
holder.locationRating.setText(pops.get(position).att_rating.toString());
holder.locationAddress.setText(pops.get(position).att_address);
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String attraction_id = pops.get(holder.getBindingAdapterPosition()).id;
readData(new FirebaseCallback() {
#Override
public void onCallBack(Attraction attr) {
bundle.putString("attid", attr.id);
bundle.putString("name", attr.att_name);
bundle.putString("address", attr.att_address);
bundle.putString("desc", attr.att_desc);
bundle.putDouble("rating", attr.att_rating);
bundle.putString("url", attr.att_url);
bundle.putSerializable("ophrs", attr.att_op_hr);
bundle.putSerializable("adminrate",attr.att_admin_rate);
bundle.putString("photo_url", attr.photo_url);
}
},attraction_id);
}
});
}
#Override
public int getItemCount() {
return pops.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
ImageView attractionImage;
TextView locationName;
TextView locationAddress;
TextView locationRating;
RelativeLayout parentLayout;
public ViewHolder(#NonNull View itemView){
super(itemView);
attractionImage = itemView.findViewById(R.id.viewmoreImage);
locationName = itemView.findViewById(R.id.viewmoreName);
locationAddress = itemView.findViewById(R.id.viewmoreLocation);
locationRating = itemView.findViewById(R.id.viewmoreRating);
parentLayout = itemView.findViewById(R.id.parent_layout_view_more);
}
}
private void readData(FirebaseCallback firebaseCallback, String attrId){
Query query = FirebaseDatabase.getInstance().getReference("attractions");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
if (!snapshot.exists()) {
System.out.println("Attraction Does Not Exist");
} else {
for (DataSnapshot attr : snapshot.getChildren()) {
if (attr.getKey().equals(attrId)) {
attraction = attr.getValue(Attraction.class);
attraction.setId(attr.getKey());
}
}
firebaseCallback.onCallBack(attraction);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
private interface FirebaseCallback{
void onCallBack(Attraction attraction);
}
}
According to your last comment:
When I try to put these variables in the bundle, it’ll just disappear and become null.
Please note that this is the expected behavior. There are multiple inconsistencies in your code. First of all, that's not how you should handle the click event in your adapter class. Attaching a click listener to each element you display, isn't a feasible solution. You should create an interface for that as you can see in this repo.
Now, each time you click a particular element in your RecyclerView, you call the readData() method. When you receive the data, you add that data to the Bundle. The problem is that the operation that is opening the new fragment is happening faster than you get the data from the database. That's the reason why you are getting null.
To solve this, you either navigate to the next fragment, only when the data is available or you can make another call, right from the second fragment.
everyone, I was trying to make a music app, and for this, I Created a Horizontal RecyclerView in my HomeFragment and my horizontal RecyclerView is getting an image with artist name.
But after clicking I load another Activity. In my other activity, I was trying to load SongsData from firebase in a listView with RecyclerView.
But the problem is I am not getting data from Firebase and it is returning null data. I provided my code below and here is the screenshot of my Firebase database:- ScreenShot
My List Class:-
public class TestUploads
{
private String songName;
private String songImageUri;
private String songUrl;
private String artistName;
public TestUploads() {
}
public String getSongName() {
return songName;
}
public void setSongName(String SongName) {
this.songName = SongName;
}
public String getSongImageUri() {
return songImageUri;
}
public void setSongImageUri(String SongImageUri) {
this.songImageUri = SongImageUri;
}
public String getSongUrl() {
return songUrl;
}
public void setSongUrl(String SongUrl) {
this.songUrl = songUrl;
}
public TestUploads(String SongImageUri, String SongName, String SongUrl ) {
this.songName = SongName;
this.artistName = SongImageUri;
this.songUrl = SongUrl;
}
}
My Adapter Class:-
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.TestViewHolder>{
private Context mContext;
private List<TestUploads> mUploads;
public TestAdapter(Context context , List<TestUploads> uploads) {
mContext = context;
mUploads = uploads;
}
#NonNull
#Override
public TestViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.test_package_layout , parent ,false);
return new TestViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull TestViewHolder holder, int position) {
TestUploads uploadcurrent = mUploads.get(position);
holder.name.setText(uploadcurrent.getSongName());
Glide.with(mContext)
.load(uploadcurrent.getSongImageUri())
.into(holder.image_view);
}
#Override
public int getItemCount() {
return mUploads
.size();
}
public class TestViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView artist_name;
public CircleImageView image_view;
public TestViewHolder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.test_package_song_name);
artist_name = itemView.findViewById(R.id.test_package_artist_name);
image_view = itemView.findViewById(R.id.test_package_image_name);
}
}
}
My Activity:-
public class TestActivity extends AppCompatActivity {
private ValueEventListener listener;
private DatabaseReference reference;
private List<TestUploads> mUploads;
private RecyclerView mRecyclerView;
private TestAdapter adapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_package_activity);
reference = FirebaseDatabase.getInstance().getReference("ArtistView").child(getIntent().getStringExtra("Artist"))
.child("Songs");
Toast.makeText(this, "" + getIntent().getStringExtra("Artist"), Toast.LENGTH_SHORT).show();
mUploads = new ArrayList<>();
mRecyclerView = findViewById(R.id.test_pacakge_recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.smoothScrollToPosition(0);
adapter = new TestAdapter(this , mUploads);
mRecyclerView.setAdapter(adapter);
listener = reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
mUploads.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
TestUploads uploads =postSnapshot.getValue(TestUploads.class);
mUploads.add(uploads);
}
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
Sorry for so much code but this is not hard to solve. If you find the solution please reply to me. Thanks for reading this.
The problem in your code lies in the fact that the names of the fields in your TestUploads class are different than the name of the properties in your database. You have in your TestUploads class a field named songName but in your database, I see it as SongName and this is not correct. The names must match. When you are using a getter named getSongName(), Firebase is looking in the database for a field named songName and not SongName. See the lowercase s letter vs. capital letter S?
There are two ways in which you can solve this problem. The first one would be to remove the data in your database and add it again using field names that start with lowercase, as exist in your TestUploads class.
If you are not allowed to use the first solution, then the second approach will be to use annotations. So you should use the PropertyName annotation in front of the getters. So in your TestUploads class, a getter should look like this:
#PropertyName("SongName")
public String getSongName() {
return songName;
}
Iam getting data from firebase and it is populated in userMessageList.adapter is notified and getItemCount is showing correct count but onBindViewHolder only run once for 1st postion of userMessage list. onBindViewHolder is not working for further positions or data
i have got no solution related to my problem
THIS IS MY ADAPTER CLASS
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {
private List <Messages> userMessagesList;
private DatabaseReference firebaseDatabase,users;
private FirebaseAuth auth;
public MessageAdapter(List<Messages> userMessagesList){
this.userMessagesList = userMessagesList;
}
#NonNull
#Override
public MessageViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.custom_messages_layout,viewGroup,false);
auth = FirebaseAuth.getInstance();
return new MessageViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final MessageViewHolder messageViewHolder, int i) {
String messageSenderID = auth.getCurrentUser().getUid();
Messages messages = userMessagesList.get(i);
String fromUserID = messages.getFrom();
String fromMessageType = messages.getType();
users = FirebaseDatabase.getInstance().getReference().child("Users").child(fromUserID);
users.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild("image")){
final String receiverImage = dataSnapshot.child("image").getValue().toString();
Picasso.get()
.load(receiverImage)
.placeholder(R.drawable.profile_image)
.into(messageViewHolder.receiverProfileImage);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
if(fromMessageType.equals("text")){
messageViewHolder.receiverMessageText.setVisibility(View.INVISIBLE);
messageViewHolder.receiverProfileImage.setVisibility(View.INVISIBLE);
if(fromUserID.equals(messageSenderID)){
messageViewHolder.senderMessageText.setBackgroundResource(R.drawable.sender_messages_layout);
messageViewHolder.senderMessageText.setTextColor(Color.BLACK);
messageViewHolder.senderMessageText.setText(messages.getMessage());
}
else{
messageViewHolder.receiverMessageText.setVisibility(View.INVISIBLE);
messageViewHolder.receiverProfileImage.setVisibility(View.VISIBLE);
messageViewHolder.receiverMessageText.setVisibility(View.VISIBLE);
messageViewHolder.receiverMessageText.setBackgroundResource(R.drawable.receiver_messeges_layout);
messageViewHolder.receiverMessageText.setTextColor(Color.BLACK);
messageViewHolder.receiverMessageText.setText(messages.getMessage());
}
}
}
#Override
public int getItemCount() {
return userMessagesList.size();
}
public class MessageViewHolder extends RecyclerView.ViewHolder{
public TextView senderMessageText, receiverMessageText;
public CircleImageView receiverProfileImage;
public MessageViewHolder(#NonNull View itemView) {
super(itemView);
senderMessageText =itemView.findViewById(R.id.sender_message_text);
receiverMessageText = itemView.findViewById(R.id.receiver_message_text);
receiverProfileImage =itemView.findViewById(R.id.message_profile_image);
}
}
}
THIS IS ACTIVITY CODE WHERE IAM POPULATING THE userMessageList
rootRef.child("Messages")
.child(messageSenderID).
child(messageReceivedID)
.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot
dataSnapshot, #Nullable String s) {
Messages messages = dataSnapshot.getValue(Messages.class);
messagesList.add(messages);
messageAdapter.notifyDataSetChanged();
}
}
As Pemba Tamang in comment above rightly said you should not have any network or database related code in your adapter.
Algorithm to solve your problem:
1. Fetch list of Messages outside of adapter.
2. Subscribe for child change outside of adapter.
3. When child changed go to view model, find this Message by it's id and update this info.
4. Update your adapter
Also will be good if you will update your adapter with DiffUtils. It will bind only changed child, not all. See: https://developer.android.com/reference/android/support/v7/util/DiffUtil
I am new to Android Studio. I am trying to develop a inventory management app but unable to show the information in a custom ListView from Firebase database. I have found similar questions and solution here but did not work for me. Below is the code of AccountHome(MainActivity). This code works sometimes when I close and restart the app multiple times. Hope to get some working solution. Thanks Screenshot of Firebase Database
public class AccountHom extends AppCompatActivity {
ListView listview;
FirebaseDatabase database;
DatabaseReference myRef;
ArrayList<String> dept ;
ArrayList<Long> total;
AccountSetBudgetHelper accountSetBudgetHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account_home_activity);
accountSetBudgetHelper = new AccountSetBudgetHelper();
listview = findViewById(R.id.set_budget_listview);
database = FirebaseDatabase.getInstance();
myRef = database.getReference("budget");
dept = new ArrayList<>();
total = new ArrayList<Long>();
AccountSetBudgetAdaptr accountSetBudgetAdapter = new AccountSetBudgetAdaptr(AccountHom.this ,dept,total);
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
accountSetBudgetHelper = ds.getValue(AccountSetBudgetHelper.class);
dept.add(accountSetBudgetHelper.getName());
total.add(accountSetBudgetHelper.getTotal());
} }
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
listview.setAdapter(accountSetBudgetAdapter);
}
}
class AccountSetBudgetAdaptr extends ArrayAdapter {
private final Activity context;
private final ArrayList<String> dept;
private final ArrayList<Long> total;
AccountSetBudgetAdaptr(Activity context, ArrayList<String> dept, ArrayList<Long> total) {
super(context, R.layout.single_row_listview_budget,dept);
this.context = context;
this.dept = dept;
this.total = total;
}
#NonNull
#Override
public View getView(int position, View view, #NonNull ViewGroup parent) {
Toast.makeText(getContext(), "this is a boy", Toast.LENGTH_SHORT).show();
View rowView = view;
if(rowView==null){
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.single_row_listview_budget, parent, false);
}
TextView mdept = rowView.findViewById(R.id.textView_setbudget_dept);
TextView mtotal = rowView.findViewById(R.id.textView_setbudget_total);
mdept.setText(dept.get(position));
mtotal.setText(String.valueOf(total.get(position)));
return rowView;
}
}
class AccountSetBudgetHelpr {
private String name;
private Long total;
public AccountSetBudgetHelpr() {
}
public AccountSetBudgetHelpr(String name, Long total) {
this.name = name;
this.total = total;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
}[enter image description here][1]
You are adding data to both lists, dept and total but you don't notify the adapter about the new data. So to solve this, please add the following line of code, right after the for loop ends:
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
accountSetBudgetHelper = ds.getValue(AccountSetBudgetHelper.class);
dept.add(accountSetBudgetHelper.getName());
total.add(accountSetBudgetHelper.getTotal());
}
accountSetBudgetAdapter.notifyDataSetChanged(); //Notify the adapter
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {}
});