Creating a button intent to save information - java

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
}
}

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");

Android studio - How to save data selected on previous activity

This is a snippet of my code. The Textview acts as buttons and have Onclicklistner on them. When cpu1000 Textview is clicked it leads to the cpu_g1000 class for which the code is shown below.
public class Game_1000 extends AppCompatActivity implements View.OnClickListener{
private TextView cpu1000, mobo1000;
TextView cpu, mobo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_1000);
cpu1000 = (TextView) findViewById(R.id.proName_G1);
mobo1000 = (TextView) findViewById(R.id.moboName_G1);
cpu1000.setOnClickListener(this);
mobo1000.setOnClickListener(this);
cpu = (TextView) findViewById(R.id.proNameG1000);
cpu.setText(getIntent().getStringExtra("Processor"));
mobo = (TextView) findViewById(R.id.moboNameG1000);
mobo.setText(getIntent().getStringExtra("Motherboard"));
}
#Override
public void onClick(View v) {
if (v == cpu1000) {
opencpu_g1000();
}
else if (v == mobo1000) {
openmobo_g1000();
}
}
public void opencpu_g1000() {
Intent intent = new Intent(this, cpu_g1000.class);
startActivity(intent);
}
public void openmobo_g1000() {
Intent intent = new Intent(this, mobo_g1000.class);
startActivity(intent);
}
In this class, there are radio buttons. The users choose one of the choices and the choice is changed to strings. The strings are sent back to Game_1000 class. The string then gets substituted with "Choose a processor" to show the new choice. The issue I am having is when I choose a motherboard the processor choice is reverted back to "Choose a processor" and the motherboard choice shows. I need both to show at the same time.
public class cpu_g1000 extends AppCompatActivity {
Button button_save;
RadioGroup rG;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cpu_g1000);
button_save = (Button) findViewById(R.id.Save_G1_cpu);
rG = (RadioGroup) findViewById(R.id.cpu_RadioGrp);
tv = (TextView) findViewById(R.id.proNameG1000);
button_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int selected_cpu = rG.getCheckedRadioButtonId();
RadioButton selectedRadioButton = (RadioButton) findViewById(selected_cpu);
String radioValue = selectedRadioButton.getText().toString();
Intent intent = new Intent(cpu_g1000.this, Game_1000.class);
intent.putExtra("Processor", radioValue);
startActivity(intent);
}
});
}
Processor is chosen:
Motherboard is chosen:
Example of choices:
The previous choice are not recored and when a new choice is made the previous choice reverts back to "Please choose a processor" but I need both of the information to show. In my original code, I have more than just 2 choices but I shortened it to make it easier to read.
Start your second (Selection) activity for result by calling startActivityForResult(...) and then when user completes the interaction set your selected data in an Intent and pass that intent to method with desired result setResult(...) then call finish on second activity.
After finishing the second activity you will receive intent data in onActivityResult(...) method of first activity, extract that data from intent and then show it to user.
More Description
Example
Try using Shared Preference. Here's a good tutorial.
For your scenario what you have to do is save Processor and Motherboard selection result or send data form one Activity to another Activity. As after processor selection you are returning to Game_1000 Activity you can use startActivityForResult. This method send back the result to calling Activity via Intent. Thats means if you call cpu_g1000 Activity from Game_1000 Activity and after selecting Processor you can send back the result to Game_1000 Activity. Game_1000 Activity will get the result in onActivityResult method.
Example:
Start cpu_g1000 Activity with startActivityForResult from Game_1000 Activity.
public void opencpu_g1000() {
Intent intent = new Intent(this, cpu_g1000.class);
startActivityForResult(intent, PROCESSOR_SELECTION_CODE);
}
In cpu_g1000 Activity set the result to intent and send back to Game_1000 Activity
button_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int selected_cpu = rG.getCheckedRadioButtonId();
RadioButton selectedRadioButton = (RadioButton) findViewById(selected_cpu);
String radioValue = selectedRadioButton.getText().toString();
Intent resultIntent = new Intent();
// TODO Add data to send back
resultIntent.putExtra("ProcessorSelection", radioValue);
setResult(Activity.RESULT_OK, resultIntent);
// finishing this activity
finish();
}
});
In Game_1000 Activity get the value sent form cpu_g1000 Activity by overriding onActivityResult method.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (PROCESSOR_SELECTION_CODE) : {
if (resultCode == Activity.RESULT_OK) {
// TODO Extract the data returned from the child Activity.
String radioValue = data.getStringExtra("ProcessorSelection");
// save the result value and update UI
}
break;
}
}
}
For mobo_g1000 Activity use same procedure to get the result back in Game_1000 Activity. Start mobo_g1000 Activity with startActivityForResult and get the value in Game_1000 Activity's onActivityResult method. You have to add another case in onActivityResult method for mobo_g1000 Activity.
Check this and this link for better understanding how this work.

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);
}
});

sending text from editText to ListView (2 activities)

i am new to Andriod and programming. i am trying to make a to do list app, which will contain a listView, Button(add) in the main layout. when i click add i want it to go to another activity which will contain a editText and a add button. when i click the add button i want to update the list in my main activity. Now i was able to get the information from the second activity but when i try to add it in my list it over writes it.
How can i update my list as soon as my main activity appears again.
Here's what i have so far :
MainActivity class :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
add = (Button) findViewById(R.id.bAdd);
list = (ListView) findViewById(R.id.lvList);
addItems(); // i think this is the error but i dont know how to fix it.
alList = new ArrayList<String>();
aaList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, alList);
list.setAdapter(aaList);
}
public void clickAdd(View v) { //when clicking the add button(add)
Intent intent = new Intent(MainActivity.this, AddItem.class);
startActivity(intent);
}
private void addItems(){
String s = getIntent().getStringExtra("item");
aaList.add(s);
aaList.notifyDataSetChanged();
}
AddItem class :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
et = (EditText) findViewById(R.id.etAdd);
bt = (Button) findViewById(R.id.badd);
}
public void add(View v){ //when clicking the add button(bt)
edit = et.getText().toString();
Intent intent = new Intent(AddItem.this, MainActivity.class);
intent.putExtra("item", edit);
startActivity(intent);
}
Can you please tell me where and why i am going wrong? Thank You
First of all, you should use startACtivityForResult
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
Then in your second Activity
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();
And back in your fist one:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
and then, your addItem should do the trick:
private void addItems(String s){
aaList.add(s);
aaList.notifyDataSetChanged();
}

taking variable from another class

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.

Categories