Unable to retrieve information from Firebase to Custom ListView Android

时间:2019-04-23 15:09:35

标签: java android firebase firebase-realtime-database

I am new to Android Studio. I am trying to develop a inventory management app but unable to show the information in a custom ListView from Firebase database. I have found similar questions and solution here but did not work for me. Below is the code of AccountHome(MainActivity). This code works sometimes when I close and restart the app multiple times. Hope to get some working solution. Thanks Screenshot of Firebase Database

public class AccountHom extends AppCompatActivity {

    ListView listview;
    FirebaseDatabase database;
    DatabaseReference myRef;
    ArrayList<String> dept ;
    ArrayList<Long> total;
    AccountSetBudgetHelper accountSetBudgetHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_home_activity);

        accountSetBudgetHelper = new AccountSetBudgetHelper();
        listview = findViewById(R.id.set_budget_listview);
        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("budget");
        dept = new ArrayList<>();
        total = new ArrayList<Long>();

        AccountSetBudgetAdaptr accountSetBudgetAdapter = new AccountSetBudgetAdaptr(AccountHom.this ,dept,total);

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    accountSetBudgetHelper = ds.getValue(AccountSetBudgetHelper.class);
                    dept.add(accountSetBudgetHelper.getName());
                    total.add(accountSetBudgetHelper.getTotal());
                } }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
        listview.setAdapter(accountSetBudgetAdapter);
    }
}
class AccountSetBudgetAdaptr extends ArrayAdapter {
    private final Activity context;
    private final ArrayList<String> dept;
    private final ArrayList<Long> total;

    AccountSetBudgetAdaptr(Activity context, ArrayList<String> dept, ArrayList<Long> total) {
        super(context, R.layout.single_row_listview_budget,dept);
        this.context = context;
        this.dept = dept;
        this.total = total;
    }
    @NonNull
    @Override
    public View getView(int position, View view, @NonNull ViewGroup parent) {

        Toast.makeText(getContext(), "this is a boy", Toast.LENGTH_SHORT).show();
        View rowView = view;
        if(rowView==null){
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.single_row_listview_budget, parent, false);
        }
        TextView mdept = rowView.findViewById(R.id.textView_setbudget_dept);
        TextView mtotal = rowView.findViewById(R.id.textView_setbudget_total);
        mdept.setText(dept.get(position));
        mtotal.setText(String.valueOf(total.get(position)));
        return rowView;
    }
}
class AccountSetBudgetHelpr {
    private String name;
    private Long total;
    public AccountSetBudgetHelpr() {
    }
    public AccountSetBudgetHelpr(String name, Long total) {
        this.name = name;
        this.total = total;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Long getTotal() {
        return total;
    }
    public void setTotal(Long total) {
        this.total = total;
    }
}[enter image description here][1]

1 个答案:

答案 0 :(得分:1)

You are adding data to both lists, dept and total but you don't notify the adapter about the new data. So to solve this, please add the following line of code, right after the for loop ends:

myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot ds : dataSnapshot.getChildren()) {
            accountSetBudgetHelper = ds.getValue(AccountSetBudgetHelper.class);
            dept.add(accountSetBudgetHelper.getName());
            total.add(accountSetBudgetHelper.getTotal());
        }
        accountSetBudgetAdapter.notifyDataSetChanged(); //Notify the adapter
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {}
});