taking variable from another class - java

I have to spinners, and when I start my app, the PHP only returns values of spinner's first choice.
First code is part of one class (IzboraGrada.java)
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
str_grad=spinner1.getSelectedItem().toString();
str_predmet=spinner2.getSelectedItem().toString();
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
And the second code is part of MainActivity.class that was in intent.
private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
String url = "http://192.168.1.102/test/spinner.php";
Bundle extras = getIntent().getExtras();
if (extras != null) {
url = extras.getString("URL");
}
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
I presume that's because str_grad and str_predmet are not defined in second class. But If I put str_grad and str_predmet in second class, they are can't be resolved as type.Any ideas what to do?

It looks like you are calling this method at the beginning, before an item is selected, so I think the problem is that you are setting the values for str_grad and str_predmet when they are first set so the selected item is the default item. Those are getter functions not listeners.
You need to move those lines inside the onClick() or use onItemSelected() on your Spinners to set those variable values
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
str_grad=spinner1.getSelectedItem().toString(); // move these lines here
str_predmet=spinner2.getSelectedItem().toString();
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
If I understand your problem correctly, that should solve your problem.

Related

After creating many TextViews and giving them IDs, I am trying to open an activity with their information. How do I do that?

So I can successfully create an android Text View using java and set it a value and an ID. But my problem is that after I create them and gave them values and id, I have no control over them. After clicking on them, I want the new page to have the data that was stored in that Text View.
Here is how I created and set value and ID to my Text Views:
textView = new TextView(SellActivity.this);
// put the data in the text view
textView.setText(data);
// give it an id
textView.setId(Integer.parseInt(getStrID));
//place it nicely under one another
textView.setPadding(0, 50, 0, 0);
// if clicked any of the textviews, open the offer page
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//setGetStrID(getStrID);
Intent intent = new Intent(SellActivity.this, OfferActivity.class);
startActivity(intent);
}
});
// add the text view to our layout
linearLayout.addView(textView);
You just have to pass the data to the new intent,
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(SellActivity.this, OfferActivity.class);
intent.putExtra("TextViewContent", textView.getText());
startActivity(intent);
}
});
Then in onCreate function of OfferActivity.java,
String s = getIntent().getStringExtra("TextViewContent");
Now the string s will contain the value of your textView.
You can put the data in the intent when you are opening a new activity
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//setGetStrID(getStrID);
Intent intent = new Intent(SellActivity.this, OfferActivity.class);
intent.putExtra("mydata", textView.getText());
startActivity(intent);
}
});
In the oncreate of OfferActivity receive this data as string
String mydata = intent.getStringExtra("mydata");

Why does Android Studio keep asking for a "Expression Expected"?

Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSighin = (Button) findViewById(R.id.btnSignIn);
btnSignUp = (Button) findViewById(R.id.btnSignUp);
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent j = new Intent(MainActivity.this, SignUp.class);
startActivity(SignUp);
}
});
btnSighin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View view) {
Intent k = new Intent(MainActivity.this, SignIn.class);
startActivity(SignIn);
}
});
}
I am working on an app that allows customers to book appointments and in order to do so, they need to sign up or sign in. I have activities made for both of them and I can't run it without getting the "Expression Expected" and I'm new to Android Studio and have no idea what to do. Any help?
You are missing some information. Anyway, i see that you have mistakes in lines
startActivity(SignUp);
and
startActivity(SignIn);
The correct way should be startActivity(j) and startActivity(k) respectively
You have to pass the intent object to the start activity method not the signup or signin class
So it should be startActivity(i) and startActivity(j)

Setting TextView in Another Class

I have researched many problems like mine on the internet and none seem to be doing what I'm trying to do. What I am trying to do is get a textview that is currently blank in my high_risk.xml file, and make it have text based on if a button is clicked in another class. Here is what I have so far...
Question13Activity(if the yes button is clicked I want to be able to set the text in the HighRisk Activity)
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = getIntent();
String text = intent.getStringExtra("New Text");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
}
});
This is how I have the static variable t defined in my highrisk activity class...
HighRiskActivity(this is where I want the text to be set and displayed)
public static TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
Intent myIntent = new Intent(HighRiskActivity.this, Question12Activity.class);
myIntent.putExtra("Text", "New Text");
startActivity(myIntent);
Every time I try and access the contents of t in another class and change it's text, it always returns null. Any way I can fix this from happening? Any help would be greatly appreciated :)
You can use Bounds, to get the the data to the new Activity and set the text from there:
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("<KEY-NAME>", "<TEXT>");
startActivity(myIntent);
And in the Question13Activity.class:
Intent intent = getIntent();
String text= intent.getStringExtra("<KEY-NAME>");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
UPDATE:
Use it so:
HighRiskActivity.class:
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("TestKey", "My new Text");
startActivity(myIntent);
}
});
Question13Activity.class:
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
Intent intent = getIntent();
String text= intent.getStringExtra("TestKey");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
In order to setText() from another class (for example, from your Custom class), you need to specify the activity on which you'd like to do that.
Unless a class does have a reference of your context, you cannot call 'findViewById()'
To do that, you use Context as a parameter for custom class constructor.
Just after you have set the context will you be able to call
TextView txtView = (TextView) ((Activity)context).findViewById(R.id.text);
txtView.setText("foo");
}

