'SetText is not working for TextView. In my code textValue.setText(Integer.toString(newValue)); is giving error as error: <identifier> expected
package com.example.menewapp;
import android.os.Bundle;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.example.menewapp.databinding.ActivityMainBinding;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration appBarConfiguration;
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
binding.fab.setOnClickListener(new View.OnClickListener() {
TextView textValue = findViewById(R.id.text_value);
String stringvalue = textValue.getText().toString();
int old = Integer.parseInt(stringvalue);
int newValue = MyNewWorker.doubleTheValue(old);
textValue.setText(Integer.toString(newValue));
@Override
public void onClick(View view) {
Snackbar.make(view, "old " + old + "is changed to " + newValue, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
SetText is not working for TextView. In my code textValue.setText(Integer.toString(newValue)); is giving error as error: expected. Please help me. textValue.getText is working but textValue.setText is not. i am new to android application development and not getting clue on how to solve this
Solution 1:[1]
Your code that reads and manipulates the text_value should be included in the onClick() method that is inside the OnClickListener()
binding.fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView textValue = findViewById(R.id.text_value);
String stringValue = textValue.getText().toString();
int originalValue = Integer.parseInt(stringValue);
int newValue = MyWorker.doubleTheValue(originalValue);
textValue.setText(Integer.toString(newValue));
Snackbar.make(view, "old " + old + "is changed to " + newValue, Snackbar.LENGTH_LONG).setAction("Action", null).show();
}
});
I came across this exact problem while going through the Pluralsight course Understanding Android Applications
Solution 2:[2]
Instead of Integer.toString(newValue) use String.valueOf()
textValue.setText(String.valueOf(newValue))
Solution 3:[3]
you can give a try to a CGo-free port of sqlite: https://pkg.go.dev/modernc.org/sqlite
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jaques Grobler |
| Solution 2 | B001ᛦ |
| Solution 3 | serge-v |
