在对话框中选择图像选项

我正在使用Java和Firebase开发一个android应用程序。我想编辑和更新存储在Firebase数据库中的数据。为此,我正在使用一个对话框,可以在其中检索数据并进行更新。

这是我的编码。

适配器类。

public class AdminAdapter extends RecyclerView.Adapter<AdminAdapter.AdminHolder> {

    UpdateInterface updateInterface;
    String noteId="",noteDate="";

    private Context mContext;
    private List<Upload> mUploads;

    public AdminAdapter(Context context,List<Upload> uploads){
        mContext = context;
        mUploads = uploads;
        updateInterface = (UpdateInterface)mContext;
    }

    @NonNull
    @Override
    public AdminHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.admin_card_view,parent,false);
        return new AdminAdapter.AdminHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull AdminHolder holder, final int position) {
        final Upload uploadCurrent = mUploads.get(position);
        holder.admTitle.setText(uploadCurrent.getTitle());
        holder.admDesc.setText(uploadCurrent.getDescription());
        holder.admDate.setText(uploadCurrent.getDate());
        holder.admTime.setText(uploadCurrent.getTime());
        holder.admSentDate.setText(uploadCurrent.getSentDate());
        Picasso.get()
                .load(uploadCurrent.getImage())
                .fit()
                .centerInside()
                .into(holder.admImage);

        holder.admEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Upload upload = mUploads.get(position);
                showDialog(upload);
            }
        });


        holder.admDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Upload upload = mUploads.get(position);
                confirmDeleteDialog(upload);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mUploads.size();
    }

    public class AdminHolder extends RecyclerView.ViewHolder{
        public TextView admTitle,admDesc,admDate,admTime,admSentDate;
        public ImageView admImage;
        public ImageView admEdit,admDelete;

        public AdminHolder(@NonNull View itemView) {
            super(itemView);
            admTitle = itemView.findViewById(R.id.admTitle);
            admDesc = itemView.findViewById(R.id.admDesc);
            admDate = itemView.findViewById(R.id.admDate);
            admTime = itemView.findViewById(R.id.admTime);
            admSentDate = itemView.findViewById(R.id.admSentTime);
            admImage = itemView.findViewById(R.id.admImage);
            admEdit = itemView.findViewById(R.id.admEditBtn);
            admDelete = itemView.findViewById(R.id.admDeleteBtn);
        }
    }

    public void showDialog(Upload uploadOb){

        String imageUrl = null;

        final Dialog dialog = new Dialog((mContext));
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_edit);
        dialog.show();
        Window window = dialog.getWindow();
        window.setLayout(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT );

        final EditText editTitle = dialog.findViewById(R.id.admEditTitle);
        final EditText editDesc = dialog.findViewById(R.id.admEditDescription);
        final EditText editDate = dialog.findViewById(R.id.admEditDate);
        final EditText editTime = dialog.findViewById(R.id.admEditTime);
        final ImageView editImage = dialog.findViewById(R.id.admEditImage);

        editTitle.setText(uploadOb.getTitle());
        editDesc.setText(uploadOb.getDescription());
        editDate.setText(uploadOb.getDate());
        editTime.setText(uploadOb.getTime());
        imageUrl = uploadOb.getImage();
        Picasso.get().load(imageUrl).into(editImage);

        editImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent chooseImage = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            }
        });


        noteId=uploadOb.getMsgKey();

        Button buttonUpdate = dialog.findViewById(R.id.admEditDoneBtn);
        final String finalImageUrl = imageUrl;
        buttonUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMM yyyy");
                Calendar calendar = Calendar.getInstance();
                noteDate = simpleDateFormat.format(calendar.getTime());

                String title = editTitle.getText().toString();
                String description = editDesc.getText().toString();
                String date = editDate.getText().toString();
                String time = editTime.getText().toString();

                Upload upload = new Upload(title,description,date,time, finalImageUrl,noteDate,noteId);
                updateInterface.UpdateEvents(upload);
            }
        });
    }


    public void confirmDeleteDialog(final Upload upload){
        final Dialog dialog = new Dialog((mContext));
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_delete);
        dialog.show();

        TextView delete = dialog.findViewById(R.id.admDelete);
        TextView cancel = dialog.findViewById(R.id.admCancel);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateInterface.DeleteEvents(upload);
                dialog.dismiss();
            }
        });
    }
}

接口


public interface UpdateInterface {

    public void UpdateEvents(Upload upload);

    public void DeleteEvents(Upload upload);
}

模型类。

public class Upload {
    private String mTitle;
    private String mDescription;
    private String mDate;
    private String mTime;
    private String imageUrl;
    private String sentDate;
    private String msgKey;

