Get firebase data when selecting RecyclerView android - android

I has two RecyclerView. For example, if I click recyclerview1, I want to take his rooms, and if I click recyclerview2, I want to take his rooms. I want to capture data based on my selected RecyclerView how can I do this? For example,
if recyclerView1 is selected, "DatabaseReference refRooms =
mDatabase.child (chipNumberNew).child("Rooms");"
if recyclerView2 is selected, I want to get "DatabaseReference =
mDatabase.child (chipNumberNewrecyclerView2).child ("Rooms");"
DatabaseReference refRooms = mDatabase.child(chipNumberNew).child("Rooms");
refRooms.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
spinner = (Spinner)itemView.findViewById(R.id.spinnerMain);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, Rooms);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
String data = snapshot.getValue(String.class);
Rooms.add(data);
addListenerOnSpinnerItemSelection();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}

You can use a recyclerview on click listener and depending on what recyclerview was clicked you can call a method.

Related

how to get back the scroll position of a listview on back press from another activity

I make a simple music app when I click on any item on Custom Listview its open new activity and when I press back button its showing listview from the top.
I need listView position where I click or where I am
//Getting List view.....
listView = (ListView) findViewById(R.id.MyListView);
myroot = FirebaseDatabase.getInstance().getReference("all");
readVariable = new ArrayList<>();
//Ends...............
// save index and top position
int index = listView.getFirstVisiblePosition();
View v = listView.getChildAt(0);
int top = (v == null) ? 0 : (v.getTop() - listView.getPaddingTop());
// restore index and position
listView.setSelectionFromTop(index, top);
}
//FireBase Work>>>>>>>>>>>>>>>>>>>
#Override
protected void onStart() {
super.onStart();
myroot.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
readVariable.clear();
for (DataSnapshot artisSnapshot : dataSnapshot.getChildren()) {
ReadVariables read = artisSnapshot.getValue(ReadVariables.class);
readVariable.add(read);
}
CustomList adapter = new CustomList(MainActivity.this, readVariable);
listView.setAdapter(adapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(MainActivity.this,"Check your network",Toast.LENGTH_LONG).show();
}
});
}
//Ends>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
}
Define readVariable and adapter as Global Variables.
CustomList adapter;
ArrayList<> readVariable = new ArrayList<>();
Remove
readVariable = new ArrayList<>();
We have already defined readVariable as a global variable and have initialized it.
Remove below lines from onDataChange
CustomList adapter = new CustomList(MainActivity.this, readVariable);
listView.setAdapter(adapter);
and write here like this
//Getting List view.....
listView = (ListView) findViewById(R.id.MyListView);
myroot = FirebaseDatabase.getInstance().getReference("all");
adapter = new CustomList(MainActivity.this, readVariable);
listView.setAdapter(adapter);
//Ends...............
Now where setAdapter line was written earlier in onDataChange,
Write this:
listView.notifyDataSetChanged();

How to add Hint in Spinner widget populated with data from FireBase Database?

My xml code is :
<Spinner
android:id="#+id/destination"
android:layout_width="344dp"
android:layout_height="30dp"
android:autofillHints="Choose destination"
android:spinnerMode="dropdown" />
I want to add hint (without using dummy entry) as to be shown to user when the activity starts.
The inbuilt xml hint feature is not working after using FireBase.
MainActivity :
myRef.child("Stations").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<String> stationname = new ArrayList<String>();
for (DataSnapshot stationSnapshot : dataSnapshot.getChildren()) {
String stationName = stationSnapshot.child("Name").getValue(String.class);
stationname.add(stationName);
}
Spinner sp2= findViewById(R.id.destination);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(FairPage.this, android.R.layout.simple_spinner_item, stationname);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(arrayAdapter);
Try this
myRef.child("Stations").addListenerForSingleValueEvent(new
ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<String> stationname = new ArrayList<String>();
stationname.add("Select Station");
for (DataSnapshot stationSnapshot : dataSnapshot.getChildren()) {
String stationName = stationSnapshot.child("Name").getValue(String.class);
stationname.add(stationName);
}
Spinner sp2= findViewById(R.id.destination);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(FairPage.this, android.R.layout.simple_spinner_item, stationname);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(arrayAdapter);
and also check when spinner item selected
if (stationname.get(postion).equals("Select Station"){
//Toast please select station
}
Just add one item in array list before setting the data from firebase. As done -
stationname.add("Select Station"); // hint
myRef.child("Stations").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<String> stationname = new ArrayList<String>();
for (DataSnapshot stationSnapshot : dataSnapshot.getChildren()) {
String stationName = stationSnapshot.child("Name").getValue(String.class);
stationname.add(stationName);
}
Spinner sp2= findViewById(R.id.destination);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(FairPage.this, android.R.layout.simple_spinner_item, stationname);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(arrayAdapter);

Data from firebase into table

I have created listview, which contains data from firebase. Now, I want to add this data into table, but I don't have any idea how to do it.
Here is my code:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("Highscore");
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<String> areas = new ArrayList<String>();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Score score = postSnapshot.getValue(Score.class);
areas.add(score.getName()+" "+score.getLevel()+" "+score.getScore());
}
ListView highScoreSpin = (ListView)findViewById(R.id.list);
ArrayAdapter<String> areasAdapter = new ArrayAdapter<String>(HighscoreActivity.this, android.R.layout.simple_list_item_1, areas);
areasAdapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
highScoreSpin.setAdapter(areasAdapter);
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});
Having score.getName(), score.getLevel() and score.getScore() you can easely add it into a TableView. Please read this post to learn how it can be done.
Remember to put this lines of code outside the onDataChange() method.
ListView highScoreSpin = (ListView)findViewById(R.id.list);
ArrayAdapter<String> areasAdapter = new ArrayAdapter<String>(HighscoreActivity.this, android.R.layout.simple_list_item_1, areas);
areasAdapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
highScoreSpin.setAdapter(areasAdapter);
Hope it helps.

