Using variables from a spinner to determine action by a button [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
In my program, I have 2 spinners: one shows a list of 3 numbers (as strings: one, two, and three), and another shows a list of countries: USA, Canada, Mexico, and Brazil. I also have a button that when tapped, goes to another activity/screen in android studio.
My question is, how should I use the variables returned from a spinner to go to another activity in android studio?
Here are the things I know:
The variables from the spinner 'exist' and can be used; I tried displaying them, and the TextView displays them. Here is the code that initializes the spinners:
//INIT SPINNER 1
Spinner spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.numbers, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
// INIT SPINNER 2
Spinner countries = findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countries.setAdapter(adapter2);
countries.setOnItemSelectedListener(this);
and here's the code that displays the two variables in one TextView:
TextView display = findViewById(R.id.display_spinner);
String text1 = spinner.getSelectedItem().toString();
String text2 = countries.getSelectedItem().toString();
display.setText(text1+text2);
The button, when used without any conditional statements relating to the spinners' variables, it opens a new activity. This is code for just opening a new activity by default:
//BUTTON INIT
button = (Button) findViewById(R.id.open_india);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openIndia();
}
});
Also, when using a simple conditional such as
if(4-3 == 1){
startActivity(intent);
}
the code works perfectly, and when I click the button on the emulator, I go to another activity/screen.
My question is, how do I use the variables returned from a spinner to determine whether or not to go to another activity/screen? I have the code for it, but for some reason, the button just decides not to go to the next activity.
The variables are set VARIABLE.toString(), so I know that the variable is a string, so it makes sense that it can be compared to another string.
String text2 = countries.getSelectedItem().toString();
if(text2 == "India"){
startActivity(intent);
}
Could anyone shed some light on this issue?

Change the condition to :
if(text2.equals("India")){
startActivity(intent);
}

You do not use spinner.getSelectedItem (). ToString ().
Instead of this
spinner.getSelectedItemPosition ();
spinner.getSelectedItemId ();
Use this.
public static final Int INDIA_CODE = 1;
int itemPos = countries.getSelectedItemPosition();
int itemId = countries.getSelectedItemId();
if(itemId == INDIA_CODE) {
startActivity(intent);
}

Related

How can i display 2 EditText data from array in one Activity in textview....?

When i display one textview it works, but when i display the second, the first textview disappears. Please help.
Here is my code
Intent intent = getIntent();
String[] data = intent.getStringArrayExtra(MainActivity.EXTRA_MESSAGE);
TextView name = new TextView(this);
name.setTextSize(25);
name.setText(data[0]+"\n");
name.setText("\n"+data[1]);
// Show text view
setContentView(name);
/* AlertDialog dialog = new AlertDialog.Builder(DisplayMessageActivity.this).create();
dialog.setTitle(name);
dialog.setMessage(message);
*/
// Show the Up button in the action bar.
setupActionBar();
Its overwrite the value on TextView.
So Use:
name.setText(data[0]+"\n"+data[1]);
Instead Of:
name.setText(data[0]+"\n");
name.setText("\n"+data[1]);
U can also use :
String dataStr="";
for(int i=0;i<data.length();i++)
{
dataStr=dataStr+"\n"+data[i];
}
name.setText(dataStr);
you must set text in one step
name.setText(data[0]+"\n"+data[1])
You are not appending text to your textview. You are actually replacing the text in textview. Before you do name.setText(); just add name.getText().toString();
For example: name.setText(name.getText().toString()+" My New data ");
Though it would be a good practice if you can use a StringBuilder for this. But above code should do the trick.
If you need to display whole array to textview,Just convert the array to string and set as text
name .setText(Arrays.toString(array));

Get string from listView item? [duplicate]

