我无法从Firebase数据库中恢复数据

时间:2019-02-09 22:33:24

标签: java android xml firebase firebase-realtime-database

我不明白为什么我的listView不显示Firebase数据库中的数据

这是我的数据库计划。

db

这是我的XML listView:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Voir_Frigo"
    android:background="@drawable/background_menu">

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/title_voir_frigo">

    </ListView>

</RelativeLayout>

这是我的listItem适配器:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/color_text">

    <TextView
        android:id="@+id/nom_alim_linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="30sp"
        android:textColor="@color/black"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/nom_quantite_aliment_linear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/quantite"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/quantite_liste_linear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_toEndOf="@id/nom_quantite_aliment"
            android:textSize="20sp" />


    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/nom_date_aliment_linear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/date_peremption"
            android:textSize="20sp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/date_liste_linear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:layout_marginLeft="5dp"/>


    </LinearLayout>


    </LinearLayout>

我的饮食班:

public class Aliment {

       private String Nom_aliment;
       private String Quantite;
       private String Date;

    public Aliment() {

      }

    public Aliment(String Nom_aliment, String Quantite, String Date) {
      this.Nom_aliment = Nom_aliment;
      this.Quantite = Quantite;
      this.Date = Date;
      }

    public String getNom_aliment(){
      return Nom_aliment;
      }

    public String getQuantite(){
      return Quantite;
      }

    public String getDate(){
      return Date;
      }
    }

我的arrayAdapter:

public class AlimentList extends ArrayAdapter<Aliment> {

    private Activity context;
    private List<Aliment> alimentList;



    public AlimentList(Activity context, List<Aliment> alimentList){
    super(context, R.layout.adapter_linear, alimentList);
    this.context = context;
    this.alimentList = alimentList;


    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View listViewItem = inflater.inflate(R.layout.adapter_linear, null, true);


    TextView tvName = (TextView) listViewItem.findViewById(R.id.nom_alim_linear);
    TextView tvQuantity = (TextView) listViewItem.findViewById(R.id.quantite_liste_linear);
    TextView tvDate = (TextView) listViewItem.findViewById(R.id.date_liste_linear);

    Aliment aliment = alimentList.get(position);

    tvName.setText(aliment.getNom_aliment());
    tvQuantity.setText(aliment.getQuantite());
    tvDate.setText(aliment.getDate());

    return listViewItem;
     }
    }

和我的活动:

public class Voir_Frigo extends AppCompatActivity {

    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
    FirebaseUser user = firebaseAuth.getCurrentUser();



    ListView listViewAliment;

    List<Aliment> alimentList;



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


    listViewAliment = findViewById(R.id.list_view);
    alimentList = new ArrayList<>();
    }

    @Override
    protected void onStart() {
    super.onStart();


    DatabaseReference databaseAliment = FirebaseDatabase.getInstance().getReference(user.getUid());

    databaseAliment.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            alimentList.clear();
            alimentList = new ArrayList<Aliment>();
            for(DataSnapshot alimentSnapshot: dataSnapshot.getChildren()){

                Aliment aliment = alimentSnapshot.getValue(Aliment.class);
                alimentList.add(aliment);
            }
                 AlimentList adapter = new AlimentList(Voir_Frigo.this, alimentList);
                listViewAliment.setAdapter(adapter);
            }

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

        }
      });
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
        startActivity(new Intent(getApplicationContext(),  Menu_principal.class));
        finish();
        }
        return super.onKeyDown(keyCode, event);
      }
    }

我显示了我的项目,但没有显示我的数据库数据。

我想在顶部显示名称,并在数量和有效日期的右侧显示数据库中存在的数据,这是什么地方错了?

https://imgur.com/mgkdTXA

1 个答案:

答案 0 :(得分:0)

您是否尝试检查适配器是否实际绑定了数据(添加了一些日志标签,例如Log.d("aliment data", aliment.getNom_alimanet()))或onDataChange是否实际上获取了正确的数据?