Firebase android studio get id on ListView items - java

first time using Firebase and pretty new to android studio, i'm trying to make a schedule app where a user can create many schedules and it would be associated to their account.
At the moment a user can create an account, create a schedule (i just have 2 fields for this, will add the rest once i get the issue sorted) and also have multiple schedules.
I would like to be able to update/delete a schedule of a user but i'm struggling to get the ID of the specific schedule node in which I need to delete.
This is what i have in Firebase with a single user and 2 schedules
I added a toast when i long click a list item which displays the corresponding scheduleId of that schedule. keep in mind this is to help so i can just long click and show if the item displays the proper scheduleId.
Part of ScheduleActivity.java
What the problem is
I have a listView with all the schedules that has a listener
In the listener I have this line which gets the ID, but the issue is since its on the listener, i wont get the ID of the schedule until i click the list item and view the details, then Im only able to view the scheduleId, otherwise i get a NULL value.
scheduleId = scheduleList.get(position).getScheduleId();
public class ScheduleActivity extends AppCompatActivity implements View.OnClickListener {
private List<Schedule> scheduleList;
private List<String> scheduleIdList;
private DatabaseReference scheduleReference;
private String userId;
public static final String SCHEDULE_TITLE = "title";
public static final String SCHEDULE_DESCRIPTION = "description";
private String scheduleId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
getSupportActionBar().hide();
scheduleList = new ArrayList<>();
scheduleIdList = new ArrayList<>();
userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
scheduleReference = FirebaseDatabase.getInstance().getReference(DBStrings.DB_SCHEDULES);
registerForContextMenu(scheduleListView);
// when a user clicks any of list items
scheduleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Schedule schedule = scheduleList.get(position);
// this gets the id but the issue is i need to first click the list item then ill get the correct id, otherwise i get a NPE because i haven't accessed the list item yet
// need to figure out how to implement this in the onStart() method so i can get the scheduleId beforehand
scheduleId = scheduleList.get(position).getScheduleId();
// intent that takes me to the activity to view the schdule details
Intent viewSchedule = new Intent(ScheduleActivity.this, ViewSchedule.class);
viewSchedule.putExtra(SCHEDULE_TITLE, schedule.getTitle());
viewSchedule.putExtra(SCHEDULE_DESCRIPTION, schedule.getDescription());
startActivity(viewSchedule);
}
}
});
}
// when a user long clicks a list item, brings up menu with option to edit/delete
// Also display a toast with scheduleID so i can see if the proper id is being retrieved
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
Toast.makeText(ScheduleActivity.this, "id: " + scheduleId, Toast.LENGTH_SHORT).show();
getMenuInflater().inflate(R.menu.schedule_menu, menu);
}
// switch statement for delete/ redirect to edit activity that i left out
// load schedule data into list view
#Override
protected void onStart() {
super.onStart();
scheduleReference.child(userId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
scheduleList.clear();
scheduleIdList.clear();
for(DataSnapshot dataSnapshot : snapshot.getChildren()) {
Schedule schedule = dataSnapshot.getValue(Schedule.class);
// add schedules to list to show in list view
scheduleList.add(schedule);
// Add all the ids of schedules in a list, i used this in my scheduleListView.setOnItemListener to grab the scheduleId.
scheduleIdList.add(schedule.getScheduleId());
}
System.out.println("id list: " + scheduleIdList);
ScheduleListAdapter adapter = new ScheduleListAdapter(ScheduleActivity.this, scheduleList);
scheduleListView.setAdapter(adapter);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
System.out.println("ERROR: " + error.toException());
}
});
}
private void deleteSchedule(String scheduleId) {
// At the moment i can only delete a item if i first view it by clicking, then i need to go back and it allows me to delete it, this is obviously because my listview listener issue (It does not let me delete without first clicking the item to view/access it) scheduleReference.child(userId).child(scheduleId).removeValue();
Toast.makeText(ScheduleActivity.this, "Schedule " + scheduleId + " was deleted!", Toast.LENGTH_LONG).show();
}
}
List view of schedules
The issue is in the scheduleListView.setOnItemClickListener , i need to find a way to grab the id maybe in onStart method or somewhere eother than the listener, but since I do not have access to the position like i did here, i am struggling to implement this.
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Schedule schedule = scheduleList.get(position);
// Talking about this <position>
scheduleId = scheduleList.get(position).getScheduleId();
}
Images to explain it better
I hope it makes sense, i would need to access each list item then go back to be able to delete a specific one.

