使用Strings.xml文件

时间:2016-09-03 00:09:55

标签: java android android-layout spinner android-spinner

所以我发现的一切都是使用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();
    }
}

这是我的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>

1 个答案:

答案 0 :(得分:3)

  

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

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

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

  

以下是填充微调器的代码

其中没有填充Spinner的代码。您从布局中检索Spinner。然后你在循环中调用setId() - 我不完全清楚为什么。你有一个名为ArrayAdapter的{​​{1}}保留承诺,但你永远不会设置它。

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

adapter

它遵循与文档中相同的配方。我碰巧使用构造函数来创建/*** 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()的自定义子类中覆盖getView(),则可能还需要覆盖ArrayAdapter

  • getDropDownView()触发选择事件,而非项目点击事件