从Spinner获取价值

时间:2014-03-26 07:27:57

标签: android android-layout android-spinner

我正在创建一个应用程序,上传包含各种信息的图像。我可以从手机图库中获取图像,我制作了一种滑块,我可以填写表格。在表单中我使用了spinner和textView,我的布局是这样的:

details.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

layout_fullscreen_image.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" >

    <ImageView
        android:id="@+id/imgDisplay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="100dp"
        android:scaleType="fitCenter" />

    <Spinner
        android:id="@+id/sizes"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="69dp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/sizes"
        android:layout_toRightOf="@+id/sizes"
        android:text="1"
        android:inputType="number"
        android:digits="0123456789"
        android:ems="10" />

    <TextView
        android:id="@+id/tvSizes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/sizes"
        android:layout_alignParentLeft="true"
        android:text="Size" />

    <TextView
        android:id="@+id/tvPieces"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/sizes"
        android:layout_alignLeft="@+id/editText1"
        android:text="Piece/s" />

</RelativeLayout>

和一个操作栏:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_add_to_cart"
        android:icon="@drawable/cart"
        android:title="@string/cart"
        android:showAsAction="ifRoom"/>

    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" />


</menu>

我有一个ImageAdapter,使用此方法将图像显示为layout_fullscreen_image.xml的滑块:

@Override
public Object instantiateItem(ViewGroup container, int position) {
    ImageView imgDisplay;
    Button btnClose;

    sizes = new ArrayList<String>();
    sizes.add("3x4");
    sizes.add("4x6");
    sizes.add("5x8");

    inflater = (LayoutInflater) _activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
            false);

    imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgDisplay);

    Spinner mSpinner = (Spinner) viewLayout.findViewById(R.id.sizes);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this._activity, android.R.layout.simple_spinner_item, sizes);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mSpinner.setAdapter(adapter);


    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
    imgDisplay.setImageBitmap(bitmap);

    ((ViewPager) container).addView(viewLayout);

    return viewLayout;
}

和一个主类Details.java

public class Details extends Activity{

    private ArrayList<String> selectedItems;
    private FullScreenImageAdapter adapter;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.details);
        Bundle b = getIntent().getExtras();
        selectedItems = (ArrayList<String>)b.getStringArrayList("selectedItems");

        Log.d("selectedItems",selectedItems.toString());

        viewPager = (ViewPager) findViewById(R.id.pager);

        Intent i = getIntent();
        int position = i.getIntExtra("position", 0);

        adapter = new FullScreenImageAdapter(Details.this, selectedItems);
        viewPager.setAdapter(adapter);

        viewPager.setCurrentItem(position);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.details_action_bar, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()){
            case R.id.action_add_to_cart:
                UploadImages();
                break;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }


    public void UploadImages(){
        Spinner spinner = (Spinner) findViewById(R.id.sizes);
        String temp = spinner.getSelectedItem().toString();     
        Toast.makeText(getApplicationContext(), temp, Toast.LENGTH_LONG).show();
    }

}

当我选择多个图像时,它为我提供了多个微调器和textView,我试图从微调器和textView中检索值,但它只给出了我的第一个值。如何获取Spinner和textView

中设置的所有值

1 个答案:

答案 0 :(得分:0)

你说的是,你得到一个只有一个项目的旋转器,r8?我做了类似的代码。但改变是,而不是:

sizes = new ArrayList<String>();
sizes.add("3x4");
sizes.add("4x6");
sizes.add("5x8");

我在strings.xml中添加了字符串数组。你能试试吗?