Android 在将数据添加到RoomDatabase后,我想更改按钮文本
我正在创建一个显示Sandwitch数据的详细信息活动按钮 我想用按钮将Sandwitch添加到房间数据库。 这是我的密码Android 在将数据添加到RoomDatabase后,我想更改按钮文本,android,database,android-room,android-button,Android,Database,Android Room,Android Button,我正在创建一个显示Sandwitch数据的详细信息活动按钮 我想用按钮将Sandwitch添加到房间数据库。 这是我的密码 import android.app.Application; import android.content.Context; import android.content.SharedPreferences; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTran
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();
}
}
}
}
导入android.app.Application;
导入android.content.Context;
导入android.content.SharedReferences;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentTransaction;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.squareup.picasso.picasso;
导入android.arch.lifecycle.ViewModelProviders;
公共类详细信息活动扩展了AppCompatActivity{
SandwitchViewModel SandwitchViewModel;
SharedReferences SharedReferences;
编辑器myEdit;
沙巫细节沙巫;
TextView名称,也可以是nownas,ingridients,来源地,描述;
按钮制作视频,如按钮;
图像视图URL;
字符串名称;
串源开关;
字符串imageURL;
字符串sandwitchdesc;
字符串sandswitchalsoknown=“”;
字符串sandwitchingreds=“”;
串链;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u details\u activivy);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar();
SharedReferences=GetSharedReferences(“ButtonText”,MODE_PRIVATE);
myEdit=SharedReferences.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.SandsoKnownas);
placeoforigin=(TextView)findViewById(R.id.originplace);
description=(TextView)findViewById(R.id.sanddescription);
URL=(ImageView)findViewById(R.id.sandwitchimage);
ingridients=(TextView)findViewById(R.id.sandComponents);
makingvideo=(按钮)findViewById(R.id.makingvideo);
likeButton=(按钮)findViewById(R.id.databaseadd);
loadUI();
}
void loadUI(){
毕加索.with(this).load(imageURL).into(URL);
name.setText(sandwitchname);
原始设置的位置(原始开关);
description.setText(sandwitchdesc);
对于(int j=0;j当房间上传数据并发送响应时,您可以这样做
if(response!=null){
button.settext("dislike);
}
您的按钮是一个还是两个?添加数据时,我只使用了一个按钮,然后将该按钮设置在room lib的响应中,就像使用按钮“data upload success”(数据上传成功)一样;