2016-09-03 40 views
0

因此,我发现的一切都是使用strings.xml文件来填充微调框。我想要做的是根据我从JSON响应中获得的信息填充微调器。我有以下代码,我用来填充一个表,但为此进行了修改。然而,我的微调仍是空白,我没有得到任何错误,除了D/ViewRootImpl: #3 mView = null填充Spinner无Strings.xml文件

下面是代码来填充微调:

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> 

回答

3

所以一切我发现正在使用strings.xml文件来填充微调器。

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

documentation恰巧使用数组资源来填充Spinner,但您可以使用createFromResource()替换其他ArrayAdapter中的示例代码。

这里是填充微调

书中有没有代码填充一个Spinner的代码。您从布局中检索Spinner。您随后在一个循环中呼叫setId() —我不完全清楚为什么。你有一个名为adapterArrayAdapter,它拥有承诺,但你永远不会设置它。

下面是从我连接到上面的示例应用程序的活性:

/*** 
    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火灾评选活动,而不是产品Click事件

+0

我会用一个ListView,但我需要一个下拉,因为使用一个ListView将太多的页面上怎么回事。 –

+0

@JamesRobertSingleton:没关系。我的观点是'Spinner'和'ListView'很像。 'ListView'也大大受欢迎。所以,如果你无法找到一个用'Spinner'做某事的例子,找一个用ListView做同样事情的例子,它应该给你提供线索,以便你用'Spinner'实现它。 – CommonsWare

+0

该示例具有私有静态最终String [] items = {“lorem”,“ipsum”,“dolor”等等)。我不想设置这些值。 –