Issue
i am creating a button in details activity that displays the data of Sandwitches.
i want to add sandwitches to room database with button. This is my code.
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();
}
}
}
}
i want the button text to be "Dislike" after adding in the database.But after coming out of this activity it is becoming "Like" because of oncreate method. can i be able to do this?
Solution
when room uplaod data and sent response then you can do this
if(response!=null){
button.settext("dislike);
}
Answered By - Adnan haider
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.