将数据添加到 RoomDatabase 后,我想更改按钮文本

问题描述

我正在详细信息活动中创建一个按钮,用于显示 Sandwitches 的数据。

我想通过按钮将三明治添加到房间数据库中。 这是我的代码

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import android.arch.lifecycle.viewmodelProviders;

public class DetailsActivy extends AppCompatActivity {
    Sandwitchviewmodel sandwitchviewmodel ;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor myEdit;
    SandwitchDetails sandwitch;
    TextView name,alsokNownas,ingridients,placeoforigin,description;
    Button makingvideo,likeButton;
    ImageView URL;
    String sandwitchname;
    String originofSandwitch;
    String imageURL;
    String sandwitchdesc;
    String sandwitchalsokNown="";
    String sandwitchingreds="";
    String makinglink;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details_activy);
        getSupportActionBar().setdisplayHomeAsUpEnabled(true);
        getSupportActionBar();
        sharedPreferences = getSharedPreferences("ButtonText",MODE_PRIVATE);
        myEdit = sharedPreferences.edit();
        Bundle data = getIntent().getExtras();
        sandwitch = (SandwitchDetails) data.getSerializable(getString(R.string.details_sandwitchobject));
        sandwitchviewmodel =  viewmodelProviders.of(this).get(Sandwitchviewmodel.class);
        sandwitchname = sandwitch.getName();
        originofSandwitch = sandwitch.getPlaceoforigin();
        imageURL = sandwitch.getURL();
        sandwitchdesc = sandwitch.getDescription();
        makinglink = sandwitch.getYoutubelink();
        name = (TextView) findViewById(R.id.name);
        alsokNownas = (TextView) findViewById(R.id.sandalsokNownas);
        placeoforigin = (TextView) findViewById(R.id.originplace);
        description = (TextView) findViewById(R.id.sanddescription);
        URL = (ImageView) findViewById(R.id.sandwitchimage);
        ingridients = (TextView) findViewById(R.id.sandingredients);
        makingvideo = (Button) findViewById(R.id.makingvideo);
        likeButton = (Button) findViewById(R.id.databaseadd);
        loadUI();
    }


    void loadUI(){
        Picasso.with(this).load(imageURL).into(URL);
        name.setText(sandwitchname);
        placeoforigin.setText(originofSandwitch);
        description.setText(sandwitchdesc);
        for(int j=0;j<sandwitch.getAlsokNownas().size();j++)
        {
            sandwitchalsokNown = sandwitchalsokNown + (String) sandwitch.getAlsokNownas().get(j) + "\n";
        }
        for(int j=0;j<sandwitch.getIngridients().size();j++)
        {
            sandwitchingreds = sandwitchingreds + (String)sandwitch.getIngridients().get(j)+"\n";
        }
        alsokNownas.setText(sandwitchalsokNown);
        ingridients.setText(sandwitchingreds);
        
    }



    public void playVideo(View view) {
        Bundle bundle = new Bundle();
        bundle.putString(getString(R.string.video_url),makinglink);
        Fragment fragment = new YoutubeVideo();
        fragment.setArguments(bundle);
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.videofragment,fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

    public void addAndDeletetoDatabase(View view)
    {
        String buttontext = (String) likeButton.getText();
        SandwitchEntity sandwitchEntity;
        if(buttontext.equals("like"))
        {

            sandwitchEntity = new SandwitchEntity(sandwitchname,sandwitch.getAlsokNownas(),sandwitch.getIngridients(),originofSandwitch,sandwitchdesc,imageURL,makinglink);
            SandwitchEntity r =  sandwitchviewmodel.checkSandwichInDatabase(sandwitchEntity.getName());
            if(r==null)
            {
                sandwitchviewmodel.insert(sandwitchEntity);
                likeButton.setText("dislike");
                Toast.makeText(this,"Inserted",Toast.LENGTH_SHORT).show();
                myEdit.putString("name",likeButton.getText().toString());
                myEdit.commit();
            }
            else
            {
                Toast.makeText(this,"Liked PrevIoUsly",Toast.LENGTH_SHORT).show();
            }
        }
        else{
            sandwitchEntity = new SandwitchEntity(sandwitchname,makinglink);
            SandwitchEntity r =  sandwitchviewmodel.checkSandwichInDatabase(sandwitchEntity.getName());
            if(r!=null)
            {
                sandwitchviewmodel.delete(sandwitchEntity);
                likeButton.setText("like");
                Toast.makeText(this,"Deleted","Not Liked PrevIoUsly",Toast.LENGTH_SHORT).show();
            }
        }
    }
}

我希望在添加数据库后按钮文本为“不喜欢”。但是在退出此活动后,由于 oncreate 方法,它变成了“喜欢”。 我能做到吗?

解决方法

当房间上传数据并发送响应时,您可以这样做

if(response!=null){
  button.settext("dislike);
  }