我正在尝试为String数组设置TextAdapter。基本上,这是一个下拉菜单,其中包含与用户要购买的商品的大小相关的字符串列表。
private void SetupSpinner(String[] drawables) {
Spinner spinner = (Spinner) view.findViewById(R.id.spinner_sizes);
spinner.setAdapter(new ImageAdapter(getActivity(), R.layout.row, drawables));
}
public class ImageAdapter extends ArrayAdapter<String> {
private String[] drawables;
public ImageAdapter(Context context, int textViewResourceId, String[] drawables) {
super(context, textViewResourceId);
this.drawables = drawables;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = new TextView(getContext());
view.setText(Integer.parseInt(drawables[position]));
return view;
}
}
我有它与可绘制对象一起工作,但这效率很低。我不想为每个尺寸选项制作图形。无论如何,所有帮助将不胜感激!
最佳答案
创建一个XML custom_spinner
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/text_main_seen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/sub_text_seen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
然后将微调器放在需要的主xml中
<Spinner
android:id="@+id/spinner_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100px"
android:drawSelectorOnTop="true" />
那你上课
public class SpinnerCustomizationActivity extends Activity {
String[] spinnerValues = { "Blur", "NFS", "Burnout","GTA IV", "Racing", };
String[] spinnerSubs = { "Ultimate Game", "Need for Speed", "Ulimate Racing", "Rockstar Games", "Thunder Bolt" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner) findViewById(R.id.spinner_show);
mySpinner.setAdapter(new MyAdapter(this, R.layout.custom_spinner, spinnerValues)); }
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context ctx, int txtViewResourceId, String[] objects)
{
super(ctx, txtViewResourceId, objects); }
@Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) { return getCustomView(position, cnvtView, prnt); }
@Override
public View getView(int pos, View cnvtView, ViewGroup prnt)
{
return getCustomView(pos, cnvtView, prnt);
}
public View getCustomView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = getLayoutInflater();
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false);
TextView main_text = (TextView)mySpinner.findViewById(R.id.text_main_seen);
main_text.setText(spinnerValues[position]);
TextView subSpinner = (TextView)mySpinner.findViewById(R.id.sub_text_seen);
subSpinner.setText(spinnerSubs[position]);
return mySpinner; }
}
}
关于java - 设置TextSpinner适配器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33794548/