Java 不使用Strings.xml文件填充微调器

Java 不使用Strings.xml文件填充微调器,java,android,android-layout,spinner,android-spinner,Java,Android,Android Layout,Spinner,Android Spinner,因此,我发现的一切都是使用strings.xml文件填充微调器。我想做的是根据从JSON响应返回的信息填充微调器。我有以下用于填充表的代码,但为此修改了它。但是,我的微调器仍然为空,除了D/ViewRootImpl:#3 mView=null 以下是填充微调器的代码: public class SecondFragment extends Fragment implements APIRequestsUtil.APIRequestResponseListener { View myVie

因此,我发现的一切都是使用strings.xml文件填充微调器。我想做的是根据从JSON响应返回的信息填充微调器。我有以下用于填充表的代码,但为此修改了它。但是,我的微调器仍然为空,除了
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();
    }
}
公共类SecondFragment扩展片段实现了APIRequestsUtil.APIRequestResponseListener
{
查看我的视图;
地图驱动器;
专用阵列适配器;
@可空
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
myView=充气机。充气(右布局。第二布局,容器,错误);
APIRequestsUtil.setOnNetWorkListener(这个);
返回myView;
}
私有void populateView(){
this.getActivity().runOnUiThread(新的Runnable()){
@凌驾
公开募捐{
drives=APIRequestsUtil.getRoutes();
微调器微调器=(微调器)myView.findviewbyd(R.id.Spinner);
int driveNum=0;
对于(Map.Entry驱动器:drives.entrySet()){
TableRow tr=新的TableRow(getActivity());
Route Route=(Route)drive.getValue();
tr.setId(driveNum++);
//tr.setLayoutParams(新建ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_父级,ViewGroup.LayoutParams.WRAP_内容));
spinner.setId(driveNum);
}
}
});
}
//@凌驾
public void onFailure(请求、可丢弃、可丢弃){
}
//@凌驾
公共响应(响应){
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>

因此,我发现的一切都是使用strings.xml文件填充微调器

这演示了如何使用Java数组的内容填充
微调器

碰巧使用数组资源填充
微调器
,但您可以将其他一些
数组适配器
替换为示例代码使用
createFromResource()
创建的一个

下面是填充微调器的代码

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

以下是我在上面链接到的示例应用程序中的活动:

/***
  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("");
  }
}
/***
版权所有(c)2008-2012 Commonware,LLC
根据Apache许可证2.0版(以下简称“许可证”)获得许可;你不可以
除非符合许可证,否则请使用此文件。你可以得到一份
许可证的有效期在http://www.apache.org/licenses/LICENSE-2.0. 除非需要
根据适用法律或书面同意,根据
许可证按“原样”分发,无任何保证或条件
任何形式的,无论是明示的还是暗示的。请参阅许可证以了解具体的信息
管理许可证下的权限和限制的语言。
从《忙碌的程序员指南》到Android开发_
https://commonsware.com/Android
*/
包com.commonware.android.selection;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.Spinner;
导入android.widget.TextView;
公共类SpinnerDemo扩展活动
实现AdapterView.OnItemSelectedListener{
私有文本视图选择;
私有静态最终字符串[]项={“lorem”、“ipsum”、“dolor”,
“坐”,“艾美特”,
“奉献者”、“献祭者”、“精英”、“摩比”、“等级”,
“ligula”、“vitae”、“arcu”、“aliquet”、“mollis”,
“etiam”、“vel”、“erat”、“Placelat”、“ante”,
“波尔蒂托”、“苏打水”、“佩伦茨克”、“奥古斯”、“普卢斯”};
@凌驾
创建公共空间(捆绑冰柱){
超级冰柱;
setContentView(R.layout.main);
selection=(TextView)findViewById(R.id.selection);
微调器旋转=(微调器)findViewById(R.id.Spinner);
spin.setOnItemSelectedListener(此);
ArrayAdapter aa=新的ArrayAdapter(此,
android.R.layout.simple\u微调器\u项,
项目);
aa.setDropDownViewResource(
android.R.layout.simple\u微调器\u下拉菜单\u项);
旋转适配器(aa);
}
@凌驾
已选择公共无效项(AdapterView父项,
视图v,内部位置,长id){
selection.setText(项目[位置]);
}
@凌驾
未选择公共无效(AdapterView父级){
selection.setText(“”);
}
}
它遵循与文档中相同的配方。我碰巧使用了一个构造函数来创建
ArrayAdapter
实例,并将其包装在作为模型数据的
字符串[]
周围。尚不清楚您想要在
微调器中拥有什么,但您可以拥有
阵列适配器(如代码所示)或
阵列适配器或其他任何东西

有关更复杂的场景,请查看
ListView
示例<代码>微调器的工作原理几乎相同,只有两个实质性的变化:

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

  • 微调器
    触发选择事件,而不是项目单击事件

因此,我发现的一切都是使用strings.xml文件来填充