我想在将数据添加到房间数据库后更改按钮文本

i want to chenge the button text after adding the data to RoomDatabase

我正在详细创建一个按钮 activity 来显示 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,sandwitch.getAlsoknownas(),sandwitch.getIngridients(),originofSandwitch,sandwitchdesc,imageURL,makinglink);
            SandwitchEntity r =  sandwitchViewModel.checkSandwichInDatabase(sandwitchEntity.getName());
            if(r!=null)
            {
                sandwitchViewModel.delete(sandwitchEntity);
                likeButton.setText("like");
                Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show();
                myEdit.putString("name", likeButton.getText().toString());
                myEdit.commit();
            }
            else
            {
                Toast.makeText(this, "Not Liked Previously", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

我希望按钮文本在添加 database.But 之后变成“不喜欢”,然后从这个 activity 中出来,因为 oncreate 方法它变成了“喜欢”。 我能做到吗?

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

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