2

我已经进行了很好的搜索,但似乎找不到答案。没有调用 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 中正确显示。我只是在设置为 deviceSpinner 的 OnItemSelectedListener() 中的 onItemSelected 方法存在问题 - 然后应该使用此方法填充 ListView,单击后会将用户带到新活动。谁能解释我做错了什么?

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

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

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

不打印。

谢谢

4

0 回答 0