V
V
Vladimir Abramov2020-08-25 19:50:40
Android
Vladimir Abramov, 2020-08-25 19:50:40

How to connect two or more spinners in one Activity?

Good afternoon! Trying to deal with a spinner? I looked at the lessons and one seemed to be able to connect and display the selected in the TextView, but with the second spinner it doesn’t work either. The list is displayed, but I don’t understand how to display the selected in the TextView. Can someone suggest?

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto "
xmlns:tools="http:// schemas.android.com/tools "
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

android:id="@+id/spBB"
android:layout_width="50dp"
android:layout_marginTop="116dp"
android:entries="@array/big_blinds"
android:spinnerMode="dropdown"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.048"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintOf_toTop_toTop_toTop parent" />

android:id="@+id/tvEnterBB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="57dp"
android:layout_marginBottom="38dp"
android:text="@string/enterBB"
app:layout_constraintBottom_toTopOf="@+id/spBB"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.092"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="1" app :
layout_constraintVertical_bias.0 /

id="@+id/textCard1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="116dp"
android:layout_marginEnd="264dp"
android:textSize="60sp"
android:textStyle="bold "
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

android:id="@+id/spPos"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="116dp"
android:layout_marginEnd="136dp"
android: entries="@array/position1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

android:id="@+id/tvEnterPos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android :layout_marginTop="57dp"
android:layout_marginEnd="80dp"
android:text="@string/tvEnterPos"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

android:id="@+id/textCard2"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="116dp"
android: layout_marginEnd="52dp"
android:textSize="60sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />



MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android. view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
Spinner spBB, spPos;
String[] position1 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"};
String[] position = {"21", "22", "23", "31", "32", "33", "35", "40", "41"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final TextView textCard1 = findViewById(R.id.textCard1);
final TextView textCard2 = findViewById(R.id.textCard2);
spBB = findViewById(R.id.spBB);
spPos = findViewById(R.id.spPos);

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, position1);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spBB.setAdapter(adapter);

AdapterView.OnItemSelectedListener itemSelectedListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

// Get the selected object
String item = (String) parent.getItemAtPosition(position);
textCard1.setText(item);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
};
spBB.setOnItemSelectedListener(itemSelectedListener);

}

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Vodakov, 2020-08-25
@vovan55

Well, you described an adapter for one spinner, but not for another.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question