简体   繁体   中英

how to change name based on activity in model using android

I create a model called Review. There are two activities (QualityReivewActivity.java & FairnessReivewActivity.java) will call the model whenever user leaves a comment. I want to

public class Review {
    float fairness_rating; //change the name to quality_rating
    String post_id;
    String review_time;
    String reviewer_id;
    String text_review;


    public Review(){
        //
    }

    Review(float fairness_rating, String post_id, String review_time, String reviewer_id, String text_review){
        this.fairness_rating=fairness_rating;
        this.post_id=post_id;
        this.review_time=review_time;
        this.reviewer_id=reviewer_id;
        this.text_review=text_review;

    }

    public float getFairness_rating() {
        return fairness_rating;
    } //change to getQuality_rating if actitivty is QualityReivewActivity.java

    public String getPost_id() {
        return post_id;
    }

    public String getReview_time() {
        return review_time;
    }

    public String getReviewer_id() {
        return reviewer_id;
    }

    public String getText_review() {
        return text_review;
    }
}

and this is the segment of code of QualityReivewActivity

Review c = new Review(mRatingBar.getRating(), post_id, timedComment.toString(), reviewer_uid, my_comment.getText().toString()); //

However, the QualityReviewActivity always shows "fairness_rating". How can I make a dynamic model name to change to "quality_rating" if I am calling from QualityReviewActivity?

You can solve it by using enum.

public enum ratingType {
 Quality,
 Fairness
}

Then use that enum in your function.

public float get_rating(ratingType type) {
 switch (type) {
 case Quality:
  return quality_rating;
 case Fairness:
  return fairness_rating;
 }
}

After that, depending on the class, you can pass the necessary arguments and it will work.

I'm not good at Java, so it may not work if I write it as it is, but the idea is ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM