在没有 Strings.xml 文件的情况下填充 Spinner

Populate Spinner WITHOUT Strings.xml file

所以我发现的所有内容都是使用 strings.xml 文件来填充微调器。我想要做的是根据我从 JSON 响应中获得的信息填充微调器。我有以下用于填充 table 的代码,但为此对其进行了修改。但是,我的微调器仍然是空白的,除了 D/ViewRootImpl: #3 mView = null

之外我没有收到任何错误

这是填充微调器的代码:

public class SecondFragment extends Fragment implements APIRequestsUtil.APIRequestResponseListener
{
    View myView;
    Map<String, Route> drives;
    private ArrayAdapter<Integer> adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.second_layout, container, false);
        APIRequestsUtil.setOnNetWorkListener(this);
        return myView;
    }

    private void populateView() {
        this.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                drives = APIRequestsUtil.getRoutes();

                Spinner spinner = (Spinner) myView.findViewById(R.id.spinner);
                int driveNum = 0;
                for (Map.Entry drive : drives.entrySet()) {
                    TableRow tr = new TableRow(getActivity());
                    Route route = (Route) drive.getValue();
                    tr.setId(driveNum++);
                    //tr.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));



                    spinner.setId(driveNum);

                }
            }
        });

    }

    //@Override
    public void onFailure(Request request, Throwable throwable) {

    }

    //@Override
    public void onResponse(Response response) {
        populateView();
    }
}

这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:id="@+id/spinner"/>
</RelativeLayout>

So everything I have found is using the strings.xml file to populate a spinner.

Here is a sample app 演示了用 Java 数组的内容填充 Spinner

documentation 恰好使用数组资源来填充 Spinner,但您可以将其他一些 ArrayAdapter 换成他们的示例代码使用 [=14= 创建的资源].

Here is the code to populate the spinner

其中没有填充 Spinner 的代码。您从布局中检索 Spinner。然后你在循环中调用 setId() ——我不完全清楚为什么。您有一个名为 adapterArrayAdapter 承诺,但您从未设置它。

这是我在上面链接的示例应用程序中的 activity:

/***
  Copyright (c) 2008-2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain   a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    https://commonsware.com/Android
*/

package com.commonsware.android.selection;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class SpinnerDemo extends Activity
  implements AdapterView.OnItemSelectedListener {
  private TextView selection;
  private static final String[] items={"lorem", "ipsum", "dolor",
          "sit", "amet",
          "consectetuer", "adipiscing", "elit", "morbi", "vel",
          "ligula", "vitae", "arcu", "aliquet", "mollis",
          "etiam", "vel", "erat", "placerat", "ante",
          "porttitor", "sodales", "pellentesque", "augue", "purus"};

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    selection=(TextView)findViewById(R.id.selection);

    Spinner spin=(Spinner)findViewById(R.id.spinner);
    spin.setOnItemSelectedListener(this);

    ArrayAdapter<String> aa=new ArrayAdapter<String>(this,
                              android.R.layout.simple_spinner_item,
                              items);

    aa.setDropDownViewResource(
      android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(aa);
  }

  @Override
  public void onItemSelected(AdapterView<?> parent,
                                View v, int position, long id) {
    selection.setText(items[position]);
  }

  @Override
  public void onNothingSelected(AdapterView<?> parent) {
    selection.setText("");
  }
}

它遵循与文档中相同的方法。我碰巧使用构造函数来创建 ArrayAdapter 实例,将其包装在作为我的模型数据的 String[] 周围。目前还不清楚你想在你的 Spinner 中有什么,但你可以有一个 ArrayAdapter<Integer> (如你的代码中所示)或 ArrayAdapter<Route> 或其他任何东西。

对于更复杂的场景,请查找 ListView 个示例。 Spinner 工作方式几乎相同,只有两个重大变化:

  • 您需要提供两个布局资源(参见上面代码中的setDropDownViewResource())。而且,如果您在 ArrayAdapter 的自定义子类中覆盖 getView(),您可能还需要覆盖 getDropDownView().

  • Spinner 触发选择事件,而不是项目点击事件