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
Related
FirebaseStorage storage;
StorageReference downloadRef;
StorageReference listRef;
Object Downloads;
ArrayList<String> arrayList;
ArrayAdapter arrayAdapter;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
arrayList = new ArrayList<>();
listRef = storage.getInstance().getReference("Project");
listRef.listAll().addOnSuccessListener(new OnSuccessListener<ListResult>() {
#Override
public void onSuccess(ListResult listResult) {
for (StorageReference items : listResult.getItems()) {
arrayList.add(items.getName());
}
}
});
arrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
}
So I am new to android, and this is what I tried to get the names of the documents so I can display them with listView. What am I getting wrong?
It seems like your onSuccess is getting called, which means you get a list of files from Firebase.
What's most likely happening is that you're processing this list correctly, but then not telling the adapter that you've changed its data. To tell the adapter that its data has changed call notifyDataSetChanged on it:
listRef.listAll().addOnSuccessListener(new OnSuccessListener<ListResult>() {
#Override
public void onSuccess(ListResult listResult) {
for (StorageReference items : listResult.getItems()) {
arrayList.add(items.getName());
}
arrayAdapter.notifyDataSetChanged();
}
});
At that point the adapter will repaint the associated views, and your data should show up.
Just trying to show in a RecyclerView the info from my database.
everything works fine. The objects are displayed.
but logcat says:
E/RecyclerView: No adapter attached; skipping layout
This is my the code (DisplayImages.java)
public class DisplayImages extends AppCompatActivity {
DatabaseReference databaseReference;
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
ProgressDialog progressDialog;
List<ImageUploadInfo> list = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_images);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(DisplayImages.this));
progressDialog = new ProgressDialog(DisplayImages.this);
progressDialog.setMessage("Loading Images From Firebase.");
progressDialog.show();
// Setting up Firebase image upload folder path in databaseReference.
// The path is already defined in MainActivity.
databaseReference = FirebaseDatabase.getInstance().getReference(Main.Database_Path);
databaseReference.addValueEventListener(new ValueEventListener() {
#override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
ImageUploadInfo imageUploadInfo = postSnapshot.getValue(ImageUploadInfo.class);
list.add(imageUploadInfo);
}
adapter = new RecyclerViewAdapter(getApplicationContext(), list);
recyclerView.setAdapter(adapter);
progressDialog.dismiss();
}
#Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
And If it makes any difference, this is what i wrote in MainActivity about the DataBase_Path :
public class Main extends AppCompatActivity implements View.OnClickListener {
DatabaseReference databaseReference;
public static final String Database_Path = "All_Image_Uploads_Database";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseReference = FirebaseDatabase.getInstance().getReference(Database_Path);
As you can see I have attached an adapter for Recycleview. so why do I keep getting this error?
i have read other questions related to same problem but none helps.
Yyou need to set the adapter after you set the layour manager, otherwise you'll get the error message you're seeing.
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
//don't set adapter here
recyclerView.setLayoutManager(new LinearLayoutManager(DisplayImages.this));
//set adapter here
Moreover, you can set the adapter with an empty list first and then when you receive the callback from firebase you can update the list and call notifyDatasetChanged() on the adapter. The problem you see is due to the fact that you're not actually setting the adapter until very later in the process (when your firebase call comes back)
Like this:
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(DisplayImages.this));
adapter = new RecyclerViewAdapter(getApplicationContext(), list);
recyclerView.setAdapter(adapter);
databaseReference.addValueEventListener(new ValueEventListener() {
#override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
ImageUploadInfo imageUploadInfo = postSnapshot.getValue(ImageUploadInfo.class);
list.add(imageUploadInfo);
}
adapter.notifyDatasetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
I don't see anywhere you are creating an adapter and setting that adapter to RecyclerView. You need to create an adapter and set adapter like following:
mAppAdapter = new AppAdapter(mModel); // or whatever constructor you want
recyclerView.setAdapter(mAppAdapter);
EDIT:
You are setting adapter to RecyclerView inside an asynchronous method. So your program will have a non deterministinc amount of time where the adapter is not actually set to recyclerView. To get rid off the warning you get you should initialize and set the adapter above the async method and only update data of the adapter inside the async method
I made a retrofit call in android and got a list response objects back but for some reason, they are not getting displayed in the listview and the application is not crashing either.
public class MainActivity extends AppCompatActivity {
private OpenNotifyService mService;
ListView listView ;
List<Response> arrayList = new ArrayList<Response>();
ArrayAdapter<Response> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mService = ApiUtils.getOpenNotifyService();
// Get ListView object from xml
listView = findViewById(R.id.myListView);
adapter = new ArrayAdapter<Response>(this,android.R.layout.simple_list_item_1, android.R.id.text1, arrayList);
loadAnswers();
}
public void loadAnswers() {
mService.getTimes(12,10).enqueue(new Callback<InternationalSpaceStationPassTimes>() {
#Override
public void onResponse(Call<InternationalSpaceStationPassTimes> call, retrofit2.Response<InternationalSpaceStationPassTimes> response) {
Log.e("Request URL", call.request().url().toString());
if (response.isSuccessful()) {
arrayList= response.body().getResponse();
// Assign adapter to ListView
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});
Log.d("MainActivity", "posts loaded from API");
} else {
int statusCode = response.code();
// handle request errors depending on status code
}
}
#Override
public void onFailure(Call<InternationalSpaceStationPassTimes> call, Throwable t) {
Log.d("MainActivity", "error loading from API");
}
});
}
Please help me..
Your list is initialized when your Activity instance is constructed:
List<Response> arrayList = new ArrayList<Response>();
During onCreate(), your adapter is constructed:
adapter = new ArrayAdapter<Response>(this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
arrayList);
Inside your loadAnswers() callback, your list is reinitialized:
arrayList= response.body().getResponse();
Finally, you attach the adapter to the listview:
listView.setAdapter(adapter);
Your problem is that your callback assigns a different List instance to your arrayList variable. Since your adapter was already created at this point, this means that the list inside of adapter is different from the list pointed to by arrayList. So nothing will appear.
You can solve this a couple of different ways. Probably the easiest would be to change your loadAnswers() callback to do this instead:
arrayList.clear();
arrayList.addAll(response.body().getResponse());
adapter.notifyDataSetChanged();
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.
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.