将所选值的索引值从微调器传递给asynctask

时间:2015-02-02 07:16:38

标签: android android-asynctask spinner

在注册过程中,当用户从微调器中选择标题时,应自动选择其数组索引值并发送到asynctask。我想在下面显示的代码中发送数组索引值(位置)。

new RegisterAsyncTask(getApplicationContext()).execute(TitleName,CountryName);

该位置的原始值位于onItemSelected()内,我想知道如何将其传递给onclick()

这是我的java代码

public class RegisterActivity extends Activity {
private String[] states, states1;
private Spinner countrySP, titleSP;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_register);

        Spinner spinner = (Spinner) findViewById(R.id.registerTitle);
        spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

        states = getResources().getStringArray(R.array.title_array);
        states1 = getResources().getStringArray(R.array.country_array);

        titleSP = (Spinner) findViewById(R.id.registerTitle);

        final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, states);
        dataAdapter
        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        titleSP.setAdapter(dataAdapter);

        countrySP = (Spinner) findViewById(R.id.registerCountry);
        ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, states1);
        dataAdapter1
        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        countrySP.setAdapter(dataAdapter1);

        final Button button = (Button) findViewById(R.id.btnRegister);
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                try {


                    String TitleName = titleSP.getSelectedItem().toString();
                    String CountryName = countrySP.getSelectedItem().toString();
                    String position = String.valueOf(titleSP.getSelectedItemPosition());
                String position = String.valueOf(countrySP.getSelectedItemPosition()); //Duplicate local variable position

                                    new RegisterAsyncTask(
                                            getApplicationContext()).execute(
                                                    TitleName,
                                                    CountryName, position);

                                    Toast.makeText(getApplicationContext(),
                                            "Asynctask started",
                                            Toast.LENGTH_SHORT).show();

                } catch (Exception ex) {

                }

            }
        });


    public class MyOnItemSelectedListener implements OnItemSelectedListener {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {

    //check which spinner triggered the listener
    switch (parent.getId()) {
    //country spinner
    case R.id.registerTitle:
        //make sure the country was already selected during the onCreate
        String[] titles = getResources().getStringArray(R.array.title_array);
        String selectedTitle = titles[position];  
        System.out.println("title = " + position );

    case R.id.registerCountry:
        //make sure the country was already selected during the onCreate
        String[] countries = getResources().getStringArray(R.array.country_array);
        String selectedCountry = countries[position];  
        System.out.println("country = " + position );
    }

}




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

    }
    }

}

我的title.xml里面的值文件夹

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title_prompt">Choose the title</string>
    <string-array name="title_array">
        <item>Mr</item>
        <item>Mrs</item>
        <item>Miss</item>
        <item>Dr</item>
        <item>Rev</item>
        <item>Ms</item>
    </string-array>
</resources>

2 个答案:

答案 0 :(得分:2)

使用getSelectedItemPosition从按钮上的Spinner获取所选项目位置,单击为:

public void onClick(View v) {
       String TitleName = titleSP.getSelectedItem().toString();
       String CountryName = countrySP.getSelectedItem().toString();
       String position=String.valueOf(titleSP.getSelectedItemPosition());
       // pass position to RegisterAsyncTask
       new RegisterAsyncTask(
         getApplicationContext()).execute(TitleName,CountryName,position);

  }

答案 1 :(得分:1)

是的,现在我明白了, 你需要做这样的事情

public void onClick(View v) {

try {


String TitleName = titleSP.getSelectedItem().toString();
String CountryName = countrySP.getSelectedItem().toString();
String selectedPos=Integer.toString(titleSP.getSelectedItemPosition()); // or countrySP if u need it to


                new RegisterAsyncTask(
                        getApplicationContext()).execute(
                                TitleName,
                                CountryName, selectedPos);
:
:

现在你必须修改RegisterAsyncTask doInBackground():

... doInBackground(String... params){
//params[0] is TitleName
//params[1] is CountryName
//params[2] is selectedPos
:
:
}