    public Upload() {

    }

    public Upload(String title,String desc,String date,String time,String image, String sDate,String mKey){
        mTitle = title;
        mDescription = desc;
        mDate = date;
        mTime = time;
        imageUrl = image;
        sentDate = sDate;
        msgKey = mKey;
    }

    public String getTitle(){
        return mTitle;
    }

    public void setTitle(String title){
        mTitle = title;
    }

    public String getDescription(){
        return mDescription;
    }

    public void setDescription(String desc){
        mDescription = desc;
    }

    public String getDate(){
        return mDate;
    }

    public void setDate(String date){
        mDate = date;
    }

    public String getTime(){
        return mTime;
    }

    public void setTime(String time){
        mTime = time;
    }

    public String getImage(){
        return imageUrl;
    }

    public void setImage(String image){
        imageUrl = image;
    }

    public String getSentDate() {
        return sentDate;
    }

    public void setSentDate(String sDate) {
        this.sentDate = sDate;
    }

    public String getMsgKey() {
        return msgKey;
    }

    public void setMsgKey(String msgKey) {
        this.msgKey = msgKey;
    }
}

主要活动。

public class MainActivityAdminEdit extends AppCompatActivity implements UpdateInterface{
    private RecyclerView mRecyclerView;
    private AdminAdapter mAdapter;

    private ProgressBar mProgressCircle;

    private DatabaseReference mDatabaseRef;
    private ArrayList<Upload> mUploads = new ArrayList<>();;

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

        assert getSupportActionBar() != null;
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mRecyclerView = findViewById(R.id.admRecyclerView);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mProgressCircle = findViewById(R.id.admProgressCircle);

        mDatabaseRef = FirebaseDatabase.getInstance().getReference("MESSAGES");
        mDatabaseRef.keepSynced(true);
        read();
    }


    private void read(){
        mDatabaseRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mUploads.clear();
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    Upload upload = postSnapshot.getValue(Upload.class);
                    mUploads.add(upload);
                }
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivityAdminEdit.this);
                linearLayoutManager.setReverseLayout(true);
                linearLayoutManager.setStackFromEnd(true);
                mRecyclerView.setLayoutManager(linearLayoutManager);
                mAdapter = new AdminAdapter(MainActivityAdminEdit.this,mUploads);
                mRecyclerView.setAdapter(mAdapter);
                mProgressCircle.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(MainActivityAdminEdit.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
                mProgressCircle.setVisibility(View.INVISIBLE);
            }
        });

    }


    @Override
    public void UpdateEvents(Upload upload) {
        mDatabaseRef.child(upload.getMsgKey()).setValue(upload).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()){
                    Toast.makeText(MainActivityAdminEdit.this, "Update Successful", Toast.LENGTH_SHORT).show();
                    read();
                } else {
                    Toast.makeText(MainActivityAdminEdit.this, "Error", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void DeleteEvents(Upload upload) {
        mDatabaseRef.child(upload.getMsgKey()).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()){
                    Toast.makeText(MainActivityAdminEdit.this, "Successfully deleted", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivityAdminEdit.this, "Error", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

对话框XML。

<androidx.constraintlayout.widget.ConstraintLayout 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:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:gravity="center">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/editTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/admEditTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/edit_title"
            android:inputType="textCapSentences"
            android:lines="2"
            android:maxLines="2"/>
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/editDescription"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginTop="20dp"
        android:hint="@string/edit_description"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTitle">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/admEditDescription"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine"
            android:lines="8"
            android:minLines="6"
            android:gravity="top|start"
            android:scrollbars="vertical"/>
    </com.google.android.material.textfield.TextInputLayout>

    <EditText
        android:id="@+id/admEditDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="@string/edit_date"
        android:ems="10"
        android:inputType="date"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editDescription"
        android:importantForAutofill="no" />

    <EditText
        android:id="@+id/admEditTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="@string/edit_time"
        android:ems="10"
        android:inputType="datetime"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/admEditDate"
        android:importantForAutofill="no" />

    <ImageView
        android:id="@+id/admEditImage"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/ic_image_black_24dp"
        android:layout_marginTop="20dp"
        android:outlineProvider="background"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/admEditTime"
        tools:ignore="ContentDescription" />

    <Button
        android:id="@+id/admEditDoneBtn"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_marginBottom="50dp"
        android:background="@drawable/sample"
        android:text="@string/done_btn"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

我在Dialog的ImageView中添加了onClickListener。当我单击它时,它希望从图库中选择图像并显示它,并询问权限。但这不起作用。如果您知道任何解决方案,将非常有帮助。

评论