Добрый день! Пытаюсь разобраться со spinner? посмотрел уроки и один вроде получилось подключить и отобразить выбранное в TextView, а вот со вторым спиннером ни как не получается. Список выводится, а как выбранное отобразить в TextView не понимаю. Может кто то подсказать?
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_height="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_constraintTop_toTopOf="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="parent"
app:layout_constraintVertical_bias="1.0" />
android: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) {
// Получаем выбранный объект
String item = (String) parent.getItemAtPosition(position);
textCard1.setText(item);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
spBB.setOnItemSelectedListener(itemSelectedListener);
}
}