从Firestore集合中获取单个文档。但clicklistener不能正常工作吗?

时间:2019-04-01 14:08:24

标签: java android firebase event-handling google-cloud-firestore

我正在从 firestore 的集合中获取单个文档。一切正常。数据正常发送,但是问题是我的点击监听器无法正常运行。我什至尝试设置断点,但是我的代码没有到那里。我的点击监听器 eo_target eo_copy_code 点击监听器无法正常工作。

public class DetailsExcitingOffer extends AppCompatActivity {

    private static final String TAG = "ExcitingOffer";


    private static final String EXCITING_KEY_COUPON_CODE = "eo_coupon_code";
    private static final String EXCITING_KEY_DESCRIPTION = "eo_description";
    private static final String EXCITING_KEY_TITLE = "eo_title";
    private static final String EXCITING_KEY_TARGET = "eo_target";
    private static final String EXCITING_DETAILS_IMAGE = "eo_Details_image";

    private TextView eo_title, eo_description, eo_couponCode, eo_target, eo_copyCode;
    private ImageView eo_details_image, eo_failed_img;
    private ProgressBar eo_img_progress;

    private FirebaseFirestore data = FirebaseFirestore.getInstance();

    private DocumentReference eo = data.document("exciting_offer/rsBS1ON04V1N4VIz3fZo");

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

        getSupportActionBar().setHomeAsUpIndicator(R.drawable.navigtion_back);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("Exciting Offer");

        eo_title = (TextView) findViewById(R.id.m_eo_title);
        eo_description = (TextView) findViewById(R.id.m_eo_description);
        eo_couponCode = (TextView) findViewById(R.id.m_eo_coupon_code);
        eo_target = (TextView) findViewById(R.id.m_eo_target);
        eo_details_image = (ImageView) findViewById(R.id.m_eo_details_img);
        eo_failed_img = (ImageView) findViewById (R.id.eo_failed_img);
        eo_img_progress = (ProgressBar) findViewById(R.id.eo_progress);
        eo_copyCode = (TextView) findViewById(R.id.m_eo_copyCode);

        eo.get()
                .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                    @Override
                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                        testing(documentSnapshot);
                    }

                    private void testing(DocumentSnapshot documentSnapshot) {

                    }
                }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

                TastyToast.makeText(getApplicationContext(), "Failed to Load Image", TastyToast.LENGTH_SHORT, TastyToast.ERROR);
                Log.d(TAG, e.toString());

            }
        });

    }

    @Override
    public void onStart() {
        super.onStart();
        eo.addSnapshotListener(new DetailsExcitingOffer(), new EventListener<DocumentSnapshot>() {
                    @Override
                    public void onEvent(@javax.annotation.Nullable DocumentSnapshot documentSnapshot, @javax.annotation.Nullable FirebaseFirestoreException e) {
                        if (e != null) {
                            return;
                        }
                        if (documentSnapshot.exists()) {

                            String title = documentSnapshot.getString(EXCITING_KEY_TITLE);

                            if(TextUtils.isEmpty(title))
                                eo_title.setText("Not Available");

                            else {
                                eo_title.setText(title);
                            }

                            String description = documentSnapshot.getString(EXCITING_KEY_DESCRIPTION);

                            if (TextUtils.isEmpty(description)) {
                                eo_description.setText("Not Available");
                            }

                            else {
                                eo_description.setText(description);
                            }

                            String coupon_code = documentSnapshot.getString(EXCITING_KEY_COUPON_CODE);

                            if(TextUtils.isEmpty(coupon_code))
                                eo_couponCode.setText("NOT REQUIRED");

                            else {
                                eo_couponCode.setText(coupon_code);
                            }

                            String dec_image = documentSnapshot.getString(EXCITING_DETAILS_IMAGE);

                            if (TextUtils.isEmpty(dec_image)) {
                                eo_details_image.setImageResource(R.drawable.failed_to_load_img);
                                eo_img_progress.setVisibility(View.GONE);
                            }

                            else {

                                Picasso.with(getApplicationContext())
                                        .load(dec_image)
                                        .fit()
                                        .into(eo_details_image, new Callback() {
                                            @Override
                                            public void onSuccess() {
                                                eo_img_progress.setVisibility(View.GONE);
                                                eo_failed_img.setVisibility(View.GONE);
                                            }

                                            @Override
                                            public void onError() {
                                                eo_img_progress.setVisibility(View.GONE);
                                                eo_failed_img.setVisibility(View.VISIBLE);
                                            }
                                        });

                                eo_target.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Uri uri = Uri.parse(EXCITING_KEY_TARGET);
                                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                        if (intent.resolveActivity(getApplicationContext().getPackageManager()) != null) {
                                            v.getContext().startActivity(intent);
                                        }
                                    }
                                });

                                eo_copyCode.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        if (android.os.Build.VERSION.SDK_INT > 11) {
                                            ClipboardManager clipboard = (ClipboardManager) getSystemService(getApplicationContext().CLIPBOARD_SERVICE);
                                            ClipData clip = ClipData.newPlainText("label", eo_couponCode.getText());
                                            clipboard.setPrimaryClip(clip);

                                            Toast.makeText(getApplicationContext(), "Code Copied",
                                                    Toast.LENGTH_SHORT).show();
                                        } else {

                                            ClipboardManager clipboard = (ClipboardManager) getSystemService(getApplicationContext().CLIPBOARD_SERVICE);
                                            ClipData clip = ClipData.newPlainText("label", eo_couponCode.getText());
                                            clipboard.setPrimaryClip(clip);

                                            Toast.makeText(getApplicationContext(), "Code Copied",
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });

                            }
                        }
                    }
                }
        );
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }
}

这是无法正常工作的代码:

eo_target.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Uri uri = Uri.parse(EXCITING_KEY_TARGET);
                                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                        if (intent.resolveActivity(getApplicationContext().getPackageManager()) != null) {
                                            v.getContext().startActivity(intent);
                                        }
                                    }
                                });

                                eo_copyCode.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        if (android.os.Build.VERSION.SDK_INT > 11) {
                                            ClipboardManager clipboard = (ClipboardManager) getSystemService(getApplicationContext().CLIPBOARD_SERVICE);
                                            ClipData clip = ClipData.newPlainText("label", eo_couponCode.getText());
                                            clipboard.setPrimaryClip(clip);

                                            Toast.makeText(getApplicationContext(), "Code Copied",
                                                    Toast.LENGTH_SHORT).show();
                                        } else {

                                            ClipboardManager clipboard = (ClipboardManager) getSystemService(getApplicationContext().CLIPBOARD_SERVICE);
                                            ClipData clip = ClipData.newPlainText("label", eo_couponCode.getText());
                                            clipboard.setPrimaryClip(clip);

                                            Toast.makeText(getApplicationContext(), "Code Copied",
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });

0 个答案:

没有答案