Related

Why firebase recyclerview is not working correctly?

I try to open each video when I click on them but what I get instead is only the second video (sometimes first video). For example, when I click on "16 best video ideas for small business" I want it to open that particular video. But what I get instead is "this tiny camera can show the world from a bug's point of view. I think the problem occurs because of for loop inside query in UserHomeVideoAdapter.
UserHomeVideoAdapter.java:
public class UserHomeVideoAdapter extends FirestoreRecyclerAdapter<FollowList, UserHomeVideoAdapter.UserVideoHolder> {
Context context;
final FirebaseFirestore db = FirebaseFirestore.getInstance();
String thumbUrl, videoTitle, videoUrl, videoDesc, videoId, publisherId;
Video video;
public UserHomeVideoAdapter(#NonNull #NotNull FirestoreRecyclerOptions<FollowList> options, Context context) {
super(options);
this.context = context;
}
#Override
protected void onBindViewHolder(#NonNull #NotNull UserVideoHolder holder, int position, #NonNull #NotNull FollowList model) {
Query query = db.collection("Videos").whereEqualTo("publisherId", model.getUserId());
query.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
if (task.getResult() != null) {
for (QueryDocumentSnapshot documentSnapshot : task.getResult()) {
video = documentSnapshot.toObject(Video.class);
Log.d("Data", documentSnapshot.getId() + " => " + documentSnapshot.getData());
thumbUrl = video.getThumbUrl();
videoTitle = video.getVideoTitle();
videoUrl = video.getVideoUrl();
videoDesc = video.getVideoDesc();
videoId = video.getVideoId();
publisherId = video.getPublisherId();
}
if (task.getResult().size() != 0) {
Glide.with(context).load(model.getUserImageUrl()).into(holder.userProfileImage);
Glide.with(context).load(thumbUrl).into(holder.videoImageView);
holder.videoTitle.setText(videoTitle);
holder.mainContainerVideo.setVisibility(View.VISIBLE);
} else if (task.getResult().size() == 0) {
holder.mainContainerVideo.getLayoutParams().height = 0;
holder.mainContainerVideo.getLayoutParams().width = 0;
}
}
} else {
Toast.makeText(context, String.valueOf(task.getException()), Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(e -> Toast.makeText(context, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show());
holder.videoContainer.setOnClickListener(v -> {
Intent intent = new Intent(context, VideoActivity.class);
intent.putExtra("videoPublisherUserName", model.getUserName());
intent.putExtra("thumbUrl", thumbUrl);
intent.putExtra("videoPublisherEmail", model.getUserEmail());
intent.putExtra("videoUrl", videoUrl);
intent.putExtra("videoId", videoId);
intent.putExtra("videoPublisherFullName", model.getUserFullName());
intent.putExtra("videoPublisherId", publisherId);
context.startActivity(intent);
});
}
#NonNull
#NotNull
#Override
public UserVideoHolder onCreateViewHolder(#NonNull #NotNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.video_cell, parent, false);
return new UserVideoHolder(v);
}
public static class UserVideoHolder extends RecyclerView.ViewHolder {
RelativeLayout videoContainer, mainContainerVideo;
CircleImageView userProfileImage;
TextView videoTitle;
ImageView videoImageView;
public UserVideoHolder(#NonNull #NotNull View itemView) {
super(itemView);
mainContainerVideo = itemView.findViewById(R.id.mainContainerVideo);
videoContainer = itemView.findViewById(R.id.videoContainer);
userProfileImage = itemView.findViewById(R.id.userProfileImage);
videoTitle = itemView.findViewById(R.id.videoTitle);
videoImageView = itemView.findViewById(R.id.videoImageView);
}
}
}
I logged videoId inside that is assigned inside for loop. Sometimes it returns ids in this order "1"; "2" and sometimes it returns like this "2"; "1". When it returns in this order "1"; "2" click opens second video even if I click first video and when it returns like this "2"; "1" click opens first video even if I click second video.
If you need additional code to solve the problem please ask and I will provide it as soon as possible. Any help is appreciated. Thanks
The short answer is that onBindViewHolder() is trying to do too much. From the documentation:
Called by RecyclerView to display the data at the specified position. This method should update the contents of the ViewHolder#itemView to reflect the item at the given position.
In other words, onBindViewHolder() is only responsible for one single item in the RecyclerView. However, you are trying to fetch all of the data for every element in the list. Instead, you should fetch the data external to your adapter and pass it in as a parameter. Then onBindViewHolder() should update the UI elements of a view inside the RecyclerView to display whatever you want for one single item.
Google has a great example CustomerAdapter. First, the constructor takes the list of data that will be displayed:
public CustomAdapter(String[] dataSet) {
mDataSet = dataSet;
}
Then onbindViewHolder() is only responsible for setting what is displayed in the UI of a single item in the RecyclerView:
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
Log.d(TAG, "Element " + position + " set.");
// Get element from your dataset at this position and replace the contents of the view
// with that element
viewHolder.getTextView().setText(mDataSet[position]);
}
It does NOT try to get data or loop over a list or anything else. All of that is someone else's responsibility.

Android Spinner load with empty value (popup is shown before populating data)

I am using https://github.com/Chivorns/SmartMaterialSpinner Library for spinner.
When i first click the spinner no adapter is attached to it or spinner items shows empty list..
like
but when i close it and again select it.. data are shown so I believe that .. due to time consuming data fetching .. spinner show empty dialog.. Now I need to handle that.. How can i only show spinner when data is populated or available.. I have tried async task and handler but not get working.. any hint would be appreciated.. Thank you
Edited ...
I have call api on spinner onTouch event to populate data
{
private void ProvinceSpinnerCode(boolean IsEditCase) {
if (IsEditCase) {
//edit case condition
} else {
provinceSpinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mService = RetrofitManager.getApiClient().create(RetrofitInstance.class);
Call<List<SelectModel>> provinceCall = mService.GetProvinces();
provinceCall.enqueue(new Callback<List<SelectModel>>() {
#Override
public void onResponse(Call<List<SelectModel>> call, Response<List<SelectModel>> response) {
if (response.body().size() >= 0) {
provinceSpinner.setAdapter(new SpinnerAdapter(response.body(),AddCustomerActivity.this));
}
}
#Override
public void onFailure(Call<List<SelectModel>> call, Throwable t) {
}
});
return false;
}
});
}
provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(AddCustomerActivity.this, "Id is : " + id, Toast.LENGTH_SHORT).show();
provinceId = (int) id;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
You are making an API call inside on touch event which will be fired when you will touch the spinner so to solve the problem just do Api call out side that event (may be inside Oncreate).
Right now provinceSpinner has a touch listerner so when you are touching it to open for the first time it is Fetching data so when you are clicking it again the alert is showing data (the data you fetched when you clicked first time)

