android Spinner后跟ListView - onItemSelected未调用

时间:2013-02-05 20:24:08

标签: android spinner

我有一个很好的搜索,似乎无法找到答案。未调用onItemSelected方法。这是我的活动的onCreate方法中的代码:

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

    try {
    // if network present.. TODO

    // Load Devices
    deviceList = new ArrayList<Device>();
    DataAsyncTask loadDevices = new DataAsyncTask(0);
    loadDevices.execute(someUrl);
    List<Device> deviceListNames = deviceList;
    final ListView tripListView = (ListView)findViewById(R.id.trip_list);
    Spinner deviceSpinner = (Spinner) findViewById(R.id.select_device_spinner);
    ArrayAdapter<Device> deviceAdapter = new ArrayAdapter<Device>(this, android.R.layout.simple_spinner_dropdown_item, deviceListNames);
    deviceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    deviceSpinner.setAdapter(deviceAdapter);
    OnItemSelectedListener spinnerListener = new OnItemSelectedListener() {


        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                      int i, long id) {

            Log.d(TAG, "select is: " + parent.getSelectedItem().toString());   

            DataAsyncTask loadTrips = new DataAsyncTask(1);
            loadTrips.execute(someUrl);
            ArrayAdapter<Trip> tripAdapter = new ArrayAdapter<Trip>(getApplication(),
                    android.R.layout.simple_list_item_1, tripList);
            tripListView.setAdapter(tripAdapter);
            tripListView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int i, long id) {

                    Log.d(TAG, parent.getSelectedItem().toString());

                    Intent intent = new Intent(getApplication(), TripSummary.class);
                    //put value of tripId to intent TODO
                    startActivity(intent);
                }

            });



         }

         @Override
         public void onNothingSelected(AdapterView<?> parent) {
               // TODO
               Log.d(TAG, "nothing selected");
         }

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

}

这是活动的布局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=".ViewTrips" >

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

<ListView
    android:id="@+id/trip_list"
    android:layout_below="@id/select_device_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/trips"
    android:clickable="true" />

</RelativeLayout>

它的值已填充并在Spinner中正确显示。我只是在OnItemSelectedListener()中设置为deviceSpinner的onItemSelected方法存在问题 - 然后该方法应该填充ListView,其中一旦点击就会将用户带到一个新活动。谁能解释我做错了什么?

我也尝试为活动实现OnItemSelectedListener,这也不起作用。

注意:LogCat中没有错误报告,

Log.d(TAG, "select is: " + parent.getSelectedItem().toString());

不打印。

由于

0 个答案:

没有答案