繁体   English   中英

在不使用Strings.xml文件的情况下填充Spinner

[英]Populate Spinner WITHOUT Strings.xml file

因此,我发现的所有内容都是使用strings.xml文件填充微调器。 我要做的是根据从JSON响应中获得的信息填充微调器。 我有以下用于填充表的代码,但为此进行了修改。 但是,我的微调器仍然是空白,除了D/ViewRootImpl: #3 mView = null之外,我没有任何错误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>

因此,我发现的所有内容都是使用strings.xml文件填充微调器。

这是一个示例应用程序 ,演示了如何使用Java数组的内容填充Spinner

文档碰巧使用了一种数组资源来填充Spinner ,但是您可以将其他一些ArrayAdapter替换为其示例代码使用createFromResource()创建的createFromResource()

这是填充微调器的代码

那里没有填充Spinner代码。 您从布局中检索Spinner 然后,您可以在循环中对其调用setId() -我尚不清楚原因。 您有一个名为adapterArrayAdapter ,它持有Promise,但从未设置过。

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

/***
  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触发选择事件,而不是项目单击事件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM