在android应用程序中从strings.xml分配值时无法获取选定的微调器值

时间:2014-08-14 15:53:42

标签: java android xml

android applicationpopulate values进入spinnerused values from values\strings.xml

 <string-array name="myList">
       <item>first</item>
        <item>second</item>
  </string-array>

layout\input_box.xml

 <Spinner
        android:id="@+id/spinner_problems"
        android:entries="@array/myList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"       
         />

上面的代码将populate spinner values from myList

on MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {

  spin_problem = (Spinner)findViewById((R.id.spinner_problems));   
        spin_problem.setOnItemSelectedListener(new OnItemSelectedListener(){

             @Override
             public void onItemSelected(AdapterView<?> adapter, View view1, int pos, long id)
             {
                  spin_problem_val= adapter.getItemAtPosition(pos).toString();
             }

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

            }
        });

    //thread run
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                try {
                    showInputDialog("Any Complaints?");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }}, 8000);  // 3000 milliseconds
        //end thread        

}

showInputDailog方法代码 //我的文本框和微调器在这个方法中

public void showInputDialog(final String textparam) throws JSONException {      
         try{
        // get prompts.xml view to show complaint popup 
        LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
        View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                MainActivity.this);
        alertDialogBuilder.setView(promptView);
         if(textparam.toString() == "Feedback"){alertDialogBuilder.setTitle("Feedback");}
         else{alertDialogBuilder.setTitle("Any Complaints?");}

        //Get all dialog box values
        final EditText name = (EditText) promptView
                .findViewById(R.id.name);
        final EditText mobno = (EditText) promptView
                .findViewById(R.id.mobno);
        final EditText complaint = (EditText) promptView
                .findViewById(R.id.complaint);



        // final Spinner problems = (Spinner)findViewById((R.id.spinner_problems));     
        // final Spinner area = (Spinner)findViewById((R.id.spinner_area));

    //  final String aa = problems.getSelectedItem().toString();
     // final String vvv = area.getSelectedItem().toString();   

//这里我需要获得微调器值以进一步处理... }

1 个答案:

答案 0 :(得分:1)

从微调器获取值,将onItemSelectedListener添加到微调器。像这样:

     String value;
     final Spinner problems = (Spinner)findViewById((R.id.spinner_problems));
      problems.setOnItemSelectedListener(new OnItemSelectedListener(){

         @Override
         public void onItemSelected(AdapterView<?> adapter, View view1, int pos, long id)
         {
              value= adapter.getItemAtPosition(pos).toString();
         }

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

        }
    });

编辑:

- 有关详情,请查看thisthisthis

编辑2:

  • 如果您的微调器位于main_activity中,请将onCreate方法中的代码添加为value作为全局变量。

  • 如果您希望您的微调器位于alertDialog中,请在showInputDialog方法中添加代码