I'm going to POST value of spinners to the database
unfortunately, I do not know how to implement the POST method at any part of the CODE.
I'm having Problem with sending Snipper values.
public class FormActivity extends AppCompatActivity implements
....
spinnerStudy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String all_study = spinnerStudy.getItemAtPosition(spinnerStudy.getSelectedItemPosition()).toString();
if(!Objects.equals(all_study, "")){
Toast.makeText(getApplicationContext(),all_study, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String all_city = spinnerCity.getItemAtPosition(spinnerCity.getSelectedItemPosition()).toString();
if(!Objects.equals(all_city, "")) {
Toast.makeText(getApplicationContext(), all_city, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
s
pinnerUniversity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String all_university = spinnerUniversity.getItemAtPosition(spinnerUniversity.getSelectedItemPosition()).toString();
if(!Objects.equals(all_university, "")) {
Toast.makeText(getApplicationContext(), all_university, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
spinnerUnited.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String all_united = spinnerUnited.getItemAtPosition(spinnerUnited.getSelectedItemPosition()).toString();
if(!Objects.equals(all_united, "")) {
Toast.makeText(getApplicationContext(), all_united, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
button.setOnClickListener(this);
...
PLEASE Help ME...In this CASE, the post method HOW sends parameters?
How to get From Spinner a public String? Thank you ALL
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//position is spinner position
//mySpinner.getSelectedItem().toString(); IS value of position (string)
mMap.put("your Key",mySpinner.getSelectedItem().toString());
//the value add to map of volley
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Related
I want to take the value of correct answers "qCorrrect" from my spinner that is in my Adapter, to activity and make it counter from that.I have getUser_input method to take that value and pass in ButtonListener.But when i go thru debbuger"user_input" in my button Activity not showing any values
#Override
public void onBindViewHolder(#NonNull final ItemAdapter.MyHolder holder, int position) {
holder.question.setText(mList.get(position).getQuestion());
holder.category.setText(mList.get(position).getCategory());
final ArrayList<String> questions=new ArrayList<>();
qCorrect=mList.get(position).getCorrectAnswer();
String qWrong= String.valueOf(mList.get(position).getIncorrectAnswers().get(0));
String qWrong1= String.valueOf(mList.get(position).getIncorrectAnswers().get(1));
String qWrong2= String.valueOf(mList.get(position).getIncorrectAnswers().get(2));
questions.add(qCorrect);
questions.add(qWrong);
questions.add(qWrong1);
questions.add(qWrong2);
Collections.shuffle(questions);
user_input=new String[mList.size()];
final ArrayAdapter<String> arrayAdapter=
new ArrayAdapter<String>(context,android.R.layout.simple_spinner_dropdown_item,questions);
holder.spinner.setAdapter(arrayAdapter);
holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(holder.spinner.getSelectedItem().equals(qCorrect)){
user_input[position]=qCorrect;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Button finshButton=findViewById(R.id.finishBtn);
finshButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int couter=0;
String[] user_input=itemAdapter.getUser_input();
for(int i=0;i<mQuestionList.size();i++){
if(mQuestionList.get(i).getCorrectAnswer().equals(user_input[i])){
couter++;
}
}
Toast.makeText
(QuestionsActivity.this, couter + "/" + mQuestionList.size() , Toast.LENGTH_SHORT).show();
}
});
I have a spinner and when an item is selected i want to recreate the activity. But when the activity is recreated it keeps constantly recreating because the new itemSelectedListener is triggered. I have fixed the bug but i am interested in why this is happening. Thank you in advance for any insights you offer.
sp_lang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
recreate();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
You can avoid the first event with a flag in the OnItemSelectedListener.
sp_lang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
Boolean firstEventConsumed = false;
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (firstEventConsumed) {
recreate();
} else {
firstEventConsumed = true;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
EDIT
I have found a solution but it is just a workaround. It is not a definitive solution.
It has a strange behaviour when you recreate the activity. When the activity is created for the first time, it doesn't call twice the onItemSelected, but when it is recreated it is called twice.
What I have done below is to control when the item is selected by the user (handling the touch event) and when it is done by the activity.
public class MainActivity extends AppCompatActivity {
private Boolean isUserAction = false;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get the spinner
// create the adapter
spinner.setAdapter(spinnerAdapter);
spinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
isUserAction = true;
return false;
}
});
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (isUserAction) {
recreate();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// do nothing
}
});
}
}
ArrayAdapter<String> deptAdapter = new ArrayAdapter<>(Courses.this, R.layout.profile_spinner_textview, coursetype);
spinner1.setAdapter(deptAdapter);
Log.d("co_id", String.valueOf(coursetypeid));
ArrayAdapter<String> centAdapter = new ArrayAdapter<>(Courses.this, R.layout.profile_spinner_textview, centertype);
spinner2.setAdapter(centAdapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
co_id=coursetypeid.get(i);
Log.d("co_id",co_id);
loaddata();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
cent_id=centerid.get(i);
loaddata();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
while opening activity same function calling multiple times. how to avoid this. i want call same function while selecting items from spinners. please help me
This is my code :
transactionTypeSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Something changed", LENGTH_SHORT).show();
}
});
But unfortunately OnItemClickListener method not working and show this message : setOnItemClickListener cannot be used with a spinner.
How can I solve the problem?
Thanks!
You should not call OnItemClickListener for a spinner. A Spinner does not support item click events.
transactionTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
Toast.makeText(MainActivity.this, "Something changed", LENGTH_SHORT).show();
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
You need to use setOnItemSelectedListener()
Register a callback to be invoked when an item in this AdapterView has been selected.
Use
transactionTypeSpinner.setOnItemSelectedListener()
instead of
transactionTypeSpinner.setOnItemClickListener()
SAMPLE CODE
transactionTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Something changed", LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Please try with the following code hope will help you
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = parent.getItemAtPosition(position).toString();
if(selectedItem.equals("Add new category"))
{
// do your stuff
}
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
I'm working with 2 spinner in my app, and I want to do this operation in my button click for example like this in visual studio
if ((spin1.text = "bla bla bla") && (spin2. text = "ho ho ho"))
{
text1.text = result;
}
Do you have any idea to perform this operation in android? thanks.
SOLVED WITH THIS CODE!!
spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View seletedItem, int pos, long id)
{
Object item = parent.getItemAtPosition(pos);
value1 = item.toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
spin2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
{
Object item = parent.getItemAtPosition(pos);
value2 = item.toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void count(View v)
{
if(value1.equals("Depok") && value2.equals("Jakarta"))
{
hasil.setText("SUCCESS");
}
}
First you have to retrieve the text from the spinner correctly, did you do that?
If not, this is how to do it:
Assuming you called your spinner spinner.
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
String txtFromSpinner = mySpinner.getSelectedItem().toString();
When comparing, take the string from the spinner and compare it with some string.
if (txtFromSpinner.equals("bla bla bla") && txtFromSpinner2.equals("ho ho ho")
{
Log.E("Well done sir.","Comparison complete!");
//Code
}
Good luck.
Try this code once.
String item1,item2;
Textview hasil;
hasil = (TextView) findViewById(R.id.textView1);
spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View seletedItem, int pos, long id)
{
String item1 = (String) parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
spin2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
{
String item2 = (String) parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
public void count(View v)
{
if(item1.equals("Depok") && item2.equals("Jakarta"))
{
hasil.setText("berhasil");
}
}