在ListView中显示来自Firebase的TextView和图像

时间:2018-02-21 22:08:22

标签: android listview firebase firebase-realtime-database

这就是我在DataBase中的内容 Click here

我想从数据中显示字段" pet name" (TextView)和" imageRef" (imageView)进入ListView,当我运行此代码时,我没有任何错误,但它仍然没有向我显示文本或数据中的图像(它显示一个空页面)。

有人告诉我我做错了什么?! 谢谢♥

我有一个ModelClass:

public class ModelClass {
    String Nume,imagine;

    public String getNume() {
        return Nume;
    }

    public void setNume(String nume) {
        Nume = nume;
    }

    public String getImagine() {
        return imagine;
    }

    public void setImagine(String imagine) {
        this.imagine = imagine;
    }

    public ModelClass(String nume, String imagine) {
        Nume = nume;
        this.imagine = imagine;
    }
}

我的适配器:

public class MyAdapt extends ArrayAdapter<ModelClass> {
    private Activity context;
    private int resource;
    private List<ModelClass> listImage;

    public MyAdapt(@NonNull Activity context, @LayoutRes int resource, @NonNull List<ModelClass> objects) {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
        listImage = objects;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();

        View v = inflater.inflate(resource, null);
        TextView tvName = (TextView) v.findViewById(R.id.tvPetName);
        ImageView img = (ImageView) v.findViewById(R.id.iv2);

        tvName.setText(listImage.get(position).getNume());
        Glide.with(context).load(listImage.get(position).getImagine()).into(img);


        return v;

    }
    public void addElement(ModelClass element) {

        listImage.add(element);
    }
}

列表视图所在的页面:

public class PetFeed2 extends AppCompatActivity {
    private DatabaseReference mDatabaseRef;
    private List<ModelClass> imgList;
    private ListView lv;
    private MyAdapt myAdapter;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pet_feed2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        imgList = new ArrayList<>();
        lv = (ListView) findViewById(R.id.lv);

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Please wait loading list image...");
        progressDialog.show();

        mDatabaseRef = FirebaseDatabase.getInstance().getReference("Data");

        mDatabaseRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                progressDialog.dismiss();


                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                    ModelClass img = snapshot.getValue(ModelClass.class);
                    imgList.add(img);
                }


                myAdapter = new MyAdapt(PetFeed2.this, R.layout.custumpet, imgList);

                lv.setAdapter(myAdapter);

                mDatabaseRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        progressDialog.dismiss();

                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                            ModelClass img = snapshot.getValue(ModelClass.class);
                            myAdapter.addElement(img);
                        }

                        myAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

问题是您需要阅读字段pet nameimageRef,但ModelClass对象没有与这些字段匹配的变量。因此,您需要在ModelClass中执行以下操作:

public class ModelClass {
    @PropertyName("pet name")
    String Nume;
    @PropertyName("imageRef")
    String imagine;

    //...

您可以将字段pet name与变量Nume匹配,并将其与其他

相匹配

编辑:

现在在您的适配器中更改此:

View v = inflater.inflate(resource, null);

View v = inflater.inflate(resource, parent, false);