Android - 微调器,onItemSelected(...) 未被调用

标签 android spinner

代码如下:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;
import com.project.locationapp.model.Device;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;


public class SelectDevice extends Activity implements OnItemSelectedListener {

private Spinner deviceSpinner;
private List<Device> deviceList;
private ArrayAdapter<Device> deviceAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select_device);
    deviceList = new ArrayList<Device>();


    try {
        deviceSpinner = (Spinner) findViewById(R.id.select_device_spinner);
        deviceAdapter = new DeviceAdapter(this, android.R.layout.simple_spinner_dropdown_item, deviceList);
        deviceSpinner.setAdapter(deviceAdapter);
        deviceSpinner.setOnItemSelectedListener(this);

        DataAsyncTask loadDevices = new DataAsyncTask();
        loadDevices.execute(new String[] { WebServiceURL.WEB_SERVICE + WebServiceURL.DEVICES + WebServiceURL.ALL });




    } catch (Exception e){
        Log.e(TAG, e.getLocalizedMessage(), e);
    }
}

@Override
public void onItemSelected(AdapterView<?> a, View v, int position,
        long id) {

    Log.d(TAG, "called!");  
    Intent intent = new Intent(this, ViewTrips.class);
    intent.putExtra("device_id", deviceAdapter.getItem(position).getId());
    startActivity(intent);

}
}

设备适配器类:

import java.util.List;
import com.project.locationapp.model.Device;
import android.content.Context;
import android.widget.ArrayAdapter;

public class DeviceAdapter extends ArrayAdapter<Device> {

public DeviceAdapter(Context context, int textViewResourceId,
        List<Device> objects) {
    super(context, textViewResourceId, objects);
}
}

Activity 布局 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SelectDevice" >


<TextView
    android:id="@+id/instructions_device_select"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="@string/select_device_instructions"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="15dp"
    android:layout_marginLeft="15dp" />

<Button
    android:id="@+id/start_service_button"
    android:layout_below="@id/instructions_device_select"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="thisDevice"
    android:text="@string/this_device"
    android:layout_marginTop="10dp"
    android:layout_marginRight="40dp"
    android:layout_marginLeft="40dp" />

<Spinner
    android:id="@+id/select_device_spinner"
    android:layout_below="@id/start_service_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/select_device"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="5dp"
    android:drawSelectorOnTop = "true" />

</RelativeLayout>

我正在等待它正常工作,然后我将进一步自定义 DeviceAdapter。

Activity 实现了 OnItemSelectedListener,因此覆盖了 onItemSelected(...),它根本没有被调用。 LogCat 中没有错误。 Spinner 是在 Activity 的布局 xml 中定义的,可以很好地显示和填充。解决这个问题的任何建议都会很棒。

谢谢。

最佳答案

可能是微调器布局,我可以看到您没有为适配器设置下拉 View 。 您可以尝试像这样初始化微调器吗:

deviceAdapter = new DeviceAdapter(this, android.R.layout.simple_spinner_item, deviceList);
deviceSpinner.setAdapter(deviceAdapter);
deviceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
deviceSpinner.setOnItemSelectedListener(this);

关于Android - 微调器,onItemSelected(...) 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15110369/

相关文章:

android - 如何在翻译动画android期间设置透明背景?

AsyncTask 运行时的 Android 闪屏

java - findViewById 与 getIdentifier 不起作用

android - 如何在android中给定的两个范围之间生成时间?

java - 当我尝试从 GitHub 存储库构建和运行代码时出现 Gradle 同步问题

java - Android 中的字符串下标和上标

android - WebRTC Android 正确挂断

android - 更改微调器的背景和项目颜色

java - onItemSelected 的问题

JavaFX 相当于 Swing 的 JSpinner?