我有一个问题,我不知道哪个是错误,当我按下添加照片按钮或发送按钮根本没有发生时我没有收到任何错误, 第一个按钮应该打开我的照片,第二个按钮应该发送到一个碎片的RecyclerView我给我的代码
对不起我的英语使用谷歌翻译
片段
public class PostCreateDialog extends DialogFragment implements View.OnClickListener{
private static final int RC_PHOTO_PICKER = 1;
private Post mPost;
private ProgressDialog mProgressDialog;
private ImageView mPostImageView;
private View mRootView;
private Uri mSelectedUri;
private ImageButton sendPostButton;
private ImageButton selectFotoButton;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
mPost = new Post();
mProgressDialog = new ProgressDialog(getContext());
mRootView = getActivity().getLayoutInflater().inflate(R.layout.create_post_dialog, null);
mPostImageView = (ImageView) mRootView.findViewById(R.id.post_dialog_display);
selectFotoButton = (ImageButton) mRootView.findViewById(R.id.post_dialog_select_imageview);
sendPostButton = (ImageButton) mRootView.findViewById(R.id.post_dialog_send_imageview);
builder.setView(mRootView);
return builder.create();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.post_dialog_select_imageview:
selectImage();
break;
case R.id.post_dialog_send_imageview:
sendPost();
break;
}
}
private void sendPost() {
mProgressDialog.setMessage("Sending post...");
mProgressDialog.setCancelable(false);
mProgressDialog.setIndeterminate(true);
mProgressDialog.show();
FirebaseUtils.getUserRef(FirebaseUtils.getCurrentUser().getEmail().replace(".",",")).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
final String postId = FirebaseUtils.getUid();
TextView postDialogText = (TextView) mRootView.findViewById(R.id.post_dialog_edittext);
String text = postDialogText.getText().toString();
mPost.setUser(user);
mPost.setNumOfLikes(0);
mPost.setNumOfComments(0);
mPost.setNumOfUnlikes(0);
mPost.setTimeOfcreation(System.currentTimeMillis());
mPost.setPostText(text);
if(mSelectedUri != null){
FirebaseUtils.getImageRef().child(mSelectedUri.getLastPathSegment()).putFile(mSelectedUri).addOnSuccessListener(getActivity(), new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String url = Contants.POST_IMAGES + "/" + mSelectedUri.getLastPathSegment();
mPost.setImageUrl(url);
addToMyPostList(postId);
}
});
}else {
addToMyPostList(postId);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
mProgressDialog.dismiss();
}
});
}
// ESTE DE AQUI ES COMO UN FINAL A TOODO SI SE COMPLETA O SI CANCELA
private void addToMyPostList(String postId){
FirebaseUtils.getPostRef().child(postId).setValue(mPost);
FirebaseUtils.getPostRef().child(postId).setValue(true).addOnCompleteListener(getActivity(), new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
mProgressDialog.dismiss();
dismiss();
}
});
}
// ESTOS DOS METODOS SIRVEN PARA SELECCIONAR LA IMAGEN
private void selectImage() {
Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT);
intent1.setType("image/jpeg");
intent1.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent1, "Comlete action using"), RC_PHOTO_PICKER);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RC_PHOTO_PICKER){
if(resultCode == RESULT_OK){
mSelectedUri = data.getData();
mPostImageView.setImageURI(mSelectedUri);
}
}
}
}
对话框:
public class PostCreateDialog extends DialogFragment implements View.OnClickListener{
private static final int RC_PHOTO_PICKER = 1;
private Post mPost;
private ProgressDialog mProgressDialog;
private ImageView mPostImageView;
private View mRootView;
private Uri mSelectedUri;
private ImageButton sendPostButton;
private ImageButton selectFotoButton;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
mPost = new Post();
mProgressDialog = new ProgressDialog(getContext());
mRootView = getActivity().getLayoutInflater().inflate(R.layout.create_post_dialog, null);
mPostImageView = (ImageView) mRootView.findViewById(R.id.post_dialog_display);
selectFotoButton = (ImageButton) mRootView.findViewById(R.id.post_dialog_select_imageview);
sendPostButton = (ImageButton) mRootView.findViewById(R.id.post_dialog_send_imageview);
builder.setView(mRootView);
return builder.create();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.post_dialog_select_imageview:
selectImage();
break;
case R.id.post_dialog_send_imageview:
sendPost();
break;
}
}
private void sendPost() {
mProgressDialog.setMessage("Sending post...");
mProgressDialog.setCancelable(false);
mProgressDialog.setIndeterminate(true);
mProgressDialog.show();
FirebaseUtils.getUserRef(FirebaseUtils.getCurrentUser().getEmail().replace(".",",")).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
final String postId = FirebaseUtils.getUid();
TextView postDialogText = (TextView) mRootView.findViewById(R.id.post_dialog_edittext);
String text = postDialogText.getText().toString();
mPost.setUser(user);
mPost.setNumOfLikes(0);
mPost.setNumOfComments(0);
mPost.setNumOfUnlikes(0);
mPost.setTimeOfcreation(System.currentTimeMillis());
mPost.setPostText(text);
if(mSelectedUri != null){
FirebaseUtils.getImageRef().child(mSelectedUri.getLastPathSegment()).putFile(mSelectedUri).addOnSuccessListener(getActivity(), new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String url = Contants.POST_IMAGES + "/" + mSelectedUri.getLastPathSegment();
mPost.setImageUrl(url);
addToMyPostList(postId);
}
});
}else {
addToMyPostList(postId);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
mProgressDialog.dismiss();
}
});
}
// ESTE DE AQUI ES COMO UN FINAL A TOODO SI SE COMPLETA O SI CANCELA
private void addToMyPostList(String postId){
FirebaseUtils.getPostRef().child(postId).setValue(mPost);
FirebaseUtils.getPostRef().child(postId).setValue(true).addOnCompleteListener(getActivity(), new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
mProgressDialog.dismiss();
dismiss();
}
});
}
// ESTOS DOS METODOS SIRVEN PARA SELECCIONAR LA IMAGEN
private void selectImage() {
Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT);
intent1.setType("image/jpeg");
intent1.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent1, "Comlete action using"), RC_PHOTO_PICKER);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RC_PHOTO_PICKER){
if(resultCode == RESULT_OK){
mSelectedUri = data.getData();
mPostImageView.setImageURI(mSelectedUri);
}
}
}
}
答案 0 :(得分:1)
尝试在按钮点击时显示任何祝酒词。 如果出现吐司,则您的点击事件正在运行,如果未显示吐司,则您的按钮点击不起作用。