【问题标题】:Displaying Arraylist on Listview trouble在 Listview 问题中显示 Arraylist
【发布时间】:2019-04-28 17:23:36
【问题描述】:

所以我有一个应用程序,它可以让您创建一个 java 对象,其中包含有关您服用的药丸的信息(名称、剂量、每天的剂量和剩余的药丸数量)。然后我存储这个firebase实时数据库的信息,然后我使用一个单独的活动从数据库中获取信息并将它存储在一个数组列表中,我想在一个列表视图中显示它。当前的问题是列表视图没有显示数组列表的所有数据,只显示最后添加的药丸对象。

数据上传代码:

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;
    }


}

以及将从 firebase 下载数据并将其显示在列表视图中的类:

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

【问题讨论】:

  • 请附上您的数据库结构和其中存储的示例数据,并删除第一个代码实例。
  • @PradyumanDixit 我在帖子底部附上了一张数据库图片。我不知道删除第一个代码实例是什么意思。谢谢!
  • 我的意思是删除您为设置数据库中的值而放置的第一个代码,如果没有问题的话。也不要将您的 Firebase URL 放在图片中,它可能会被滥用。
  • 嘿@AndrewHunter,通过单击答案旁边的勾选标记类型 V 形按钮将答案标记为正确,它应该变成绿色。这有助于问题的未来读者,我也很感激。干杯! :)

标签: java firebase android-studio listview arraylist


【解决方案1】:

我认为问题在于设置适配器。在添加实际值之前,您已经在适配器中提供了数组,并且您在 onDataChange() 方法中完成了它。

这就是为什么只显示最后一个药丸数据的原因。要解决该问题,请使用以下代码行:

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);
    });

【讨论】:

    猜你喜欢
    • 2018-07-12
    • 2017-08-15
    • 1970-01-01
    • 2013-09-30
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 2015-08-19
    相关资源
    最近更新 更多