我想添加一个"删除按钮"在自定义列表视图中删除firebase中的数据以及firebase存储中的图像

时间:2017-12-12 09:46:04

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

我是Android开发的新手。这是我的firebase数据库的图片。

enter image description here

我在自定义列表视图中显示我的Android应用程序中的电影名称和图像。

这是我的ModelClass

public class ModelClass {

String name,imagelink;


public ModelClass() {

}

public ModelClass(String name, String imagelink) {
    this.name = name;
    this.imagelink = imagelink;

}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getImagelink() {
    return imagelink;
}

public void setImagelink(String imagelink) {
    this.imagelink = imagelink;
}

}

这是我的适配器类

import android.app.Activity;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.List;



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



private DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("MovieDetails");


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.movie_name_view);
    ImageView img = (ImageView) v.findViewById(R.id.movie_image_view);


    tvName.setText(listImage.get(position).getName());
    Glide.with(context).load(listImage.get(position).getImagelink()).into(img);



    return v;

}

public void addElement(ModelClass element) {

    listImage.add(element);
}

}

这是我的活动

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.google.firebase.database.ChildEventListener;
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;
import java.util.List;

public class CheckMovieInfoActivity 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_check_movie_info);

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

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

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

    myAdapter = new MyAdapter(CheckMovieInfoActivity.this, R.layout.customlistview, 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) {

            progressDialog.dismiss();
        }
    });
}

}

这是我的自定义列表视图XML

<?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="match_parent">

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

        <ImageView
            android:id="@+id/movie_image_view"
            android:layout_width="140dp"
            android:layout_height="140dp"
            android:src="@drawable/upload_image_small"/>

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

            <TextView
                android:id="@+id/movie_name_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MOVIE NAME"
                android:layout_gravity="center"
                android:textSize="20dp"/>

        </LinearLayout>

    </LinearLayout>

<Button
    android:layout_marginTop="8dp"
    android:id="@+id/btndel"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Delete Movie"
    android:background="@drawable/roundedbutton"/>


</LinearLayout>

我想在自定义列表视图中添加删除按钮,以删除数据以及数据库中的图像。

我没有收到任何错误我只想添加删除按钮来删除数据。提前谢谢。

0 个答案:

没有答案