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.
Related
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.
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);
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
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.
I'm having trouble reloading the ListView after pushing a string to firebase. ListView updates when I restart the application. I have used the notifyDataSetChanged() function with my ListAdapter. Here is what I'm doing:
myFirebaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("App",dataSnapshot.getValue().toString());
names = dataSnapshot.getValue().toString();
String[] planets = new String[] { names, "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
ArrayList<String> planetList = new ArrayList<String>();
namesList.addAll( Arrays.asList(planets) );
// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, android.R.id.text1, namesList);
mainListView.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
Firebase engineer here,
I would recommend checking out a simple chat example I made recently (what you're trying to do is detailed here) or our more fully fleshed out Android Chat example, which both demonstrate how to use model objects to bind to ArrayAdapters and ListViews.
The TL;DR is that you want to do something more like this:
// Create instance variables to be used from inside your activity
private ArrayList<Map<String, Object>> mMessages;
private Firebase mRef;
...
// Some time later, instantiate your instance variables
this.mMessages = new ArrayList<Message>();
// Set up ListAdapter
ListAdapter messageAdapter = new ArrayAdapter(this , R.layout.message_layout , mMessages);
this.setListAdapter(messageAdapter);
// Set up Firebase
Firebase.setAndroidContext(this);
this.mRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
// Add listener to your Firebase database reference
this.mRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// Create objects and add to ArrayList, which should fire ArrayAdapter methods and update the ListView
Map<String, Object> data = (Map<String, Object>)dataSnapshot.getValue();
MainActivity.this.mMessages.add(data);
}
...
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
Every time you are creating new listadapter. Just use the old one. There's a conflict here because you are creating a new adapter and setting it to the same time you are calling notifyDatachanged. It is not logical. Do not bind new adapter.