passing 2 variables into passedView - java

I can't seem to get this. I'm trying to pass two strings to a new activity but can only get one to pass. Can someone help? I would like the passed strings to say "I would like" movieName movieGroup "everyday"
public class Main22Activity extends AppCompatActivity {
String passedVar=null;
private TextView passedView= null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main22);
Bundle extras = getIntent().getExtras();
passedVar=getIntent ().getStringExtra(MainActivity.ID_EXTRA + "movieName");
passedVar2=getIntent ().getStringExtra(MainActivity.ID_EXTRA + "movieGroup");
passedView = (TextView) findViewById(R.id.textView3);
passedView.setText("I would like " + passedVar, passedVar2 + "everyday");
}

public class Main22Activity extends AppCompatActivity {
String passedVar=null;
String passedVar2=null;
private TextView passedView= null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main22);
Bundle extras = getIntent().getExtras();
passedVar=getIntent ().getStringExtra(MainActivity.ID_EXTRA +"movieName");
passedVar2=getIntent ().getStringExtra(MainActivity.ID_EXTRA + "movieGroup");
passedView = (TextView) findViewById(R.id.textView3);
passedView.setText("I would like " + passedVar + "," + passedVar2 + "everyday");
}
Notice change in
passedVar + "," + passedVar2

Related

Second activity can't read getExtras from intent

I'm trying to get the values from a recyclerView item (Firestore as database) in a new activity by using intents if I click on it. Don't know if that is the best way, but I got so far.
In my second activity I only get Null in the textViews I want to populate.
I've tried to test it with Toasts. You will see there is two Toast messages. The first one in the mainActivity (UltraLiquors) show the data I want to pull correctly (the ID, product and price), but the second one in the details activity only shows "null", so I assume there is a problem with my getExtras in my detail activity. I just don't know where to look anymore.
Please be so kind and help, I really have been struggling with this a few days.
Here is my mainActivity (UltraLiquors.class)
public class UltraLiquors extends AppCompatActivity {
private FirebaseFirestore ultra_liquor_db = FirebaseFirestore.getInstance();
private CollectionReference promo_oneRef = ultra_liquor_db.collection("ultra_liquors");
private static final int ACTIVITY_NUM = 2;
private UltraLiquorsRecyclerAdapter ultra_liquors_adapter;
private static final String TAG = "Ultra Liquors";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ultra_liquors);
setUpPromoOneRecyclerView();
//setupBottomNavigationView();
setTitle("Ultra Liquors");
Context context;
}
private void setUpPromoOneRecyclerView() {
Query query = promo_oneRef.whereEqualTo("promo_number", "1").orderBy("department");
FirestoreRecyclerOptions<Ultra_liquors> options = new FirestoreRecyclerOptions.Builder<Ultra_liquors>()
.setQuery(query, Ultra_liquors.class)
.build();
ultra_liquors_adapter = new UltraLiquorsRecyclerAdapter(options);
RecyclerView promo_one_recyclerView = findViewById(R.id.recycler_view_ultra_liquors);
//recyclerView.setHasFixedSize(true);
promo_one_recyclerView.setLayoutManager(new LinearLayoutManager(this));
promo_one_recyclerView.setAdapter(ultra_liquors_adapter);
ultra_liquors_adapter.setOnItemClickListener(new UltraLiquorsRecyclerAdapter.OnItemClickListener() {
#Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
Ultra_liquors note = documentSnapshot.toObject(Ultra_liquors.class);
String id = documentSnapshot.getId();
String product = (String) documentSnapshot.get("product");
String price = (String) documentSnapshot.get("price");
String path = documentSnapshot.getReference().getPath();
Toast.makeText(UltraLiquors.this,
"Position: " + position + " ID: " + product + price, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(UltraLiquors.this, ViewProduct.class);
intent.putExtra("Product", product);
intent.putExtra("Price", price);
startActivity(intent);
}
});
}
#Override
protected void onStart() {
super.onStart();
ultra_liquors_adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
ultra_liquors_adapter.stopListening();
}
}
And here is the detailActivity (ViewProduct) where I want to pull the data from the intent:
public class ViewProduct extends AppCompatActivity {
private String edit_product_ID;
//private String edit_product_Product;
private String edit_product_Price;
private TextView productView, priceView;
private TextView mSave, mDelete;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_product);
Intent intent = getIntent();
String product;
String price;
product = String.valueOf(getIntent().getExtras().get("product"));
price = String.valueOf(getIntent().getExtras().get("price"));
Toast.makeText(this, "The Product ID: " + edit_product_ID +
" Product Name " + product + " Product Price " + price, Toast.LENGTH_LONG).show();
TextView productView = findViewById(R.id.product);
productView.setText(product);
TextView priceView = findViewById(R.id.price);
priceView.setText(price);
mSave = (TextView) findViewById(R.id.save);
mDelete = (TextView) findViewById(R.id.delete);
}}
I am not the best yet with Java or Android, so please be patient.
You're using:
intent.putExtra("Product", product);
intent.putExtra("Price", price);
But then you're trying to get them with:
product = String.valueOf(getIntent().getExtras().get("product"));
price = String.valueOf(getIntent().getExtras().get("price"));
See how these values are different ?
You need to use the same casing, as it's case sensitive, so use something like:
product = String.valueOf(getIntent().getExtras().get("Product"));
price = String.valueOf(getIntent().getExtras().get("Price"));

