Android studio cannot resolve symbol equals - java

I am making a simple mobile app. For now I am just testing the app and trying to pass some values in between java files and put them in empty TextViews. I get values from a previous activity via Intent and then trying to pass them on to another activity called ConsultActivity.java:
String username = getIntent().getStringExtra("Identifiant");
final TextView tv = (TextView)findViewById(R.id.TVUsername);
if(username.equals("marcel123")){
tv.setText("M Dupond");
}
else if(username.equals("tommy1")){
tv.setText("M Thompson");
}
else if(username.equals("emma89")){
tv.setText("Mme Sinieux");
}
consult = (ImageView)findViewById(R.id.consult);
consult.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,ConsultActivity.class);
i.putExtra("Username", tv.getText().toString());
startActivity(i);
}
});
However in my ConsultActivity, when I am doing basically the same thing, my equals are highlighted and say "Cannot resolve symbol equals"
String name = getIntent().getStringExtra("Username");
final TextView textV = (TextView)findViewById(R.id.TVUsername2);
if(name.equals("M Dupond")){
textV.setText("M Dupond");
}
else if(name.equals("M Thompson")){
textV.setText("M Thompson");
}
else if(name.equals("Mme Sinieux")){
textV.setText("Mme Sinieux");
}

Probably its just a Synchronization problem try with: Sych project with gradle files

Related

Unable to send variable value from one activity to the other in Android

I am trying to send the value stored in a variable my_var from one activity to the other in Android. There are probably already many similar questions here at SOV, but I have been trying things by my own, and so far, no success. I shall highly appreciate little help or hints on what I am doing wrong?
My (pseudo/example) code is like this:
public class MyActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
public String my_var;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
My_method();
}
public void my_method() {
// This is a method that makes HTTP GET request and parse response to my_var
my_var = responseObject1.getX() + " " + responseObject1.getY()
}
// Then, at the bottom of MyActivity, I am creating an Intent to pass my_var to another activity to show it in TextView.
// I took this method from here[.][1]
public void rsa_key(String s){
Intent intent = new Intent(getBaseContext(), AnotherClass.class);
intent.putExtra("my_var", my_var);
startActivity(intent);
}
}
Then, in the other activity (in OnCreate), I am trying to get my_var like this:
// public String my_var in initialization
Intent intent = getIntent();
my_var = intent.getStringExtra("my_var");
The app compiles, and I get no errors, but I can't see my_var value (XML layout) when put it in TextView.setText(my_var); in the other activity. There were no useful hints in the log as well. Can somebody help me to understand what am I doing wrong? or missing something.
I also tried SharedPreferences like this but no luck!
In first acitvity:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("varKey", my_var);
editor.commit();
Second actvity:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
my_var = sharedPref.getString("varKey", my_var);
I shall highly appreciate help/suggestions to fix this. Many thanks!
try this code for putExtra and get data in the second activity.
Use this to "put" the file...
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
i.putExtra("my_var", my_var);
startActivity(i);
Then, to retrieve the value try something like:
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("my_var");
}
} else {
newString= (String) savedInstanceState.getSerializable("my_var");
}

How can I transfer different .putextras for some intent?

So i have some markers and Custom Info Windows and putextras.
I don't know to use putextras that much but I know how to use them a bit.
So I used a put extra to transfer data from MapsActivity to Country Adapter which I will explain:
So my idea is that when a user clicks on CustomInfoWindow an intent opens named Country Adapter. That country adapter has 2 textviews for title and content. Currently i am successful just in transferring one set of data but the other just doesn't work. I may be doing something wrong but I get really confused in setting Put extras.
MapsActivity:
#Override
public void onInfoWindowClick(Marker marker) {
if("India".equals(marker.getTitle())) {
Intent intent = new Intent(this,Country.class);
intent.putExtra("India","text");
startActivity(intent);
}
if("Australia".equals(marker.getTitle())){
Intent intent = new Intent(this,Country.class);
intent.putExtra("Austrailia","text");
// want to create a method that when this is clicked it gives different texts.
startActivity(intent);
}
My Country Adapter:
Button bt = findViewById(R.id.button);
bt.setOnClickListener(v -> openMap());
TextView countryName = findViewById(R.id.textView);
TextView Main = findViewById(R.id.textView2);
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
String India = bundle.getString("India");
countryName.setText("India,South Asia");
}
if(bundle != null) {
String Australia = bundle.getString("Australia");
countryName.setText("Australia,Oceania");
}
}
public void openMap(){
finish();
}
}
I want to display different texts for both of them but it doesn't work. I am very new to this so please answer in detail..
You are on the right track, but your primary issue lies in the way you extract the data from the Intent bundle.
Firstly, consider your code:
if(bundle != null) {
String India = bundle.getString("India");
countryName.setText("India,South Asia");
}
if(bundle != null) {
String Australia = bundle.getString("Australia");
countryName.setText("Australia,Oceania");
}
You essentially first extract information about India. Regardless of whether this is successful, you then do the same for Australia - overwriting the text you just set for India. You will only ever be able to see Australia as the output.
Your second problem lies in the way you use Intent extras. From the Android documentation:
public Intent putExtra (String name, String value)
name String: The name of the extra data, with package prefix.
value String: The String data value. This value may be null.
So you are better off using a name of the extra such as com.mypackage.CountryName and then set the value to India. You can then have a separate Intent extra named com.mypackage.CountryDetails with the descriptive value you wish to show. In your MapsActivity:
Intent intent = new Intent(this,Country.class);
if("India".equals(marker.getTitle())) {
intent.putExtra("com.mypackage.CountryName", "India,South Asia");
intent.putExtra("com.mypackage.CountryDetails", "...");
} else if("Australia".equals(marker.getTitle())){
intent.putExtra("com.mypackage.CountryName", "Australia,Oceania");
intent.putExtra("com.mypackage.CountryDetails", "...");
}
startActivity(intent);
And in your Country Adapter:
Button bt = findViewById(R.id.button);
bt.setOnClickListener(v -> openMap());
TextView countryName = findViewById(R.id.textView);
TextView Main = findViewById(R.id.textView2);
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
String name = bundle.getString("com.mypackage.CountryName");
countryName.setText(name);
String details = bundle.getString("com.mypackage.CountryDetails");
countryDetails.setText(details);
// If you absolutely must do something on a per-country basis, then use a
// switch or chained if..else inside of this if statement
}
// Notice how the second if statement is gone
}
public void openMap(){
finish();
}
}
You can have a long discussion about delegation of responsibilities and architecture. I ignore this for now. But in terms of good coding practices for naming your Intent extras, have a look at this related answer.