App Crashes upon receiving data from intent

I have two activities that I would like to send data between. I would like to send two strings from the second activity to the first, where the strings will be manipulated and applied to the code. Here is the intent I have in the second activity to send the strings to the first activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
phoneNum = (EditText)findViewById(R.id.phoneNum);
msgCounter = (EditText)findViewById(R.id.msgCount);
apply = (Button)findViewById(R.id.butto);
save = (Button)findViewById(R.id.butt);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pass = new Intent();
pass.putExtra("value", phoneNum.getText().toString());
pass.putExtra("cost",msgCounter.getText().toString());
startActivity(pass);
}
});
And here is the intent I have in the first acitivity to receive the data:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_messages);
request_code = 1;
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.but);
assert button != null;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(TextMessages.this,Settings.class);
startActivityForResult(intent, request_code);
Intent a = new Intent(TextMessages.this,Settings.class);
startActivityForResult(a, recode);
}
});
Intent pass = getIntent();
String address = pass.getStringExtra("value");
String string2 = pass.getStringExtra("cost");
However, the app always crashes when I start it. So I can not use any of the data. Thanks in advance to any help I may receive.
In the first activity you should get your strings from onActivityResult. You shouldn't use startActivity in second activity.
Checkout this link for more information
https://developer.android.com/training/basics/intents/result.html
In your second activity you should use setResult method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
phoneNum = (EditText)findViewById(R.id.phoneNum);
msgCounter = (EditText)findViewById(R.id.msgCount);
apply = (Button)findViewById(R.id.butto);
save = (Button)findViewById(R.id.butt);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pass = new Intent();
pass.putExtra("value", phoneNum.getText().toString());
pass.putExtra("cost",msgCounter.getText().toString());
setResult(Activity.RESULT_OK,pass);
finish();
}
});
In your first activity you should get Intent data inside the onActivityResult method.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == request_code) {
if(resultCode == Activity.RESULT_OK){
String value=data.getStringExtra("value");
String cost=data.getStringExtra("cost");
//handle value and cost.
}
}
}
I think it's because you missed out to put the target Activity class when you instantiate your intent. It should be like this:
Intent pass = new Intent(TargetActivity.class);
pass.putExtra("value", phoneNum.getText().toString());
pass.putExtra("cost",msgCounter.getText().toString());
startActivity(pass);
Hope it helps.
EDIT: But I agree with Abdullah. Since the first activity started the second activity, use onActivityResult to pass values to the first activity.
My guess is, the app starts into the second code block you posted. There is no value for String address = pass.getStringExtra("value"); or String string2 = pass.getStringExtra("cost"); when you start your app - only when the second activity sends these to the first activity. I would recommend finding a way to determine if its the second activity creating the first activity or creating a method to set these values in the first activity and call it in the second activity. Also, your second activity should be:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
phoneNum = (EditText)findViewById(R.id.phoneNum);
msgCounter = (EditText)findViewById(R.id.msgCount);
apply = (Button)findViewById(R.id.butto);
save = (Button)findViewById(R.id.butt);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pass = new Intent(SecondActivity.this, FirstActivity.class);
pass.putExtra("value", phoneNum.getText().toString());
pass.putExtra("cost",msgCounter.getText().toString());
startActivity(pass);
}
});

Creating a button intent to save information

I am creating an android application that uses data from buttons clicked to generate an answer stored in the shared preferences. I am having trouble finding the necessary intent code to save the information when the user clicked on the button so it can be used to calculate the answer. As you can see from the code when the user clicks the button it will generate the next activity.Thanks!
Button button18 = (Button) findViewById(R.id.button18);
button18.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//I need to store the results then move to the next activity shown below?
Intent i = new Intent(EnterAgeActivity.this, EnterWeightActivity.class);
startActivity(i);
// TODO Auto-generated method stub
}
});
`
Send the data with the intent.
#Override
public void onClick(View v) {
//I need to store the results then move to the next activity shown below?
Bundle data = new Bundle();
data.putString(key, value);
data.putInt(key, value);
Intent i = new Intent(EnterAgeActivity.this, EnterWeightActivity.class);
i.putExtra("data", data);
startActivity(i);
}
EnterWeightActivity oncreate:
#Override
public void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
if(intent.hasExtras) {
Bundle data = intent.getExtras();
// Your data
}
}

Categories