Java/Android : First tiny app answer always "false"

I try to create my first tiny app but i have a problem.
My tiny math app always say my answer is false.I don't understand why.
This is my first app, i don't know where i'm wrong.
private TextView problem;
private EditText question;
private Button button;
private TextView reponse;
private int aleatoire = new Random().nextInt(61) + 20;
private int aleatoire2 = new Random().nextInt(48) + 20;
private int result = aleatoire + aleatoire2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
problem = (TextView) findViewById(R.id.problem);
question = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
reponse = (TextView)findViewById(R.id.resultat);
problem.setText("Result = "+aleatoire+"+"+aleatoire2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str=question.getText().toString();
if (str.equals(result)) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}
}
});
Answer always "False"
I'm a new student in the dev world.
The problem with code is that you are comparing string with integer, that's why it always returns false as java is strictly typed language.
problem code:
if(str.equals(result)){...}
possible solutions:
if( str.equals(""+result)){...}
or
str.equals(String.valueof(result)) // best solution
or
if(result==Integer.parseInt(str)){...}
Here is corrected code:
private TextView problem;
private EditText question;
private Button button;
private TextView reponse;
private int aleatoire = new Random().nextInt(61) + 20;
private int aleatoire2 = new Random().nextInt(48) + 20;
private int result = aleatoire + aleatoire2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
problem = (TextView) findViewById(R.id.problem);
question = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
reponse = (TextView)findViewById(R.id.resultat);
problem.setText("Result = "+aleatoire+"+"+aleatoire2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str=question.getText().toString();
if (str.equals(""+result)) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}
}
});
As azurefrog comment says, you're comparing a String to an int. You will need to transform str to an int or result to a String. You can for example do:
String str=question.getText().toString();
if (str.equals(String.valueOf(result))) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}

Passing values to activities and reciveving them in url

I am trying to receive an integer in url.
This how i pass value from one activity to another:
private void displayCategoriesInformation(CategoriesModel categoriesModel) {
//get references to your views
TextView tvCategoryId = (TextView) findViewById(R.id.tvCategoryId);
final int categoryId = categoriesModel.getId();
//set values from your categoriesModel java object to textView
tvCategoryId.setText("Id : " + categoriesModel.getId());
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Categories.this, SubCategories.class);
intent.putExtra("parameter_name", categoryId);
startActivity(intent);
}
});
}
in SubCategory.class i receive it like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.row_subcategory);
Intent intent = getIntent();
int recivedId = intent.getIntExtra("parameter_name", 2);
TextView tvRecivedId = (TextView) findViewById(R.id.tvRecivedId);
tvRecivedId.setText("recivedId" + recivedId);
dialog = new ProgressDialog(this);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.setMessage("Loading, please wait.....");
spinnerFood = (Spinner) findViewById(R.id.spinFood);
okButton = (Button) findViewById(R.id.bOk);
// spinner item select listener
spinnerFood.setOnItemSelectedListener(this);
new JSONTask().execute("http://146.185.178.83/resttest/subCategories");
}
now the value is stored in the variable recivedId which is 1 or 2 or 3 or 4
what i want to do is execute this JSONTask url like this
new JSONTask().execute("http://146.185.178.83/resttest/categories/recivedId/subCategories");
so the end url would look like this http://146.185.178.83/resttest/categories/1/subcategories/
how can i achieve this
String url = "http://146.185.178.83/resttest/categories/" + recivedId +"/subcategories/";
new JSONTask().execute(url);

Spinner value is not captured