This question already has answers here:
How to get string from ListView?
(3 answers)
Closed 9 years ago.
I'm trying to get the string from my listView item that I click.
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
I have tried accessing it using:
TextView text = (TextView) arg1.findViewById(R.id.text1);
The id of the textView of android.R.layout.simple_list_item_1 is android:id="#android:id/text1" but according to Eclipse text1 cannot be resolved or is not a field
I'm sure I'm making a mistake accessing the default android textView in the simple list item, but I'm not sure how to access it. Any ideas?
ArrayAdapter has getItem(int position), you can rely on it to retrieve the string your a looking for. How did you get arg1?
This ended up working as the solution for me. Thanks for giving me the though user2433059
TextView textView = (TextView) arg1.findViewById(android.R.id.text1);
String text = textView.getText().toString();
text1 cannot be resolved because it is an Android resource and you won't be able to access it through your R variable.
You will fix your issue using:
TextView text = (TextView) arg1.findViewById(android.R.id.text1);
String itemText = text.getText().toString();

Sending the written text to screen in android

I am just a beginner in android and i created a textbox, user enters some text and presses send button, and i want this text to be sent to screen, like texting message or like whatsapp.
I created a textbox and a button, then i added onClick method named sendAnswer to button and it is like the following:
public void sendAnswer()
{
EditText editText = (EditText)findViewById(R.id.answer);
String editTextStr = editText.getText().toString();
Log.d(editTextStr, editTextStr);
}
where "answer" is the id of editText box. But this does not work. How can i do that?
Also, i will try to do the following in the next step: When user enters a text and sends it, an automatic message will be output to screen by the phone, it will be like chatting with the phone. Whatever user sends will be seen at the right hand side of the screen and whatever phone says will be seen at the left hand side of the screen. Can you give me some idea how to do these? Thank you
If you have installed android samples.
please look at how to use ListView and how to dynamically add items to it.
you will need a ListView for what you want to do.
have a look at
android-sdks/samples/android-8/BluetoothChat
it may help you.
check this example
Manage Items dynamically
It is like one way traffic. You are only getting the text from EditText to display the text on Screen you need to have two things:-
Have a View i.e. TextView, EditText or anything in screen to show the Text other than EditText (answer)
Set editTextStr to that using setText() to that View.
So you function should like this,
public void sendAnswer()
{
EditText editText = (EditText)findViewById(R.id.answer);
String editTextStr = null;
if(editText!=null){ //Its good to check the return value of findViewById
editTextStr = editText.getText().toString();
}else{
editTextStr = "Answer EditText doesnot exists";
}
TextView textView = (TextView)findViewById(R.id.output); //replace output with the ID of your new output view
if(textView!=null){
textView.setText(editTextStr);
}
Log.d(editTextStr, editTextStr);
}
String editTextStr;
public void sendAnswer()
{
EditText editText = (EditText)findViewById(R.id.answer);
editTextStr = editText.getText().toString();
Log.d(editTextStr, editTextStr);
calltextview();
}
public void calltextview()
{
TextView textView = (TextView)findViewById(R.id.chat);
textView.setText(editTextStr);
}
make change in the layout like your desired design.try it.
As you want to send the string to the next screen , use Intents
And to my Understanding point of view on your question.
You can use PutExtra(...)
Also You must Check this,
Click here
In the next step , You can Change the position of string in the second activity to match your requirement.
This will surely help you...

Different layouts depending on checkbox

There are 4 checkboxes and a button and as default all checkboxes are checked. Upon button click I need to test to see if any checkboxes are deselcted and if one is then the next activity (that the button launches) needs to hide a text input box. Right now I have it checking like:
if (!picCheck.isChecked()) {
intent.putExtra(PIC_CHECK, 1);
} else {
intent.putExtra(PIC_CHECK, 2);
}
This basically will send the next activity a message with a value of either 1 or 2,
being deselcted
2 being selected.
Then in the next activity I would hide textbox if that message == 1.
On an unrelated note does this look correct?
EditText editText = (EditText) findViewById(R.id.eventNameBox);
String nameMessage = editText.getText().toString();
editText = (EditText) findViewById(R.id.eventLocationBox);
String locMessage = editText.getText().toString();
editText = (EditText) findViewById(R.id.aboutEvent);
Will this convert whatever is in the eventNameBox, eventLocationBox and aboutEvent into strings? The next code after that is:
intent.putExtra(NAME_MESSAGE, nameMessage);
intent.putExtra(LOCATION_MESSAGE, locMessage);
intent.putExtra(ABOUT_MESSAGE, aboutMessage);
This will work?
If you're just trying to hide the TextView why don't you just use
textView.setVisibility(View.GONE);
this will hide the TextView

