从Spinner中检索项目并放置一些条件

时间:2012-03-01 10:03:09

标签: java android

我首先有两个数组用于小时,第二个用于分钟,这是我的数组在string.xml中声明它

   ` <string-array name="feedbacktypelist">
    <item>@string/hr0</item>  
    <item>@string/hr1</item>  
    <item>@string/hr2</item>  

</string-array>

<string-array name="array2">
    <item>@string/min5</item>  
    <item>@string/min10</item>  
    <item>@string/min15</item>  
    <item>@string/min20</item>  
    <item>@string/min25</item>  
    <item>@string/min30</item>
    <item>@string/min35</item>  
    <item>@string/min40</item>  
    <item>@string/min45</item>
    <item>@string/min50</item>  
    <item>@string/min55</item>  
    <item>@string/min59</item>


</string-array>`

这是我在java中的代码

  package lmp.app.pkg;

  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.content.Intent;
  import android.database.Cursor;
  import android.graphics.Bitmap;
  import android.net.Uri;
  import android.os.Bundle;
  import android.provider.MediaStore;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemSelectedListener;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ImageView;
  import android.widget.RadioGroup;
  import android.widget.RadioGroup.OnCheckedChangeListener;
  import android.widget.Spinner;
  import android.widget.TextView;
  import android.widget.Toast;

  public class CreateNewForm extends Activity implements OnItemSelectedListener  {


Button Browse;
ImageView  CasePic;
Spinner CaseDurationH, CaseDurationM;
TextView tesst;
RadioGroup GenderSelection;
EditText CaseName, CaseClothes, CaseMoreInfo, CaseAge;
Button Next;
//For Browsering Picture 
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;

@Override
protected void onCreate(Bundle savedInstanceState) 
{

    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_new_form);

    //To Call initializer Function 
    initializer();

    //j list

    // 1-For Uploading Picture
    Browse.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
             // in onCreate or any event where your want the user to
            // select a file
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,
                    "Select Picture"), SELECT_PICTURE);
        }
    });

    // 1-Name
    final MyCase case1 = new MyCase();
    case1.setName(CaseName.getText().toString());

    // 2-Gender For Group Radio
    GenderSelection.clearCheck();
    GenderSelection.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(RadioGroup group, int checkedId) {
             switch (checkedId) {
            case R.id.radio0:
                case1.setGender("Male");
                break;
            case R.id.radio1:
                case1.setGender("Female");
                break;
            default:
                break;
            }

        }
    });

    //3-Age

     String age = CaseAge.getText().toString();


    /*int tstnum =case1.getAge();
    tesst.setText(tstnum); */

     //4-Duration Time
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.feedbacktypelist, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    CaseDurationH.setAdapter(adapter);


    //5-Case Clothes
    case1.setClothes(CaseClothes.getText().toString());

    //6-Case More Information
    case1.setMoreInfo(CaseMoreInfo.getText().toString());

    //Move to 2nd form page
    Next= (Button)findViewById(R.id.Next2);
    Next.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) { 
            switch (v.getId()) { 
            case R.id.Next2:
                try
                {
                     Intent k = new Intent(CreateNewForm.this, CreateNewForm_2.class);

                     startActivity(k);

                }catch(Exception e){

                }

                break;

            }
        }
            });



  //Spinner
    CaseDurationH.setOnItemSelectedListener(new OnItemSelectedListener() {
        int i =CaseDurationH.getSelectedItemPosition();
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            int i = CaseDurationH.getSelectedItemPosition();

            if(i==2){
                CaseDurationM.setEnabled(false);
            }
            String str = parent.getSelectedItem().toString();

            if(str.equals("hr0"))
                    {

                }

            if(str.equals("hr1"))
                    {

                }


            if(str.equals("hr2"))
                    {
                CaseDurationM.setEnabled(false);  
                }



            }
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        } });

}


// To initialize the variables 
private void initializer() {
    // TODO Auto-generated method stub
    //This information will be filled by a user
            //CasePic = (ImageView) findViewById(R.id.imageView1);
            CaseName= (EditText) findViewById(R.id.caseNm);
            GenderSelection= (RadioGroup) findViewById(R.id.radioGroup1);
            CaseAge= (EditText) findViewById(R.id.caseaage);
            tesst= (TextView) findViewById(R.id.textView8);
            CaseDurationH= (Spinner) findViewById(R.id.Shr);
            CaseDurationM= (Spinner) findViewById(R.id.Smin);
            CaseClothes= (EditText) findViewById(R.id.caseClothes);
            CaseMoreInfo= (EditText) findViewById(R.id.caseMrInfo);
            CasePic = (ImageView) findViewById(R.id.casepic);
            Browse = (Button) findViewById(R.id.browseCasePic);
}


//For Uploading Picture
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
        }
    }
}

//For Uploading Picture
public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}


public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub

}


public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}


}

我想: 1-在微调器中检索用户选择它的项目,而不是位置 2 - 如果用户从spinner1中选择项目“hr02”,则spinner2将禁用

感谢您的帮助,StackOverFlow现在成为您的英雄! :“)

2 个答案:

答案 0 :(得分:1)

使用此

String str = parent.getSelectedItem().toString();

if(str.equals("hr2")
{
    spinner2.setEnabled(false);  
}

答案 1 :(得分:0)

试试这个:

String str = parent.getSelectedItem().toString();
if (str.equals("hr0")){
        //retrieve the item as string
}
if (str.equals("hr1")){
        //retrieve the item as string
}
if (str.equals("hr2")){
        //retrieve the item as string
        //make the 2nd spinner disable
}