Please, do not mark this as duplicate if you are not sure
I have three spinners and a botton. When the botton is clicked, the program makes a calculation depending on the value of the three spinners. Then this value passes two another activity and it shows in an editText. Here is my code:
Main
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
capturarTexto();
}
private void capturarTexto() {
Button button_calc = (Button) findViewById(R.id.button_calc);
button_calc.setOnClickListener(get_edit_view_button_listener);
}
private Button.OnClickListener get_edit_view_button_listener = new Button.OnClickListener() {
public void onClick(View v) {
EditText edit_text = (EditText) findViewById(R.id.textBox1);
String edit_text_value = edit_text.getText().toString();
StringTokenizer st = new StringTokenizer(edit_text_value);
int num_words = st.countTokens();
Spinner espec = (Spinner) findViewById(R.id.espec);
String espec_value = espec.getSelectedItem().toString();
Spinner lengor = (Spinner) findViewById(R.id.lista_origen);
String lengor_value = lengor.getSelectedItem().toString();
Spinner lengdest = (Spinner) findViewById(R.id.lista_destino);
String lengdest_value = lengdest.getSelectedItem().toString();
double precio = 0;
if(espec_value .equals("Medicina")){
if (lengor_value .equals("ES") && lengdest_value .equals("EN")){
precio = num_words * 0.12;
}
if (lengor_value .equals("ES") && lengdest_value .equals("FR")){
precio = num_words * 0.12;
}
if (lengor_value .equals("ES") && lengdest_value .equals("DE")){
precio = num_words * 0.12;
}
Intent intent = new Intent(Main2Activity.this, Main3Activity.class);
intent.putExtra("precio",precio);
startActivity(intent);
}
};
}
Main2
public class Main3Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent=getIntent();
int precio =(int) intent.getExtras().getInt("precio");
TextView txtCambio = (TextView) findViewById(R.id.textView4);
txtCambio.setText("Precio Total: "+ precio + " €");
}
After testing it, the value passed in this line of code:
intent.putExtra("precio",precio)
is allways 0. But if I change it to this:
intent.putExtra("precio",num_words)
it passes correctly the total number of words. This makes me think that the script is not entering in the first if(espec_value .equals("Medicina")) and then, it is not making any calculation.
Does anyone have an idea of how to solve this problem?
Thank you for your time
You are sending Double value and accessing Integer value.
Change the line in Main3Activity.
double precio = intent.getExtras().getDouble("precio");
If you want to parse double value to int then add one more line
int p = (int) precio;

aplying onPostExecute to onClickListener

i have made program which works with AsyncTask it prints a list of the JSON data when program is executed, but the problem is that i want it to execute when i press the button. How do i get the results of AsyncTask into my onClickButtonListener ? How do i call AsyncTask from onClick?
Code:
public class Instillinger extends MainActivity {
DatabaseHelper myDb;
String navn;
String adresse;
String bilmere;
TextView visNavn;
TextView visAdresse;
TextView visBil;
EditText navnFelt;
EditText adrFelt;
EditText bilFelt;
Button lagreButton;
Button tilbakeButton;
Button visDataButton;
List<Bruker> brukere;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.instillinger);
myDb = new DatabaseHelper(this);
// visNavn = (TextView) findViewById(R.id.visNavn);
visAdresse = (TextView) findViewById(R.id.visAdresse);
//visBil = (TextView) findViewById(R.id.visBil);
navnFelt = (EditText) findViewById(R.id.navnFelt);
adrFelt = (EditText) findViewById(R.id.adrFelt);
bilFelt = (EditText) findViewById(R.id.bilFelt);
lagreButton = (Button) findViewById(R.id.lagreButton);
tilbakeButton = (Button) findViewById(R.id.tilbakeButton);
//visDataButton = (Button) findViewById(R.id.visData);
brukere = myDb.getBrukere();
for(Bruker b: brukere){
String log = "Du er registrert som: "+ b.getNavn() + "\n" +
"Adresse: " + b.getAdresse() + "\n" +
"Bilmerke: " + b.getBilmerke() + "\n" +
"For å oppdatere informasjon fyll Alle feltene nede";
visAdresse.setText(log);
}
settInnData();
}
public void settInnData() {
lagreButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myDb.addContact(new Bruker("Giedrius","mldc","123","1"));
brukere = myDb.getBrukere();
for(Bruker b: brukere){
String log = "Du er registrert som: "+ b.getNavn() + "\n" +
"Adresse: " + b.getAdresse() + "\n" +
"Bilmerke: " + b.getBilmerke() + "\n" +
"Bilmerke: " + b.getState() + "\n" +
"For å oppdatere informasjon fyll Alle feltene nede";
visAdresse.setText(log);
}
//myDb.getContact(myDb.getCount()).toString();
}
});
}
}}
You can call your AsyncTask class inside your button listener by simply invoking new YourAsyncTaskName().execute(). Good Luck.

Categories