如何将快照中的ID传递给其他活动Android

时间:2020-04-18 06:42:09

标签: android google-cloud-firestore

enter image description here我已经设法在AdminOfferFragment中获取我的文档ID,并且我希望将该值传递给EditOffer以更新数据。我尝试使用Intent put extra在另一个活动中获取我的ID,但它仍显示为null。那么,如何在AdminOfferFragment中的onClick之后将我的ID传递给EditOffer?

AdminOfferFragment

String id;
    RecyclerView myRecycleView;
    private FirestoreAdapter2 adapter;
    FirebaseFirestore fStore;
    private String email = "";
    Button Btn;
    Button Btn2;
    FirebaseAuth fAuth;
     String userId ="";
    private Integer position = 0;
    ArrayList<String> arraylist = new ArrayList<>();
    ArrayAdapter<String> arrayAdapter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_admin_offer, container, false);
        myRecycleView = v.findViewById(R.id.recyclerView);
        Btn = v.findViewById(R.id.editBtn);
        Btn2 = v.findViewById(R.id.deleteBtn);
        fAuth = FirebaseAuth.getInstance();
        fStore = FirebaseFirestore.getInstance();
        Btn2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                startActivity(new Intent(getActivity(),LoginUI.class));
            }
        });
        Query query = fStore.collection("offer");
        PagedList.Config config = new PagedList.Config.Builder().setInitialLoadSizeHint(10).setPageSize(3).build();
        FirestorePagingOptions<OfferInfo> options = new FirestorePagingOptions.Builder<OfferInfo>().setLifecycleOwner(this)
                .setQuery(query, config, new SnapshotParser<OfferInfo>() {
                    @NonNull
                    @Override
                    public OfferInfo parseSnapshot(@NonNull DocumentSnapshot snapshot) {
                        OfferInfo offerInfo = snapshot.toObject(OfferInfo.class);
                        String itemId = snapshot.getId();
                        offerInfo.setItem_id(itemId);
                        return offerInfo;

                    }
                }).build();

        adapter = new FirestoreAdapter2(options, this);

        myRecycleView.setHasFixedSize(true);
        myRecycleView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        myRecycleView.setAdapter(adapter);

        return v;
    }
    @Override
    public void onItemClick(DocumentSnapshot snapshot, int position) {
        OfferInfo offerInfo = snapshot.toObject(OfferInfo.class);
        Log.d("Item_CLICK", "Clicked the item : " + position + "and the ID:" + offerInfo.getName());
        Log.d("Item_CLICK", "Clicked the item : " + position + "and the ID:" + snapshot.getId());
        this.userId = snapshot.getId();
        id = snapshot.getId();
        String reward_name = offerInfo.getName();
        String reward_description = offerInfo.getDescription();
        String reward_point = Integer.toString(offerInfo.getPoint());
        this.position = position;
        Intent intent = new Intent(getActivity(), EditOffer.class);
        intent.putExtra("ID", id);
        intent.putExtra("REWARD_NAME", reward_name);
        intent.putExtra("REWARD_DESCRIPTION", reward_description);
        intent.putExtra("REWARD_POINT", reward_point);
        startActivity(intent);
    }
    public String getOfferId(){
        return id;
    }

EditOffer

public class EditOffer extends AppCompatActivity {
    EditText offer1, description1, point1;
    Button editBtn;
    FirebaseFirestore fStore;
    String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_offer);
        Intent intent = getIntent();
        String reward_name = intent.getStringExtra("REWARD_NAME");
        String reward_description = intent.getStringExtra("REWARD_DESCRIPTION");
        String reward_point = intent.getStringExtra("REWARD_POINT");
        offer1 = findViewById(R.id.Rewardinfo);
        description1 = findViewById(R.id.description);
        point1 = findViewById(R.id.point);
        offer1.setText(reward_name);
        description1.setText(reward_description);
        point1.setText(reward_point);
        id = intent.getStringExtra("ID");
        editBtn = findViewById(R.id.Btn2);
        editBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Editable newname = offer1.getText();
                Editable newdescription = description1.getText();
                Editable newpoint = point1.getText();
                updateDocument(newname, newdescription, newpoint, id);
            }
        });



    }

    private void updateDocument(Editable newname, Editable newdescription, Editable newpoint, String id) {
        this.id = id;

        DocumentReference documentReference = fStore.collection("offer").document(id);
        documentReference.update("name", newname);
        documentReference.update("description", newdescription);
        documentReference.update("point", newpoint)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Toast.makeText(EditOffer.this,"Document Updated",Toast.LENGTH_LONG).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(EditOffer.this,e.getMessage(),Toast.LENGTH_LONG).show();
                        Log.d("Androidview", e.getMessage());
                    }
                });

    }
}

FirestoreAdapter2

public class FirestoreAdapter2 extends FirestorePagingAdapter<OfferInfo, FirestoreAdapter2.OfferViewHolder> {

    private OnItemClickListener onItemClickListener;

    public FirestoreAdapter2(@NonNull FirestorePagingOptions<OfferInfo> options, OnItemClickListener onItemClickListener) {
        super(options);
        this.onItemClickListener = (OnItemClickListener) onItemClickListener;
    }

    @Override
    protected void onBindViewHolder(@NonNull OfferViewHolder holder, int position, @NonNull OfferInfo model) {

        holder.list_email.setText(model.getName());
        holder.list_fname.setText(model.getDescription());
        holder.list_point.setText(Integer.toString(model.getPoint()));

    }
    @NonNull
    @Override
    public OfferViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.offer_item, parent, false);

        return new OfferViewHolder(view);

    }

    public class OfferViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private TextView list_email;
        private TextView list_fname;
        private TextView list_point;
        public OfferViewHolder(@NonNull View itemview) {
            super(itemview);
            list_email = itemView.findViewById(R.id.textView);
            list_fname = itemView.findViewById(R.id.textView2);
            list_point = itemView.findViewById(R.id.textView3);
            itemview.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            onItemClickListener.onItemClick(getItem(getAdapterPosition()), getAdapterPosition());

        }
    }
    public interface OnItemClickListener{
        void onItemClick(DocumentSnapshot snapshot, int position);
    }

}

OfferInfo

public class OfferInfo {
    private String item_id;
    private String name;
    private String description;
    private int point;

    private OfferInfo(){}

    private OfferInfo(String name, String description, String item_id, int point){
        this.name = name;
        this.description = description;
        this.point = point;
        this.item_id = item_id;

    }

    public String getName(){
        return name;
    }
    public void setEmail(String name){
        this.name = name;
    }
    public String getDescription(){
        return description;
    }
    public void setFName(String description){
        this.description = description;
    }


    public String getItem_id() {
        return item_id;
    }

    public void setItem_id(String item_id) {
        this.item_id = item_id;
    }
    public int getPoint() {
        return point;
    }

    public void setPoint(int point) {
        this.point = point;
    }
}

1 个答案:

答案 0 :(得分:1)

在新活动中,您不会初始化firestoreref,因此请将其添加到打开的活动中:

fStore = FirebaseFirestore.getInstance();