I am building an android app in which when the user create its account as a technician his name id display in a listview in which other users which don't have firebase outh.the problem is that I am unable to show data of technicians in the listview.
This is my firebase database
This is my code that return 0 items or blank listview. I tried several things, but unable to display the information of technicians. Anyone please help me with this I am new in firebase.
public class Technician extends Activity {
ListView listView;
ArrayList<String> tecnames = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_technician);
ImageButton Back_Btn = (ImageButton) findViewById(R.id.back);
Back_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Technician.this, Technician_main.class);
startActivity(i);
finish();
}
});
listView = (ListView) findViewById(R.id.list_item);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(Technician.this, Book_Now.class);
startActivity(intent);
finish();
}
});
Technician.CostumAdapter costumAdapter = new Technician.CostumAdapter();
listView.setAdapter(costumAdapter);
GetItems();
}
private class CostumAdapter extends BaseAdapter {
#Override
public int getCount() {
return tecnames.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.cuctom_listview, null);
TextView txt_one = (TextView) view.findViewById(R.id.skill_txt);
txt_one.setText(tecnames.get(i));
return view;
}
}
private void GetItems() {
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = database.child("user");
final Query itemsQuery = ref.orderByChild("name");
itemsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
Pojo item = singleSnapshot.getValue(Pojo.class);
tecnames.add(String.valueOf(item));
Log.e(TAG,"value of name"+itemsQuery);
}
}else {
Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Log.e(TAG, "Returning " + tecnames.size() + " items");
}
}
instead of this:
Technician.CostumAdapter costumAdapter = new Technician.CostumAdapter();
Write this:
Technician.CostumAdapter costumAdapter = new Technician.CostumAdapter(getApplicationContext(),tecnames);
Then add this constructor inside of the class:
private ArrayList<String> datas;
Context mContext;
public CustomAdapter(Context context, ArrayList<String> datas) {
super(context, R.layout.row_item, datas);
this.mContext=context;
this.datas = datas;
}
change addListenerforSingleValueEvent to addValueEventListener
Ok, there are few things that you need to fix:
1.Your array list tecnames should be of your POJO class
ArrayList<POJO> tecnames = new ArrayList<>();
Now change tecnames.add(String.valueOf(item)); to tecnames.add(item);
2.As #Peter Haddad suggested, you need to give this list to your adapter as a dataset.
3.Firebase DB listeners are asynchronous so you need to notify your adapter after you get data in your dataset(list). Add the following code inside onDataChange after your foreach loop:
costumAdapter.notifyDataSetChanged();
You can refer to this link for the 3rd point.
Related
Im trying to filter my listview based on a spinner selection. I can succesfully filter the listview. However, when I tried deleting on of the item on the list, the listview will not refreshed and even loaded unfiltered data into the list. Below is what i have tried so far.
My adapter
public class NoteList extends ArrayAdapter<Notes> {
private Activity context;
private List<Notes> noteList;
public NoteList(Activity context, List<Notes> noteList){
super(context, R.layout.list_layout, noteList);
this.context = context;
this.noteList = noteList;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_layout, null, true);
TextView textViewCategory = (TextView) listViewItem.findViewById(R.id.textViewCategory);
// TextView textViewNote = (TextView) listViewItem.findViewById(R.id.textViewNote);
TextView textViewRating = (TextView) listViewItem.findViewById(R.id.textViewRating);
Notes note = noteList.get(position);
textViewCategory.setText(note.getNoteCategory());
// textViewNote.setText(note.getNote());
textViewRating.setText(note.getRating());
return listViewItem;
}
}
And below is my MyNotes activity
public class MyNotes extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
ListView listViewNotes;
List<Notes> notesList;
DatabaseReference databaseNotes;
TextView category, date;
Spinner spinner;
NoteList adapter;
String display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_notes);
Slidr.attach(this);
listViewNotes = (ListView) findViewById(R.id.listViewNotes);
category = (TextView) findViewById(R.id.textViewCategory);
notesList = new ArrayList<>();
date = (TextView) findViewById(R.id.DateTime);
spinner = (Spinner) findViewById(R.id.notes_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.notes_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
databaseNotes = FirebaseDatabase.getInstance().getReference("notes");
databaseNotes.keepSynced(true);
String minutes = DateFormat.getDateTimeInstance().format(new Date());
date.setText(minutes);
listViewNotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Notes notes = notesList.get(i);
showUpdateDialog(notes.getNoteId(), notes.getNote(), notes.getRating(), notes.getNoteCategory(), notes.getCreationDateLong());
//return false;
}
});
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
display = adapterView.getItemAtPosition(i).toString();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
notesList.clear();
if (display.equals("Grateful")) {
notesList.clear();
Query query = reference.child("notes").orderByChild("rating").equalTo(display);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
notesList.clear();
for (DataSnapshot noteSnapshot : dataSnapshot.getChildren()) {
Log.d("Grateful Datasnapshot", ":"+noteSnapshot);
Notes notes = noteSnapshot.getValue(Notes.class);
notesList.add(notes);
}
adapter = new NoteList(MyNotes.this, notesList);
listViewNotes.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Toast.makeText(this, "Selected: " + display, Toast.LENGTH_SHORT).show();
}
if (display.equals("Ungrateful")) {
notesList.clear();
Query query = reference.child("notes").orderByChild("rating").equalTo(display);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
notesList.clear();
// if (dataSnapshot.exists()) {
for (DataSnapshot noteSnapshot : dataSnapshot.getChildren()) {
Notes notes = noteSnapshot.getValue(Notes.class);
notesList.add(notes);
Log.d("Ungrateful Datasnapshot", ":"+noteSnapshot);
}
adapter = new NoteList(MyNotes.this, notesList);
listViewNotes.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Toast.makeText(this, "Selected: " + display, Toast.LENGTH_SHORT).show();
} if (display.equals("All")){
notesList.clear();
databaseNotes.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
notesList.clear();
for (DataSnapshot noteSnapshot : dataSnapshot.getChildren()) {
if(noteSnapshot.exists()) {
Notes notes = noteSnapshot.getValue(Notes.class);
notesList.add(notes);
Log.d("All Datasnapshot", ":"+noteSnapshot);
}
}
adapter = new NoteList(MyNotes.this, notesList);
listViewNotes.setAdapter(adapter);
//adapter.notifyDataSetChanged();
// adapter.notifyDataSetInvalidated();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Toast.makeText(this, "Selected: " + display, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
buttonDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog diaBox = AskOption(noteId);
diaBox.show();
alertDialog.dismiss();
}
});
}
private void deleteNote(String noteId) {
DatabaseReference drNote = FirebaseDatabase.getInstance().getReference("notes").child(noteId);
drNote.removeValue();
// notesList.clear();
// Toast.makeText(this, "Note is deleted", Toast.LENGTH_LONG).show();
}
}
As you can see above, im trying to filter is by the selected value, eg All, Grateful and Ungrateful. After deleting an item in a filtered "Grateful" list, the list will generate unfiltered list. I've search and tried similar question in SO but nothing works.
Thank you in advance.
This is my database image
I am trying to add name node to a listview, how can I add a single firebase node into listview? I tried several things but unable to do it. My catlog say
"Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'com.google.firebase.database.DatabaseReference
com.google.firebase.database.FirebaseDatabase.getReference(java.lang.String)'
on a null object reference"
Please anyone tell me how to do this I am new in firebase.
public class Technician extends Activity {
private FirebaseDatabase database;
ListView listView;
ArrayList<String> tecnames = new ArrayList<>();
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_technician);
ImageButton Back_Btn = (ImageButton) findViewById(R.id.back);
Back_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Technician.this, Technician_main.class);
startActivity(i);
finish();
}
});
DatabaseReference myRef = database.getReference("user");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
String value = childSnapshot.getValue(String.class);
tecnames.add(value);
}
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
listView = (ListView) findViewById(R.id.list_item);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(Technician.this, Book_Now.class);
startActivity(intent);
finish();
}
});
Technician.CostumAdapter costumAdapter = new Technician.CostumAdapter();
listView.setAdapter(costumAdapter);
}
private class CostumAdapter extends BaseAdapter {
#Override
public int getCount() {
return tecnames.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.cuctom_listview, null);
TextView txt_one = (TextView) view.findViewById(R.id.skill_txt);
txt_one.setText(tecnames.get(i));
return view;
}
}
}
To solve this, change this line of code:
DatabaseReference myRef = database.getReference("user");
with
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = rootRef.child("user");
You are getting null because you didn't instantiate your reference.
Here OnItemclickListener is not getting called can anyone Know what is the problem?? Here is my Code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_item);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String Name = pairName[position];
Intent intent = new Intent(MainActivity.this, EditDetailsActivity.class);
intent.putExtra("Name", Name);
startActivity(intent);
}
});
getSwitchStatus(pairName,listView);
}
public ListView getSwitchStatus(final String[] pairName1, final ListView listView1) {
final ArrayList<Boolean> xz=new ArrayList<>();
String x;
for (int n =0 ;n<pairName1.length; n++) {
x = pairName1[n];
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference databaseReference = database.getReference("data").child(x.replace("/",""));;
DatabaseReference databaseReference11 = databaseReference.child("Switch");
Log.v("value111",String.valueOf(n)+databaseReference11.toString());
databaseReference11.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Boolean value = dataSnapshot.getValue(Boolean.class);
xz.add(value);
if (xz.size()==pairName1.length) {
sigAdapter adapter = new sigAdapter(MainActivity.this, pairName, imageId, xz);
listView1.setAdapter(adapter);
}
Log.v("valu",xz.toString());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
return listView1;
}
please try this,
may be it works.
your method call like this.
//holder.relQuestion is you row file main layout id so replace this with your id.
this method is put in your adapter
holder.relQuestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//here you can write your code
}
});
I have tried everything but my data from firebase is not getting loaded automatically when I open the app and I have to click a edittext to load the data.
I have tried
adapter.Update(data);
recycler.invalidate();
notifyDataSetChanged();
adapter.notifyDataSetChanged();
Please can someone help me out.
EDITED:
I am saving to firebase like this:
public FirebaseHelper(DatabaseReference db) {
this.db = db;
}
//WRITE IF NOT NULL
public Boolean save(Spacecraft spacecraft)
{
if(spacecraft==null)
{
saved=false;
}else
{
try
{
db.child("Spacecraft").push().setValue(spacecraft);
saved=true;
}catch (DatabaseException e)
{
e.printStackTrace();
saved=false;
}
}
return saved;
}
//IMPLEMENT FETCH DATA AND FILL ARRAYLIST
private void fetchData(DataSnapshot dataSnapshot)
{
spacecrafts.clear();
for (DataSnapshot ds : dataSnapshot.getChildren())
{
Spacecraft spacecraft=ds.getValue(Spacecraft.class);
spacecrafts.add(spacecraft);
}
}
//READ THEN RETURN ARRAYLIST
public ArrayList<Spacecraft> retrieve() {
db.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s){
fetchData(dataSnapshot);
}
});
return spacecrafts;
}
}
My viewholder class
public MyViewHolder(View itemView) {
super(itemView);
nameTxt= (TextView) itemView.findViewById(R.id.nameTxt);
propTxt= (TextView) itemView.findViewById(R.id.propellantTxt);
descTxt= (TextView) itemView.findViewById(R.id.descTxt);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener)
{
this.itemClickListener=itemClickListener;
}
#Override
public void onClick(View view) {
this.itemClickListener.onItemClick(this.getLayoutPosition());
}
My adapter class
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
Context c;
ArrayList<Spacecraft> spacecrafts;
public MyAdapter(Context c, ArrayList<Spacecraft> spacecrafts) {
this.c = c;
this.spacecrafts = spacecrafts;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v=LayoutInflater.from(c).inflate(R.layout.model,parent,false);
return new MyViewHolder(v);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final Spacecraft s=spacecrafts.get(position);
holder.nameTxt.setText(s.getName());
holder.propTxt.setText(s.getPropellant());
holder.descTxt.setText(s.getDescription());
holder.setItemClickListener(new ItemClickListener() {
#Override
public void onItemClick(int pos) {
//OPEN DETAI ACTIVITY
openDetailActivity(s.getName(),s.getDescription(),s.getPropellant());
}
});
}
#Override
public int getItemCount() {
return spacecrafts.size();
}
//OPEN DETAIL ACTIVITY
private void openDetailActivity(String...details)
{
Intent i=new Intent(c,DetailActivity.class);
i.putExtra("NAME_KEY",details[0]);
i.putExtra("DESC_KEY",details[1]);
i.putExtra("PROP_KEY",details[2]);
c.startActivity(i);
}
in my mainactivity in onCreate I am calling the data like this
recycler = (RecyclerView) rootView.findViewById(R.id.recycler);
recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
database = FirebaseDatabase.getInstance().getReference();
spacecraft = new ArrayList<>();
firebasehelper = new FirebaseHelper(database);
adapter = new JAdapter(getActivity(), firebasehelper.retrieve());
recycler.setAdapter(adapter);
The problem is with the retrieve method: it's adding a ChildEventListener that executes asynchronously (meaning it doesn't block until the children data are fetched) and then it immediately returns the spacecrafts list which is still not yet filled by the asynchronous listener code.
You should refresh the RecyclerView adapter in the actual code that fetches data, i.e. in the fetchData method, e.g.:
private void fetchData(DataSnapshot dataSnapshot)
{
spacecrafts.clear();
for (DataSnapshot ds : dataSnapshot.getChildren())
{
Spacecraft spacecraft=ds.getValue(Spacecraft.class);
spacecrafts.add(spacecraft);
}
adapter.spacecrafts = spacecrafts; // update the list in the adapter
adapter.notifyDataSetChanged(); // refresh
}
In this case, the retrieve method can have a void return type as it is pointless to return the list from it.
Use FirebaseRecyclerAdapter for load the data from Firebase.
modify the helper constructer to be like this:
public FirebaseHelper(Context c, DatabaseReference db, ListView lv) {
this.c = c;
this.db = db;
this.lv = lv;
}
then add this to the fetchData method after the for loop:
if (spacecrafts.size() > 0)
{
adapter = new CustomAdapter(c, spacecrafts);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
then in the main activity modify it like this:
helper = new FirebaseHelper(this, db, lv);
//ADAPTER
//adapter = new CustomAdapter(this, helper.retrieve());
//lv.setAdapter(adapter);
helper.retrieve();
this should work for you.
Check if you are setting up adapter in onCreate() method or not, if you are calling it in onStart() then it will not load data automatically. Set Adapter in onCreate() method.
I have a custom listview to display data from Firebase database.
SectionDetails model
public class SectionDetails {
private String sectionCode;
private String sectionSeats;
public SectionDetails() {
}
public SectionDetails(String sectionCode, String sectionSeats) {
this.sectionCode = sectionCode;
this.sectionSeats = sectionSeats;
}
public String getSectionCode() {
return sectionCode;
}
public String getSectionSeats() {
return sectionSeats;
}
public void setSectionCode(String sectionCode) {
this.sectionCode = sectionCode;
}
public void setSectionSeats(String sectionSeats) {
this.sectionSeats = sectionSeats;
}
}
FirebaseHelper class
public class FirebaseHelper {
DatabaseReference db;
Boolean saved;
ArrayList<SectionDetails> sectionDetailsArrayList = new ArrayList<>();
public FirebaseHelper(DatabaseReference db) {
this.db = db;
}
public Boolean save(SectionDetails sectionDetails) {
if (sectionDetails == null) {
saved = false;
} else {
try {
db.push().setValue(sectionDetails);
saved = true;
adapter.notifyDataSetChanged();
} catch(DatabaseException e) {
e.printStackTrace();
saved = false;
}
}
return saved;
}
private void fetchData(DataSnapshot dataSnapshot) {
sectionDetailsArrayList.clear();
SectionDetails sectionDetails = dataSnapshot.getValue(SectionDetails.class);
sectionDetailsArrayList.add(sectionDetails);
}
public ArrayList<SectionDetails> retrieve() {
db.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return sectionDetailsArrayList;
}
}
CustomAdapter class
public class CustomAdapter extends BaseAdapter {
Context c;
ArrayList<SectionDetails> sectionDetailsArrayList;
public CustomAdapter(Context c, ArrayList<SectionDetails> sectionDetailsArrayList) {
this.c = c;
this.sectionDetailsArrayList = sectionDetailsArrayList;
}
#Override
public int getCount() {
return sectionDetailsArrayList.size();
}
#Override
public Object getItem(int position) {
return sectionDetailsArrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(c).inflate(R.layout.sections_custom_listview, parent, false);
}
TextView lvTvSectionCode = (TextView) convertView.findViewById(R.id.lvTvSectionCode);
TextView lvTvSectionSeats = (TextView) convertView.findViewById(R.id.lvTvSectionSeats);
final SectionDetails sd = (SectionDetails) this.getItem(position);
lvTvSectionCode.setText(sd.getSectionCode());
lvTvSectionSeats.setText("allocated seats: " + sd.getSectionSeats());
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(c, sd.getSectionCode(), Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
MainActivity class
public class AddSection extends AppCompatActivity implements View.OnClickListener {
DatabaseReference mRef;
FirebaseHelper helper;
CustomAdapter adapter;
Button btnCreateSection;
ListView lvSectionsListOne;
String getFullSection, seats;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_section);
lvSectionsListOne = (ListView) findViewById(R.id.lvSectionsList);
mRef = FirebaseDatabase.getInstance().getReference().child(Constants.FIREBASE_COURSES).child("sections");
helper = new FirebaseHelper(mRef);
adapter = new CustomAdapter(this, helper.retrieve());
lvSectionsListOne.setAdapter(adapter);
btnCreateSection = (Button) findViewById(R.id.btnCreateSection);
btnCreateSection.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == btnCreateSection) {
getFullSection = "section 1A";
seats = "20";
SectionDetails sectionDetails = new SectionDetails(getFullSection, seats);
if (helper.save(sectionDetails)) {
adapter = new CustomAdapter(AddSection.this, helper.retrieve());
lvSectionsListOne.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
}
I posted a similar question here, but this problem is different. When first data is added, nothing is shown in the listview. When second data is added, first data is shown in listview. And when third data is added, first data is replaced by second data, and second data is shown in listview. I have tried adding adapter.notifyDataSetChanged(), but still the same result.
Data is retrieved (and synchronized) from the Firebase Database to your app asynchronously. This means that your sectionDetailsArrayList may be modified at any time. When you modify the data, you need to tell the adapter about it, so that it can update the view.
private void fetchData(DataSnapshot dataSnapshot) {
sectionDetailsArrayList.clear();
SectionDetails sectionDetails = dataSnapshot.getValue(SectionDetails.class);
sectionDetailsArrayList.add(sectionDetails);
// tell the adapter that we changed its data
adapter.notifyDataSetChanged();
}
This should update the view. But since you clear out the list for every child that gets added or changed, the app will only show the latest added/modified item.
Setting up a synchronized array in Firebase is somewhat involved, since you have to deal with all event types and with the fact that evens can happen in any order. For that reason we create the FirebaseUI library, which contains adapters from the Firebase Database to a ListView and RecyclerView.