I want to use findViewById with strings as an Id

I am new to Android Studio and just trying to learn new ways to code and I'm stuck right now. I would like to use a String in a findViewById for example.
public void BDate0(String BId, String BHistoryClass) {
bdatum0 = (Button) findViewById(R.id.BId);
bdatum0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent BDatum0 = new Intent(bethistory.this, BHistoryClass.class);
startActivity(BDatum0);
}
});
}
and I would like to call the BDate0 with the strings for example,
BDate0(BId1, BHistory1);
I have 10 buttons and the activity they start would be different every day.
Sorry for bad englihs it's not my native language and thank you for help in advance.
You can use this code to replace the findViewById(..). This should make it possible to use a string as identifier:
String BId = "button1"; // for example
int id = getResources().getIdentifier(BId, "id", getPackageName());
// finds R.id.button1
bdatum0 = (Button) findViewById(id);

Passing an integer using Intent not working in Android

I have an Intent set up that opens up a new activity and I wanted to pass an Integer value. The opening of the activity works but as soon as I use a code to pass on the value app crashes.
Here is my code of main activity -
public void onFinish() {
tap1.setClickable(false);
Intent i = new Intent( Single.this, FinalScore.class);
i.putExtra("kee1", count);
startActivity(i);
Value is fetched using below code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final_score);
TextView tx = (TextView) findViewById(R.id.textView3);
tx.setText(getIntent().getExtras().getInt("kee1"));
}
you need cast int value to String for setting to TextView:
you can use following code:
tx.setText(""+getIntent().getExtras().getInt("kee1"));
add "" to first of the value do this for you.
or you can use following code to cast that:
String value = String.valueOf(getIntent().getExtras().getInt("kee1"));
tx.setText(value);
you can use Integer.toString(); too as #Duncan mentioned on comment.

Buttons isn't setting value

Im brand new to android and I have no idea what I'm doing wrong. The current code looks like:
public class TypeActivity extends Activity {
private boolean alcoholin = false;
...
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.search_type);
alcohol = (Button) findViewById(R.id.alcohol_button);
...
alcohol.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alcoholin=true;
Intent i = new Intent (TypeActivity.this,ingredients.class);
startActivity(i);
}
});
...
public boolean getalcholin(){
return alcoholin;
}
This code is then supposed to set a value in another class. I have tested the code and I know that if i state the the boolean is true in the beginning of my code, then I will make the other code's boolean equal true. However, if I try to set the value when the user presses the button the value does not get updated.
Please help!
On Android the standard way to send data from one Activity to another is by specifying "extras" on the Intent that you use to start a new activity.
You are already using an Intent in your onClick method to start your "ingredients" activity (Your code would be more readable if you named your activity something like IngredientsActivity instead) - you just need to add some "extras" to it.
Please read up on the training tutorial here, but without knowing what your ultimate goal is you probably want something like:
alcohol.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent (TypeActivity.this,IngredientsActivity.class);
i.putExtra(IngredientsActivity.EXTRA_INGREDIENT_TYPE, "alcohol");
startActivity(i);
}
});
... and then in IngredientsActivity you would have something like:
public static final String EXTRA_INGREDIENT_TYPE = "ingredient";
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ingredients);
String ingredientType = getIntent().getStringExtra(IngredientsActivity.EXTRA_INGREDIENT_TYPE);
}
This would never work because alcoholin would NOT exist as this is an entirely different Activity. Why don't you instead use the Extras of the intent to pass data between the two Activities.
Standards
Class names should always have each word capitalized like MyClassObject in java.

Categories