How to edit a child value when under a firebase auto generated key

I am trying to edit the value of "shopB" in my database. I am doing so inside of a view holder, by use of a dialog box with an edit text field and an approve button. When a user clicks on a specific transaction, they're given a dialog box where they enter the value for shopB and then click Approve.
I am struggling to do this as I cannot access that value because of the uniquely generated key that firebase has. I have many posts with similar problems to mine but as I am doing this inside of a view holder I do not see how I can use DataSnapshot. Any help would be greatly appreciated as I am getting very lost.
Database Structure:
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(android.view.View view, int position, boolean isLongClick) {
Toast.makeText(Request.this, "Receiving: " + shopA, Toast.LENGTH_SHORT).show();
ThisDialog = new Dialog(Request.this);
ThisDialog.setContentView(R.layout.dialog_template);
final EditText Write = (EditText) ThisDialog.findViewById((R.id.write));
Button Approve = (Button) ThisDialog.findViewById(R.id.approve);
Write.setEnabled(true);
Approve.setEnabled(true);
Approve.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String newShopB = Write.getText().toString().replace(".", " ");
transaction.child("key").child("shopB").setValue(newShopB);
Toast.makeText(Request.this, "CustB Approval", Toast.LENGTH_SHORT).show();
ThisDialog.cancel();
}
});
ThisDialog.show();
}
});
This would be my code but obviously where I have "transaction.child("key")" does not find the key.
Oh and transaction is defined earlier in my code as
database = FirebaseDatabase.getInstance();
start = database.getReference("Transaction");
transaction = start.child(passedEmail);
Here's a possible solution to workaround on your project:
start.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.child(key).getChildren()) {
//insert additional codes here
}
}
});

