Я использую Android 4.4
и испытываю проблемы implementing a spinner
. Проблема: Spinner не устанавливает выбранный элемент, когда он выбран из списка.
в соответствии с this и преследует другие посты, которые я пробовал. Кажется, решение не работает, мне не хватает чего-то, я что-то упустил очень важно, что я просто не понимаю?.
Что должна делать программа: я хочу сканировать сети Wi-Fi и хочу, чтобы пользователь выбрал соединение Wi-Fi из счетчика и установил его.
Как это сейчас: Spinner показывает доступные сети Wi-Fi, но когда я нажимаю на одну из них, она не выбирается.
public class SetupActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener
{
private WifiManager wifiManager;
private ListView listView;
private Button buttonScan;
private int size = 0;
private List<ScanResult> results;
private ArrayList<String> arrayList = new ArrayList<>();
private ArrayAdapter adapter;
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_setup );
buttonScan = findViewById(R.id.btnScan);
buttonScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
scanWifi();
}
});
spinner = findViewById(R.id.spinnerSSID);
listView = findViewById(R.id.wifiList);
wifiManager = (WifiManager) getApplicationContext().getSystemService( Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
Toast.makeText(this, "WiFi is disabled ... We need to enable it", Toast.LENGTH_LONG).show();
wifiManager.setWifiEnabled(true);
}
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(adapter);
scanWifi();
ArrayAdapter<String> adp = new ArrayAdapter<String>(SetupActivity.this,
android.R.layout.simple_spinner_item, arrayList );
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
spinner.setOnItemSelectedListener(this);
}
private void scanWifi() {
arrayList.clear();
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifiManager.startScan();
Toast.makeText(this, "Scanning WiFi ...", Toast.LENGTH_SHORT).show();
}
BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
results = wifiManager.getScanResults();
unregisterReceiver(this);
for (ScanResult scanResult : results) {
arrayList.add(scanResult.SSID + " - " + scanResult.capabilities);
adapter.notifyDataSetChanged();
}
}
};
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position);
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Здесь xml из макета:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
android:tint="?attr/colorControlNormal"
tools:context=".SetupActivity">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DeviceID: 1234abcd"
app:layout_constraintBottom_toTopOf="@+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Setup"
android:textSize="36sp"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Content Installed!"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose which Setup"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="240dp"
android:layout_marginTop="196dp"
android:text="Local"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="196dp"
android:layout_marginEnd="240dp"
android:text="Online"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="432dp"
android:layout_height="297dp"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView11"
app:srcCompat="@drawable/usbstick" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="11dp"
android:layout_height="252dp"
android:layout_marginTop="150dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4"
app:srcCompat="@color/colorPrimary" />
<EditText
android:id="@+id/etxtContentId"
android:layout_width="445dp"
android:layout_height="44dp"
android:layout_marginTop="63dp"
android:layout_marginEnd="3dp"
android:ems="10"
android:hint="Content ID"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView6"
app:layout_constraintTop_toBottomOf="@+id/textView12" />
<EditText
android:id="@+id/etxtWifiPassword"
android:layout_width="445dp"
android:layout_height="44dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="3dp"
android:ems="10"
android:hint="Wifi Password"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/imageView6"
app:layout_constraintTop_toBottomOf="@+id/spinnerSSID" />
<Spinner
android:id="@+id/spinnerSSID"
android:layout_width="447dp"
android:layout_height="44dp"
android:layout_marginStart="1dp"
android:layout_marginTop="38dp"
android:spinnerMode="dropdown"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView6"
app:layout_constraintTop_toBottomOf="@+id/etxtContentId" />
<Button
android:id="@+id/btnTestConnection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="24dp"
android:text="Test Connection"
app:layout_constraintStart_toEndOf="@+id/imageView6"
app:layout_constraintTop_toBottomOf="@+id/etxtWifiPassword" />
<Button
android:id="@+id/btnWifiConnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="24dp"
android:text="Connect"
app:layout_constraintStart_toEndOf="@+id/btnTestConnection"
app:layout_constraintTop_toBottomOf="@+id/etxtWifiPassword" />
<Button
android:id="@+id/btnFinish"
android:layout_width="392dp"
android:layout_height="45dp"
android:layout_marginStart="13dp"
android:layout_marginTop="9dp"
android:layout_marginEnd="43dp"
android:text="Finish and perform online update"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView6"
app:layout_constraintTop_toBottomOf="@+id/btnTestConnection" />
<Button
android:id="@+id/btnScan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="27dp"
android:layout_marginTop="25dp"
android:text="Scan"
app:layout_constraintStart_toEndOf="@+id/btnWifiConnect"
app:layout_constraintTop_toBottomOf="@+id/etxtWifiPassword" />
<ListView
android:id="@+id/wifiList"
android:layout_width="412dp"
android:layout_height="99dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Важные части есть в деятельности и макете. xml
private Spinner spinner;
...
spinner = findViewById(R.id.spinnerSSID);
...
ArrayAdapter<String> adp = new ArrayAdapter<String>(SetupActivity.this,
android.R.layout.simple_spinner_item, arrayList );
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
spinner.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position);
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
parent.getItemAtPosition(0);
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(0).toString(),
Toast.LENGTH_SHORT).show();
}
} );
И в XML:
<Spinner
android:id="@+id/spinnerSSID"
android:layout_width="447dp"
android:layout_height="44dp"
android:layout_marginStart="1dp"
android:layout_marginTop="38dp"
android:spinnerMode="dropdown"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView6"
app:layout_constraintTop_toBottomOf="@+id/etxtContentId" />
Спасибо за любую помощь заранее!
РЕДАКТИРОВАТЬ: я заметил эту ошибку в консоли:
E/dalvikvm: Could not find class 'android.widget.ThemedSpinnerAdapter', referenced from method androidx.appcompat.widget.AppCompatSpinner$DropDownAdapter.<init>
Также в отладчике я заметил, что слушатель не вызывается ...