我有一个由两个片段组成的活动。左边是侧边栏,右边是MapView(见下面的截图)。
在左侧,我有一个旋转器,将在地图上填充对象。现在,我已经用一些值硬编码,直到我弄清楚这个问题。选择微调器后,会出现一个对话框,但它是空的。知道发生了什么事吗?
这是左边片段的代码(它不是很复杂):
package android.splitdisplay;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
public class SideFragment extends Fragment {
String[] tracks = { "ObjectOne", "ObjectTwo",
"ObjectThree"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getActivity().setContentView(R.layout.sidefragment);
final Spinner s = (Spinner) this.getActivity().findViewById(
R.id.track_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this
.getActivity().getBaseContext(),
android.R.layout.simple_spinner_item, tracks);
s.setAdapter(adapter);
s.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int item = s.getSelectedItemPosition();
Toast.makeText(SideFragment.this.getActivity().getBaseContext(),
"clicked "+item, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
public void onListItemClick(ListView parent, View v, int position, long id) {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.sidefragment, container, true);
}
}
以下是SideFragment的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Tracks"
android:textColor="#adff2f"
android:textSize="30sp" />
<Spinner
android:id="@+id/track_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="@string/track_prompt"
android:spinnerMode="dialog" >
</Spinner>
</LinearLayout>
这是我点击微调器时所看到的:
以下是zip中的所有源代码:
答案 0 :(得分:2)
问题不在于Spinner,问题在于你是在给两个Spinners充气。一个在onCreateView方法中,另一个在此行中:
this.getActivity().setContentView(R.layout.sidefragment);
只有一个带有提供者的微调器...所以我建议你在onCreationView中设置所有的GUI变量,如下所示:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.sidefragment, container, true);
final Spinner s = (Spinner) v.findViewById(
R.id.track_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this
.getActivity().getBaseContext(),
android.R.layout.simple_spinner_item, tracks);
s.setAdapter(adapter);
s.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int item = s.getSelectedItemPosition();
Toast.makeText(SideFragment.this.getActivity().getBaseContext(),
"clicked "+item, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
return v;
}
并删除onCreate上的覆盖方法。
答案 1 :(得分:0)
这就是我一直做我的旋转器的方式
ArrayAdapter<CharSequence> cAdapter;
cAdapter = ArrayAdapter.createFromResource(this, R.array.colors,android.R.layout.simple_spinner_item);
int cSpinnerDD = android.R.layout.simple_spinner_dropdown_item;
cAdapter.setDropDownViewResource(cSpinnerDD);
color.setAdapter(cAdapter);
尝试一下