2016-09-03 76 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」等等)。我不想設置這些值。 –