gpt4 book ai didi

java - Spinner onItemSelectedListener 不起作用?

转载 作者:行者123 更新时间:2023-12-02 09:06:25 25 4
gpt4 key购买 nike

我使用 Android 4.4,并且在实现微调器时遇到问题。问题:从列表中选择项目时,微调器未设置所选项目。

根据this我尝试过的数百个其他帖子似乎没有解决方案对我有用,我是否错过了一些我没有意识到的非常重要的事情?。

程序应该做什么:我确实想扫描 wifi 网络,并希望用户从微调器中选择 wifi 连接并进行设置。

现在情况如何:微调器显示可用的 wifi 网络,但当我单击一个网络时,它未被选中。

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>

重要的部分在activity和layout.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>

还在调试器中我注意到监听器没有被调用...

最佳答案

使用

 spinner.setAdapter(adapter);

当您将适配器设置为微调器时。我查过了。

注意 - 您的适配器“adp”有问题。我不知道你为什么使用它。

关于java - Spinner onItemSelectedListener 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59785822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com