Firebase data retrieval returns an empty screen

I am trying to retrieve data from firebase to a listview...However this code returns a blank screen .My database has got 3 children with for data fields each.
All i see is an empty screen.
I have no idea on how to solve this anyone please:The code is here
public class Business extends AppCompatActivity {
private ListView business;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_business);
}
public ArrayList<String> arr;
public ArrayAdapter adapter;
#Override
protected void onStart() {
super.onStart();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
arr = new ArrayList<>();
ValueEventListener listener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
map2list((Map) dataSnapshot.getValue());
//formats the datasnapshot entries to strings
adapter.notifyDataSetChanged();
//makes the ListView realtime
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
System.out.println(databaseError.toException());
// ...
}
};
mDatabase.addValueEventListener((com.google.firebase.database.ValueEventListener) listener);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, arr);
ListView listView = (ListView) findViewById(R.id.business);
listView.setAdapter(adapter);
}
public void map2list(Map<String,Long> map){
arr.clear();
for (Map.Entry<String, Long> entry : map.entrySet()) {
Long key = Long.parseLong(entry.getKey());
String d = DateFormat.getDateTimeInstance().format(key);
Long value = entry.getValue();
arr.add(d + ": " + value);
}
}
}
If the whole page is blank when you set the content view, this might have happened if one of your views in your layout has a layout_height attribute of 'match parent'. This may be one of the reasons why you are not seeing anything. Another reason why you might not bee seeing anything is that listeners run on separate threads. Try rearranging your code like this.
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, arr);
ListView listView = (ListView) findViewById(R.id.business);
listView.setAdapter(adapter);
mDatabase.addValueEventListener((com.google.firebase.database.ValueEventListener) listener);
// add the listner after setting the adapter

Spinner populated with Firebase information does not show selected item

Disclaimer
I did a lot of research before making this question, and I know that there is a lot of questions like this with some answers, but none of them solved my problem, so I am making this question as an act of despair.
Context
I'm populating a spinner with an array list populated with information from Firebase database.
The problem
The spinner show the information when I click on it, but does not show the selected icon.
The code
Getting the information:
public static List<String> getStudentsList() {
final List<String> studentsList = new ArrayList<>();
studentsList.add("");
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Students");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot studentSnapshot: dataSnapshot.getChildren()) {
String studentName = studentSnapshot.getValue(Student.class).getStudentName();
studentsList.add(studentName);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return studentsList;
}
Setting the spinner:
private void setUpStudentSpinner() {
List<String> studentsList = FirebaseUtils.getStudentsList();
ArrayAdapter<String> studentAdapter = new ArrayAdapter<>(getApplicationContext(),
android.R.layout.simple_spinner_dropdown_item, studentsList);
studentAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner studentSpinner = (Spinner) findViewById(R.id.spinnerLessonStudent);
studentSpinner.setAdapter(studentAdapter);
}
And I call the method setUpStudentSpinner() on onCreate().
Hope someone can help me.

Categories

Resources