Android Firebase RecyclerView Adapter Remove Views

I am facing a Firebase RecyclerView problem where I cannot remove unwanted CardViews from my RecyclerViews. In my code I check the city's name and the guide's chosen city to match them. It populates guide's details only if the guide's city matches the picked city, but it also shows empty cardview with default layout.
guideDataRef = FirebaseDatabase.getInstance().getReference().child("Guides");
public void recycler() {
super.onStart();
try {
//Guide RecyclerView
Query guideQuery = guideDataRef.orderByKey();
guideQuery.keepSynced(true);
FirebaseRecyclerOptions guideOptions =
new FirebaseRecyclerOptions.Builder<UserModelClass>().setQuery(guideQuery, UserModelClass.class).build();
guideAdapter = new FirebaseRecyclerAdapter<UserModelClass, guideViewHolder>(guideOptions) {
#Override
protected void onBindViewHolder(#NonNull guideViewHolder holder, final int position, #NonNull final UserModelClass model) {
String pickedcity = model.getPickedCity();
String postname = (String) cityName.getText();
if(pickedcity.equals(postname)) {
final String guide_key= getRef(position).getKey();
holder.setGuideName(model.getName());
holder.setGuideSurname(model.getSurName());
holder.setGuideImage(getApplicationContext(), model.getPhotoURL());
// holder.mView.setVisibility(View.VISIBLE);
//Guide Click listener
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent guideHireIntent = new Intent(getApplication(), GuideHireActivity.class);
guideHireIntent.putExtra("guide_id", guide_key);
finish();
startActivity(guideHireIntent);
}
});
}
}
#NonNull
#Override
public guideViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout_guides, parent, false);
return new guideViewHolder(view);
}
#Override
public void onError(DatabaseError e){
Toast.makeText(getApplicationContext(), "Error by stopping ", Toast.LENGTH_SHORT).show();
}
#Override
public int getItemCount() {
return super.getItemCount();
}
#Override
public void onDataChanged() {
super.onDataChanged();
notifyDataSetChanged();
}
};
guideAdapter.notifyDataSetChanged();
guideRecyclerView.setAdapter(guideAdapter);
guideAdapter.startListening();
} catch (DatabaseException e) {
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
}
enter image description here
enter image description here
I can change the adapter visibility to gone if it does not match with the requirements but the problem is that after making it's visibility gone it is still there holding the place (but invisible - there's still an empty space). How can I avoid populating an item from the recycler view completely, instead of making it invisible if the requirements do not match?
You're not showing what guideDataRef is in your code, so I'm assuming that it's just aDatabaseReference object for everything beneath a \Guides node.
If you're doing that, you're going to get a call for onBindViewHolder for every child at that particular location. This means that you're going to be asked to make a view for every child. You cannot choose whether or not a view will appear for that item.
It looks like you're assuming that your if statement in onBindViewHolder method will skip over those items. But what's actually happening is that you're simply allowing an empty view to occupy that spot in the list.
Instead, you should come up with a query that generates only the items of interest to your list. This means you'll have to tell Firebase to filter for children that meet your criteria.
You can also read the entire contents of the location, manually filter out the items you don't want, and build a list of items you do want. You can then build an custom adapter with that list, and it can then become the input to a ListView or even better to a RecyclerView.

