簡體   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