Displaying Arraylist on Listview trouble
So I have an app that will let you create a java object containing information about a pill you take (Name, dose amount, number of doses per day, and number of pills left). I then store this information of firebase real-time database, then I use a separate activity to get the information from the database and store it in an array list, which i want to be displayed in a list view.Current issue is that the list view is not showing all data of array list, only the last pill object that was added.
Code for the uploading of data:
package com.example.andrew.pilltracker;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.Toolbar;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.auth.*;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class SecondActvty extends AppCompatActivity {
static String ref = "Advil";
private FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRootRef;
int id = 1;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
Intent intent = new Intent(SecondActvty.this, MainActivityController.class);
startActivity(intent);
return true;
case R.id.navigation_dashboard:
return true;
case R.id.navigation_notifications:
Intent intent1 = new Intent(SecondActvty.this, ThirdActivity.class);
startActivity(intent1);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_view);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRootRef = mFirebaseDatabase.getReference();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//id++;
EditText pillName = (EditText)findViewById(R.id.pillName);
EditText pillDose = (EditText)findViewById(R.id.pillDose);
EditText pillCount = (EditText)findViewById(R.id.pillCount);
EditText doseDuration = (EditText)findViewById(R.id.doseDuration);
String name = pillName.getText().toString();
String dose = pillDose.getText().toString();
String day = doseDuration.getText().toString();
String count = pillCount.getText().toString();
ref = name;
Pill pill = new Pill();
pill.setName(name);
pill.setCount(count);
pill.setDay(day);
pill.setDose(dose);
if(!name.equals("") && !dose.equals("") && !day.equals("") && !count.equals("")) {
mRootRef.child("pills").child(name).setValue(pill);
toastMessage("Success!");
pillName.setText("");
pillCount.setText("");
doseDuration.setText("");
pillDose.setText("");
}
else {
toastMessage("Please fill all fields");
}
}
});
}
private void toastMessage(String message){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
public static String getReference(){
return ref;
}
}
And the class that will download the data from firebase and show it in the listview:
package com.example.andrew.pilltracker;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class MainActivityController extends AppCompatActivity {
private TextView mTextMessage;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mRootRef;
private ArrayList<String> array;
private ListView mListView;
private ArrayAdapter adapter;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
Intent intent = new Intent(MainActivityController.this, SecondActvty.class);
startActivity(intent);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_view);
mListView = (ListView) findViewById(R.id.listview);
array = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRootRef = mFirebaseDatabase.getReference();
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
mRootRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
showData(dataSnapshot);
mListView.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void showData(DataSnapshot dataSnapshot){
for(DataSnapshot ds : dataSnapshot.getChildren()){
Pill pInfo = new Pill();
pInfo.setName(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getName());
pInfo.setCount(ds.child(SecondActvty.getReference().toString()/*Integer.toString(databaseRef)*/).getValue(Pill.class).getCount());
pInfo.setDose(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDose());
pInfo.setDay(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDay());
array.add(pInfo.getName());
array.add("Number of Pills Left: " + pInfo.getCount());
array.add("Number of Pills per Dose: " + pInfo.getDose());
array.add("Number of Doses per Day: " + pInfo.getDay());
}
}
//Database shit
@Override
protected void onStart(){
super.onStart();
}
}
Database structure:
Database
java firebase
add a comment |
So I have an app that will let you create a java object containing information about a pill you take (Name, dose amount, number of doses per day, and number of pills left). I then store this information of firebase real-time database, then I use a separate activity to get the information from the database and store it in an array list, which i want to be displayed in a list view.Current issue is that the list view is not showing all data of array list, only the last pill object that was added.
Code for the uploading of data:
package com.example.andrew.pilltracker;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.Toolbar;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.auth.*;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class SecondActvty extends AppCompatActivity {
static String ref = "Advil";
private FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRootRef;
int id = 1;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
Intent intent = new Intent(SecondActvty.this, MainActivityController.class);
startActivity(intent);
return true;
case R.id.navigation_dashboard:
return true;
case R.id.navigation_notifications:
Intent intent1 = new Intent(SecondActvty.this, ThirdActivity.class);
startActivity(intent1);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_view);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRootRef = mFirebaseDatabase.getReference();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//id++;
EditText pillName = (EditText)findViewById(R.id.pillName);
EditText pillDose = (EditText)findViewById(R.id.pillDose);
EditText pillCount = (EditText)findViewById(R.id.pillCount);
EditText doseDuration = (EditText)findViewById(R.id.doseDuration);
String name = pillName.getText().toString();
String dose = pillDose.getText().toString();
String day = doseDuration.getText().toString();
String count = pillCount.getText().toString();
ref = name;
Pill pill = new Pill();
pill.setName(name);
pill.setCount(count);
pill.setDay(day);
pill.setDose(dose);
if(!name.equals("") && !dose.equals("") && !day.equals("") && !count.equals("")) {
mRootRef.child("pills").child(name).setValue(pill);
toastMessage("Success!");
pillName.setText("");
pillCount.setText("");
doseDuration.setText("");
pillDose.setText("");
}
else {
toastMessage("Please fill all fields");
}
}
});
}
private void toastMessage(String message){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
public static String getReference(){
return ref;
}
}
And the class that will download the data from firebase and show it in the listview:
package com.example.andrew.pilltracker;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class MainActivityController extends AppCompatActivity {
private TextView mTextMessage;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mRootRef;
private ArrayList<String> array;
private ListView mListView;
private ArrayAdapter adapter;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
Intent intent = new Intent(MainActivityController.this, SecondActvty.class);
startActivity(intent);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_view);
mListView = (ListView) findViewById(R.id.listview);
array = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRootRef = mFirebaseDatabase.getReference();
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
mRootRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
showData(dataSnapshot);
mListView.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void showData(DataSnapshot dataSnapshot){
for(DataSnapshot ds : dataSnapshot.getChildren()){
Pill pInfo = new Pill();
pInfo.setName(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getName());
pInfo.setCount(ds.child(SecondActvty.getReference().toString()/*Integer.toString(databaseRef)*/).getValue(Pill.class).getCount());
pInfo.setDose(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDose());
pInfo.setDay(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDay());
array.add(pInfo.getName());
array.add("Number of Pills Left: " + pInfo.getCount());
array.add("Number of Pills per Dose: " + pInfo.getDose());
array.add("Number of Doses per Day: " + pInfo.getDay());
}
}
//Database shit
@Override
protected void onStart(){
super.onStart();
}
}
Database structure:
Database
java firebase
1
Please attach your database structure with example data stored in it and remove the first instance of code.
– PradyumanDixit
Nov 27 '18 at 3:34
@PradyumanDixit I have attached a picture of the database at the bottom of the post. I don't know what you mean by remove the first instance of code. Thanks!
– Andrew Hunter
Nov 27 '18 at 4:49
I mean remove the first code you've put for setting the values in database, if there is no problem with that. Also don't put your firebase URL in images, it can be misused.
– PradyumanDixit
Nov 27 '18 at 4:54
Hey @AndrewHunter, do mark the answer as correct by clicking tick mark type V shaped button next to the answer, it should turn green. This helps future readers of the question and I'd appreciate that too. Cheers! :)
– PradyumanDixit
Dec 3 '18 at 5:55
add a comment |
So I have an app that will let you create a java object containing information about a pill you take (Name, dose amount, number of doses per day, and number of pills left). I then store this information of firebase real-time database, then I use a separate activity to get the information from the database and store it in an array list, which i want to be displayed in a list view.Current issue is that the list view is not showing all data of array list, only the last pill object that was added.
Code for the uploading of data:
package com.example.andrew.pilltracker;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.Toolbar;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.auth.*;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class SecondActvty extends AppCompatActivity {
static String ref = "Advil";
private FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRootRef;
int id = 1;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
Intent intent = new Intent(SecondActvty.this, MainActivityController.class);
startActivity(intent);
return true;
case R.id.navigation_dashboard:
return true;
case R.id.navigation_notifications:
Intent intent1 = new Intent(SecondActvty.this, ThirdActivity.class);
startActivity(intent1);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_view);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRootRef = mFirebaseDatabase.getReference();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//id++;
EditText pillName = (EditText)findViewById(R.id.pillName);
EditText pillDose = (EditText)findViewById(R.id.pillDose);
EditText pillCount = (EditText)findViewById(R.id.pillCount);
EditText doseDuration = (EditText)findViewById(R.id.doseDuration);
String name = pillName.getText().toString();
String dose = pillDose.getText().toString();
String day = doseDuration.getText().toString();
String count = pillCount.getText().toString();
ref = name;
Pill pill = new Pill();
pill.setName(name);
pill.setCount(count);
pill.setDay(day);
pill.setDose(dose);
if(!name.equals("") && !dose.equals("") && !day.equals("") && !count.equals("")) {
mRootRef.child("pills").child(name).setValue(pill);
toastMessage("Success!");
pillName.setText("");
pillCount.setText("");
doseDuration.setText("");
pillDose.setText("");
}
else {
toastMessage("Please fill all fields");
}
}
});
}
private void toastMessage(String message){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
public static String getReference(){
return ref;
}
}
And the class that will download the data from firebase and show it in the listview:
package com.example.andrew.pilltracker;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class MainActivityController extends AppCompatActivity {
private TextView mTextMessage;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mRootRef;
private ArrayList<String> array;
private ListView mListView;
private ArrayAdapter adapter;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
Intent intent = new Intent(MainActivityController.this, SecondActvty.class);
startActivity(intent);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_view);
mListView = (ListView) findViewById(R.id.listview);
array = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRootRef = mFirebaseDatabase.getReference();
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
mRootRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
showData(dataSnapshot);
mListView.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void showData(DataSnapshot dataSnapshot){
for(DataSnapshot ds : dataSnapshot.getChildren()){
Pill pInfo = new Pill();
pInfo.setName(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getName());
pInfo.setCount(ds.child(SecondActvty.getReference().toString()/*Integer.toString(databaseRef)*/).getValue(Pill.class).getCount());
pInfo.setDose(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDose());
pInfo.setDay(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDay());
array.add(pInfo.getName());
array.add("Number of Pills Left: " + pInfo.getCount());
array.add("Number of Pills per Dose: " + pInfo.getDose());
array.add("Number of Doses per Day: " + pInfo.getDay());
}
}
//Database shit
@Override
protected void onStart(){
super.onStart();
}
}
Database structure:
Database
java firebase
So I have an app that will let you create a java object containing information about a pill you take (Name, dose amount, number of doses per day, and number of pills left). I then store this information of firebase real-time database, then I use a separate activity to get the information from the database and store it in an array list, which i want to be displayed in a list view.Current issue is that the list view is not showing all data of array list, only the last pill object that was added.
Code for the uploading of data:
package com.example.andrew.pilltracker;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.Toolbar;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.auth.*;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class SecondActvty extends AppCompatActivity {
static String ref = "Advil";
private FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRootRef;
int id = 1;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
Intent intent = new Intent(SecondActvty.this, MainActivityController.class);
startActivity(intent);
return true;
case R.id.navigation_dashboard:
return true;
case R.id.navigation_notifications:
Intent intent1 = new Intent(SecondActvty.this, ThirdActivity.class);
startActivity(intent1);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_view);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRootRef = mFirebaseDatabase.getReference();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//id++;
EditText pillName = (EditText)findViewById(R.id.pillName);
EditText pillDose = (EditText)findViewById(R.id.pillDose);
EditText pillCount = (EditText)findViewById(R.id.pillCount);
EditText doseDuration = (EditText)findViewById(R.id.doseDuration);
String name = pillName.getText().toString();
String dose = pillDose.getText().toString();
String day = doseDuration.getText().toString();
String count = pillCount.getText().toString();
ref = name;
Pill pill = new Pill();
pill.setName(name);
pill.setCount(count);
pill.setDay(day);
pill.setDose(dose);
if(!name.equals("") && !dose.equals("") && !day.equals("") && !count.equals("")) {
mRootRef.child("pills").child(name).setValue(pill);
toastMessage("Success!");
pillName.setText("");
pillCount.setText("");
doseDuration.setText("");
pillDose.setText("");
}
else {
toastMessage("Please fill all fields");
}
}
});
}
private void toastMessage(String message){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
public static String getReference(){
return ref;
}
}
And the class that will download the data from firebase and show it in the listview:
package com.example.andrew.pilltracker;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class MainActivityController extends AppCompatActivity {
private TextView mTextMessage;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mRootRef;
private ArrayList<String> array;
private ListView mListView;
private ArrayAdapter adapter;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
Intent intent = new Intent(MainActivityController.this, SecondActvty.class);
startActivity(intent);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_view);
mListView = (ListView) findViewById(R.id.listview);
array = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRootRef = mFirebaseDatabase.getReference();
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
mRootRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
showData(dataSnapshot);
mListView.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void showData(DataSnapshot dataSnapshot){
for(DataSnapshot ds : dataSnapshot.getChildren()){
Pill pInfo = new Pill();
pInfo.setName(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getName());
pInfo.setCount(ds.child(SecondActvty.getReference().toString()/*Integer.toString(databaseRef)*/).getValue(Pill.class).getCount());
pInfo.setDose(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDose());
pInfo.setDay(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDay());
array.add(pInfo.getName());
array.add("Number of Pills Left: " + pInfo.getCount());
array.add("Number of Pills per Dose: " + pInfo.getDose());
array.add("Number of Doses per Day: " + pInfo.getDay());
}
}
//Database shit
@Override
protected void onStart(){
super.onStart();
}
}
Database structure:
Database
java firebase
java firebase
edited Nov 27 '18 at 5:42
Prateek Bhardwaj
626
626
asked Nov 27 '18 at 2:04
Andrew HunterAndrew Hunter
12
12
1
Please attach your database structure with example data stored in it and remove the first instance of code.
– PradyumanDixit
Nov 27 '18 at 3:34
@PradyumanDixit I have attached a picture of the database at the bottom of the post. I don't know what you mean by remove the first instance of code. Thanks!
– Andrew Hunter
Nov 27 '18 at 4:49
I mean remove the first code you've put for setting the values in database, if there is no problem with that. Also don't put your firebase URL in images, it can be misused.
– PradyumanDixit
Nov 27 '18 at 4:54
Hey @AndrewHunter, do mark the answer as correct by clicking tick mark type V shaped button next to the answer, it should turn green. This helps future readers of the question and I'd appreciate that too. Cheers! :)
– PradyumanDixit
Dec 3 '18 at 5:55
add a comment |
1
Please attach your database structure with example data stored in it and remove the first instance of code.
– PradyumanDixit
Nov 27 '18 at 3:34
@PradyumanDixit I have attached a picture of the database at the bottom of the post. I don't know what you mean by remove the first instance of code. Thanks!
– Andrew Hunter
Nov 27 '18 at 4:49
I mean remove the first code you've put for setting the values in database, if there is no problem with that. Also don't put your firebase URL in images, it can be misused.
– PradyumanDixit
Nov 27 '18 at 4:54
Hey @AndrewHunter, do mark the answer as correct by clicking tick mark type V shaped button next to the answer, it should turn green. This helps future readers of the question and I'd appreciate that too. Cheers! :)
– PradyumanDixit
Dec 3 '18 at 5:55
1
1
Please attach your database structure with example data stored in it and remove the first instance of code.
– PradyumanDixit
Nov 27 '18 at 3:34
Please attach your database structure with example data stored in it and remove the first instance of code.
– PradyumanDixit
Nov 27 '18 at 3:34
@PradyumanDixit I have attached a picture of the database at the bottom of the post. I don't know what you mean by remove the first instance of code. Thanks!
– Andrew Hunter
Nov 27 '18 at 4:49
@PradyumanDixit I have attached a picture of the database at the bottom of the post. I don't know what you mean by remove the first instance of code. Thanks!
– Andrew Hunter
Nov 27 '18 at 4:49
I mean remove the first code you've put for setting the values in database, if there is no problem with that. Also don't put your firebase URL in images, it can be misused.
– PradyumanDixit
Nov 27 '18 at 4:54
I mean remove the first code you've put for setting the values in database, if there is no problem with that. Also don't put your firebase URL in images, it can be misused.
– PradyumanDixit
Nov 27 '18 at 4:54
Hey @AndrewHunter, do mark the answer as correct by clicking tick mark type V shaped button next to the answer, it should turn green. This helps future readers of the question and I'd appreciate that too. Cheers! :)
– PradyumanDixit
Dec 3 '18 at 5:55
Hey @AndrewHunter, do mark the answer as correct by clicking tick mark type V shaped button next to the answer, it should turn green. This helps future readers of the question and I'd appreciate that too. Cheers! :)
– PradyumanDixit
Dec 3 '18 at 5:55
add a comment |
1 Answer
1
active
oldest
votes
I think the problem is with setting up the adapter. You've given the array in the adapter before adding the actual values and you're getting it done inside the onDataChange() method.
That's why only the last pill data is getting displayed. To solve the problem please use the following lines of code:
mRootRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
Pill pInfo = new Pill();
pInfo.setName(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getName());
pInfo.setCount(ds.child(SecondActvty.getReference().toString()/*Integer.toString(databaseRef)*/).getValue(Pill.class).getCount());
pInfo.setDose(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDose());
pInfo.setDay(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDay());
array.add(pInfo.getName());
array.add("Number of Pills Left: " + pInfo.getCount());
array.add("Number of Pills per Dose: " + pInfo.getDose());
array.add("Number of Doses per Day: " + pInfo.getDay());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
} // don't ignore errors, do something for this too
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
mListView.setAdapter(adapter);
});
Hey, so now my listview isn't showing anything
– Andrew Hunter
Nov 27 '18 at 14:00
Here it is with your changes impleminted pastebin.com/67awg4LK
– Andrew Hunter
Nov 27 '18 at 14:00
I'm getting an error on the adapter declaration
– Andrew Hunter
Nov 27 '18 at 14:12
What is the error you're getting?
– PradyumanDixit
Nov 27 '18 at 16:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491727%2fdisplaying-arraylist-on-listview-trouble%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think the problem is with setting up the adapter. You've given the array in the adapter before adding the actual values and you're getting it done inside the onDataChange() method.
That's why only the last pill data is getting displayed. To solve the problem please use the following lines of code:
mRootRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
Pill pInfo = new Pill();
pInfo.setName(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getName());
pInfo.setCount(ds.child(SecondActvty.getReference().toString()/*Integer.toString(databaseRef)*/).getValue(Pill.class).getCount());
pInfo.setDose(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDose());
pInfo.setDay(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDay());
array.add(pInfo.getName());
array.add("Number of Pills Left: " + pInfo.getCount());
array.add("Number of Pills per Dose: " + pInfo.getDose());
array.add("Number of Doses per Day: " + pInfo.getDay());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
} // don't ignore errors, do something for this too
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
mListView.setAdapter(adapter);
});
Hey, so now my listview isn't showing anything
– Andrew Hunter
Nov 27 '18 at 14:00
Here it is with your changes impleminted pastebin.com/67awg4LK
– Andrew Hunter
Nov 27 '18 at 14:00
I'm getting an error on the adapter declaration
– Andrew Hunter
Nov 27 '18 at 14:12
What is the error you're getting?
– PradyumanDixit
Nov 27 '18 at 16:18
add a comment |
I think the problem is with setting up the adapter. You've given the array in the adapter before adding the actual values and you're getting it done inside the onDataChange() method.
That's why only the last pill data is getting displayed. To solve the problem please use the following lines of code:
mRootRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
Pill pInfo = new Pill();
pInfo.setName(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getName());
pInfo.setCount(ds.child(SecondActvty.getReference().toString()/*Integer.toString(databaseRef)*/).getValue(Pill.class).getCount());
pInfo.setDose(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDose());
pInfo.setDay(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDay());
array.add(pInfo.getName());
array.add("Number of Pills Left: " + pInfo.getCount());
array.add("Number of Pills per Dose: " + pInfo.getDose());
array.add("Number of Doses per Day: " + pInfo.getDay());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
} // don't ignore errors, do something for this too
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
mListView.setAdapter(adapter);
});
Hey, so now my listview isn't showing anything
– Andrew Hunter
Nov 27 '18 at 14:00
Here it is with your changes impleminted pastebin.com/67awg4LK
– Andrew Hunter
Nov 27 '18 at 14:00
I'm getting an error on the adapter declaration
– Andrew Hunter
Nov 27 '18 at 14:12
What is the error you're getting?
– PradyumanDixit
Nov 27 '18 at 16:18
add a comment |
I think the problem is with setting up the adapter. You've given the array in the adapter before adding the actual values and you're getting it done inside the onDataChange() method.
That's why only the last pill data is getting displayed. To solve the problem please use the following lines of code:
mRootRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
Pill pInfo = new Pill();
pInfo.setName(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getName());
pInfo.setCount(ds.child(SecondActvty.getReference().toString()/*Integer.toString(databaseRef)*/).getValue(Pill.class).getCount());
pInfo.setDose(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDose());
pInfo.setDay(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDay());
array.add(pInfo.getName());
array.add("Number of Pills Left: " + pInfo.getCount());
array.add("Number of Pills per Dose: " + pInfo.getDose());
array.add("Number of Doses per Day: " + pInfo.getDay());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
} // don't ignore errors, do something for this too
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
mListView.setAdapter(adapter);
});
I think the problem is with setting up the adapter. You've given the array in the adapter before adding the actual values and you're getting it done inside the onDataChange() method.
That's why only the last pill data is getting displayed. To solve the problem please use the following lines of code:
mRootRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
Pill pInfo = new Pill();
pInfo.setName(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getName());
pInfo.setCount(ds.child(SecondActvty.getReference().toString()/*Integer.toString(databaseRef)*/).getValue(Pill.class).getCount());
pInfo.setDose(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDose());
pInfo.setDay(ds.child(SecondActvty.getReference().toString()).getValue(Pill.class).getDay());
array.add(pInfo.getName());
array.add("Number of Pills Left: " + pInfo.getCount());
array.add("Number of Pills per Dose: " + pInfo.getDose());
array.add("Number of Doses per Day: " + pInfo.getDay());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
} // don't ignore errors, do something for this too
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
mListView.setAdapter(adapter);
});
answered Nov 27 '18 at 5:00
PradyumanDixitPradyumanDixit
2,1772819
2,1772819
Hey, so now my listview isn't showing anything
– Andrew Hunter
Nov 27 '18 at 14:00
Here it is with your changes impleminted pastebin.com/67awg4LK
– Andrew Hunter
Nov 27 '18 at 14:00
I'm getting an error on the adapter declaration
– Andrew Hunter
Nov 27 '18 at 14:12
What is the error you're getting?
– PradyumanDixit
Nov 27 '18 at 16:18
add a comment |
Hey, so now my listview isn't showing anything
– Andrew Hunter
Nov 27 '18 at 14:00
Here it is with your changes impleminted pastebin.com/67awg4LK
– Andrew Hunter
Nov 27 '18 at 14:00
I'm getting an error on the adapter declaration
– Andrew Hunter
Nov 27 '18 at 14:12
What is the error you're getting?
– PradyumanDixit
Nov 27 '18 at 16:18
Hey, so now my listview isn't showing anything
– Andrew Hunter
Nov 27 '18 at 14:00
Hey, so now my listview isn't showing anything
– Andrew Hunter
Nov 27 '18 at 14:00
Here it is with your changes impleminted pastebin.com/67awg4LK
– Andrew Hunter
Nov 27 '18 at 14:00
Here it is with your changes impleminted pastebin.com/67awg4LK
– Andrew Hunter
Nov 27 '18 at 14:00
I'm getting an error on the adapter declaration
– Andrew Hunter
Nov 27 '18 at 14:12
I'm getting an error on the adapter declaration
– Andrew Hunter
Nov 27 '18 at 14:12
What is the error you're getting?
– PradyumanDixit
Nov 27 '18 at 16:18
What is the error you're getting?
– PradyumanDixit
Nov 27 '18 at 16:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491727%2fdisplaying-arraylist-on-listview-trouble%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please attach your database structure with example data stored in it and remove the first instance of code.
– PradyumanDixit
Nov 27 '18 at 3:34
@PradyumanDixit I have attached a picture of the database at the bottom of the post. I don't know what you mean by remove the first instance of code. Thanks!
– Andrew Hunter
Nov 27 '18 at 4:49
I mean remove the first code you've put for setting the values in database, if there is no problem with that. Also don't put your firebase URL in images, it can be misused.
– PradyumanDixit
Nov 27 '18 at 4:54
Hey @AndrewHunter, do mark the answer as correct by clicking tick mark type V shaped button next to the answer, it should turn green. This helps future readers of the question and I'd appreciate that too. Cheers! :)
– PradyumanDixit
Dec 3 '18 at 5:55