pass spinner and switch values to another activity

我们两清 提交于 2020-12-15 06:06:32

问题


I'm trying to pass Switch and Spinner Values from PostAdapter to PostDetail Activity on item click. Here is how I done it:

holder.post_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor= mContext.getSharedPreferences("PRESS", Context.MODE_PRIVATE).edit();
                editor.putString("postid", post.getPostid());
                editor.apply();

              /*  ((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment
                        , new PostDetailFragment()).commit();*/
               Intent i = new Intent(mContext, PostDetails.class);
               i.putExtra("Instructions",mPost.get(position).getInstructions());
               i.putExtra("title",mPost.get(position).getTitle());
               i.putExtra("postimage",mPost.get(position).getPostimage());
               i.putExtra("Meal", mPost.get(position).getMeal().toString());
                boolean s = mPost.get(position).getVegetarian().isChecked();
                if (s){
                    i.putExtra("Vegetarian", "Vegetarian");
                }


               mContext.startActivity(i);
               ((Activity) mContext).overridePendingTransition(0, 0);

            }
        });

Here is the Setter and Getter Code:

public class Post {
    private String postid;
    private String postimage;
    private String title;
    private String publisher;
    private String Instructions;
    private Switch Vegetarian;
    private String publishTime;
    private Spinner Meal;

    public Post(String postid, String postimage, String title, String publisher, String Instructions,Switch Vegetarian, String publishTime, Spinner Meal)
    {
        this.postid = postid;
        this.postimage = postimage;
        this.title = title;
        this.publisher = publisher;
        this.Instructions = Instructions;
        this.Vegetarian = Vegetarian;
        this.publishTime = publishTime;
        this.Meal = Meal;
    }
    @Exclude
    public Spinner getMeal() {
        return Meal;
    }

    public void setMeal(Spinner meal) {
        Meal = meal;
    }

    public Post ()
    { }

    public Switch getVegetarian() {
        return Vegetarian;
    }
    @Exclude
    public void setVegetarian(Switch vegetarian) {
        Vegetarian = vegetarian;
    }

    public String getPublishTime() {
        return publishTime;
    }

    public void setPublishTime(String publishTime) {
        this.publishTime = publishTime;
    }

    public String getInstructions() {
        return Instructions;
    }

    public void setInstructions(String instructions) {
        Instructions = instructions;
    }

    public String getPostid() {
        return postid;
    }

    public void setPostid(String postid) {
        this.postid = postid;
    }

    public String getPostimage() {
        return postimage;
    }

    public void setPostimage(String postimage) {
        this.postimage = postimage;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }
}

from PostDetail Activity Here is how I retrieved the values:

 setTtitle();
        setPostImagee();
        setInstra();
        setVegetariann();
       // setMealType();
    }

    public void setTtitle(){
        title = findViewById(R.id.title);
        title.setText(getIntent().getStringExtra("title"));
    }
    public void setPostImagee(){
        postImage = findViewById(R.id.postImage);
        postImage.setImageResource(getIntent().getIntExtra("postimage", R.id.logo_image));
    }
    public void setInstra(){
        Inst = findViewById(R.id.Inst);
        Inst.setText( getIntent().getStringExtra("Instructions"));
    }
    public void setVegetariann(){
        Vegetarian = findViewById(R.id.Vegetariane);
        Vegetarian.setText(getIntent().getStringExtra("Vegetarian"));
        }

the problem is When I remove @Exclude from Setter and Getter it shows this error:

 com.google.firebase.database.DatabaseException: Found conflicting getters for name: isFocusable

and when I add it, this error is gone but this new error appear:

Attempt to invoke virtual method 'java.lang.String android.widget.Spinner.toString()' on a null object reference

the problem is that with it being a Spinner or a switch, because it works for regular Text just fine. How can I pass the Spinner/Switch values correctly?

来源:https://stackoverflow.com/questions/64887914/pass-spinner-and-switch-values-to-another-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!