#Askfirebase Get the previous item values(POJO) in firebase recycler adapter in android

AskFirebase How to get the previous item values(POJO) in firebase recycler adapter without using database query.
// Set up FirebaseRecyclerAdapter with the Query
Query postsQuery = getQuery(mDatabase);
mAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(Post.class, R.layout.item_post,
PostViewHolder.class, postsQuery) {
#Override
protected void populateViewHolder(final PostViewHolder viewHolder, final Post model, final int position) {
final DatabaseReference postRef = getRef(position);
Log.e(TAG, "populateViewHolder: " + position);
// Set click listener for the whole post view
final String postKey = postRef.getKey();
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Launch PostDetailActivity
Intent intent = new Intent(getActivity(), PostDetailActivity.class);
intent.putExtra(PostDetailActivity.EXTRA_POST_KEY, postKey);
startActivity(intent);
}
});
// Determine if the current user has liked this post and set UI accordingly
if (model.stars.containsKey(getUid())) {
viewHolder.starView.setImageResource(R.drawable.ic_toggle_star_24);
} else {
viewHolder.starView.setImageResource(R.drawable.ic_toggle_star_outline_24);
}
// Bind Post to ViewHolder, setting OnClickListener for the star button
viewHolder.bindToPost(model, new View.OnClickListener() {
#Override
public void onClick(View starView) {
// Need to write to both places the post is stored
Log.e(TAG, "new: ");
DatabaseReference globalPostRef = mDatabase.child("posts").child(postRef.getKey());
DatabaseReference userPostRef = mDatabase.child("user-posts").child(model.uid).child(postRef.getKey());
// Run two transactions
onStarClicked(globalPostRef);
onStarClicked(userPostRef);
}
});
}
};
mRecycler.setAdapter(mAdapter);
Suppose their are five cell list whenever i am facing second cell in the list that time i want to put a condition based on first cell value. So how i can fatch the value of first cell?
I already try to using arraylist to store the POJO of Post . But the problem is whenever some item is deleted from firebase table that item onDataChange call but populateViewHolder doesn't call. Their is also a way to get previous data using database query that is
DatabaseReference ref = getRef(position-1);
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Log.e(TAG, "CHild exist: ");
} else {
Log.e(TAG, "no CHild exist: ");
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
But i don't want to use this database query is their any other way?
The Design Firebase data structure for topic Answer and Comment its like your problem.
gdtdg6765rf and hjgdhs567hd are unique key get by firebase
hjgdhs567hd is answer
gdtdg6765rf is comment to answer hjgdhs567hd
created is -1*UNIX Timestamp for ordering
date, time and toanswer was saved in comments by answer belong to
if to delete answer set all flags "deleted=1" where child "toanswer=deleted answer key" to populate again
#eurosecom above image is my layout where their is a recycler view which populate through FirebaseRecyclerAdapter . Now those green cell is my single cell. you see a red circle which denote the date. in case of position==0 I just simple visible the layout, and in case of position>0 i want to put the condition based on previous item date.
Now in my FirebaseRecyclerAdapter i have to put the condition so i have to fetch the previous position date. So as i am already doing a network oparetion using Query to fetch the msg list i don't want to put addListenerForSingleValueEvent in the populateview again as because it will again fetch the val from database. So is their any other way to get the previous item?

Categories