How to disable OnClickListerners

I am developing an app where the user has to match the image and corresponding name of it correctly.
My problem is when the user selects the image first and selects the wrong name it will display wrong answer and if he selects the answer it will be displayed correct answer.
The user should not have to re-select the image again
I have made the onClickListerner's null but it wont work some of my code is as follows,
txt_tag[0] = (TextView) findViewById(R.id.txt_tag1);
txt_tag[0].setOnClickListener(this);
txt_tag[0].setTypeface(tf);
txt_tag[1] = (TextView) findViewById(R.id.txt_tag2);
txt_tag[1].setOnClickListener(this);
txt_tag[1].setTypeface(tf);
txt_tag[2] = (TextView) findViewById(R.id.txt_tag3);
txt_tag[2].setOnClickListener(this);
txt_tag[2].setTypeface(tf);
txt_tag[3] = (TextView) findViewById(R.id.txt_tag4);
txt_tag[3].setOnClickListener(this);
txt_tag[3].setTypeface(tf);
img[0] = (ImageButton) findViewById(R.id.img1);
img[0].setOnClickListener(this);
img[1] = (ImageButton) findViewById(R.id.img2);
img[1].setOnClickListener(this);
img[2] = (ImageButton) findViewById(R.id.img3);
img[2].setOnClickListener(this);
img[3] = (ImageButton) findViewById(R.id.img4);
img[3].setOnClickListener(this);
btn_nxt = (Button) findViewById(R.id.btn_next);
btn_nxt.setOnClickListener(this);
and I have called an method inside that method where I have made all onClickListerner's null
txt_tag[0].setOnClickListener(null);
txt_tag[1].setOnClickListener(null);
txt_tag[2].setOnClickListener(null);
txt_tag[3].setOnClickListener(null);
img[0].setOnClickListener(null);
img[1].setOnClickListener(null);
img[2].setOnClickListener(null);
img[3].setOnClickListener(null);
Can anyone tell me where I am going wrong or any modifications I can do to it.
Thanks in advance
Try using
txt_tag[0].setClickable(false);
txt_tag[1].setClickable(false);
..
img[0].setClickable(false);
img[1].setClickable(false);
..
Your question is not that clear.. but if you want your image and text tag not clickable.. make them android:clickable="false" in xml or setClickable(false);
If I were you I would be checking that logic in a listener. So if the quiestion (if it's a quiz) is in the state "ANSWERED", don't react to event.
Your question is unclear, but I understand it as follows:
You have a bunch of ImageViews and a bunch of TextViews and a Mapping between them.
You want to be able to first select an ImageView, then a TextView. If they match, "correct answer" will be displayed somewhere, if not, "wrong answer" will be displayed
If you click on a TextView before an ImageView is selected, nothing happens
If you click on a Textview and another TextView is already selected, nothing happens
If that is correct, you can do this like this: You keep two variables
int selectedImage = -1;
int selectedText = -1;
In your OnClickListener you update their values like this:
if (source instanceof ImageViews) {
selectedImage = getArrayIndex(source); // I guess you already have a method to retrieve the index
selectedText = -1; // reset textSelection
} else {
if (selectedText < 0) {
selectedText = getArrayIndex(source);
}
}
updateAnswerTextView(); // here you check if the two selections (selectedText and selectedImage) match and display the corresponding string.
Instead, you could just iterate through the TextView array and call
setClickable(false);
on every element as soon as one is clicked. If a new image is selected, you will have to set them to clickable again.
EDIT: And I agree with Rob, you should not remove your Listeners to achieve this behaviour.
Here's my suggestion, if you want you code up specific behaviour you can use the onClickListener callback to achieve what you want.
In the listener, check the state of the image; if it is already selected and you want to ignore the event then you just exit from your callback.
I think setting the onClickListener to null is the wrong thing to do.

Categories