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

时间:2017-06-08 17:17:58

标签: android image listview firebase firebase-realtime-database

我的数据库中有enter image description here

我想将所有数据显示到ListView中,我没有错误,但没有向我显示文本和图像。我有一个ModelClass:

public class ModelClass {
String title,image;

public ModelClass(String title,String image) {
    this.image = image;
    this.title = title;
}

public ModelClass() {
}

public String getTitle() {return title;   }
public void setTitle(String title) {
    this.title = title;
}
public String getImage() {
    return image;
}
public void setImage(String image) {
    this.image = image;
}}

适配器:

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

public MyAdapter(@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.txt_desert);
    ImageView img = (ImageView) v.findViewById(R.id.iv_desert);

    tvName.setText(listImage.get(position).getTitle());
    Glide.with(context).load(listImage.get(position).getImage()).into(img);

    return v;

}}

类在哪里是ListView:

public class Deserturi extends AppCompatActivity {

private DatabaseReference mDatabaseRef;
private List<ModelClass> imgList;
private ListView lv;
private MyAdapter myAdapter;
private ProgressDialog progressDialog;

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

   imgList = new ArrayList<>();
   lv = (ListView) findViewById(R.id.blog_list);

   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 MyAdapter(Deserturi.this, R.layout.design_row, imgList);

           lv.setAdapter(myAdapter);
       }

       @Override
       public void onCancelled(DatabaseError databaseError) {

           progressDialog.dismiss();
       }
   });

你能告诉我我做错了吗?!感谢

1 个答案:

答案 0 :(得分:0)

在您的活动中尝试此操作:

public void addElement(ModelClass element) {

    listImage.add(element);
}

在适配器中:

public class ModelClass {
String Nume,imagine;
...

在你的模特中:

$config =  array(
          'image_library'   => 'gd2',
          'source_image'    =>  $data['full_path'],
          'maintain_ratio'  =>  TRUE,
          'width'           =>  250,
          'height'          =>  250,
        );
        $this->image_lib->clear();
        $this->image_lib->initialize($config);
        $this->image_lib->resize();