问题描述
因此,我发现的所有内容都在使用strings.xml文件填充微调器.我要做的是根据从JSON响应中获得的信息填充微调器.我有以下用于填充表的代码,但为此进行了修改.但是,我的微调框仍然空白,除了D/ViewRootImpl: #3 mView = null
So everything I have found is using the strings.xml file to populate a spinner. What I want to do is populate a spinner based on information I get back from a JSON response. I have the following code that I used to populate a table but modified it for this. However, my spinner is still blank and I get no errors besides D/ViewRootImpl: #3 mView = null
以下是填充微调器的代码:
Here is the code to populate the spinner:
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>
推荐答案
此处是一个示例应用程序,该示例演示了如何填充Spinner
以及Java数组的内容.
Here is a sample app that demonstrates populating a Spinner
with the contents of a Java array.
文档恰好使用数组资源进行填充Spinner
,但您可以将其他ArrayAdapter
换成他们的示例代码使用createFromResource()
创建的一个.
The documentation happens to use an array resource for populating the Spinner
, but you could swap in some other ArrayAdapter
for the one their sample code creates using createFromResource()
.
其中没有代码填充Spinner
.您从布局中检索Spinner
.然后,您可以在循环中对其调用setId()
—我还不完全清楚为什么.您有一个名为adapter
的ArrayAdapter
可以保存承诺,但是您从未设置过它.
There is no code in there that populates a Spinner
. You retrieve a Spinner
from your layout. You then call setId()
on it in a loop — I am not completely clear why. You have an ArrayAdapter
named adapter
that holds promise, but you never set it up.
这是我上面链接到的示例应用程序中的活动:
Here is the activity from the sample app that I linked to above:
/***
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>
或其他任何内容.
It follow the same recipe as in the documentation. I happen to use a constructor to create the ArrayAdapter
instance, wrapping it around the String[]
that is my model data. It is unclear what you want to have in your Spinner
, but you could have an ArrayAdapter<Integer>
(as is shown in your code) or ArrayAdapter<Route>
or whatever.
对于更复杂的情况,请查找ListView
示例. Spinner
的工作原理几乎相同,只有两个实质性的变化:
For more complex scenarios, look for ListView
examples. Spinner
works nearly identically, with only two substantial changes:
-
您需要提供两个布局资源(请参见上面的代码中的
setDropDownViewResource()
).而且,如果您在ArrayAdapter
的自定义子类中覆盖getView()
,则可能还需要覆盖getDropDownView()
.
You need to provide two layout resources (see
setDropDownViewResource()
in the code above). And, if you overridegetView()
in a custom subclass ofArrayAdapter
, probably you also need to overridegetDropDownView()
.
Spinner
触发选择事件,而不是项目点击事件
Spinner
fires selection events, not item click events
这篇关于在不使用Strings.xml文件的情况下填充Spinner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!