是否有可能在同一界面中有两个微调器?

时间:2012-11-14 10:35:26

标签: android android-spinner

我试图在同一个界面中有2个微调器,但它不起作用。为什么?它只显示一个微调器。

一个从数据库android:id="@+id/sp11"检索信息,另一个从字符串文件android:id="@+id/typeMessage_spinner"检索信息。 只有从数组@string文件中检索的文件才有效。 如果我删除android:id="@+id/typeMessage_spinner"从数据库中检索的那个工作。 在界面中只显示一个微调器。

像这样 http://im13.gulfup.com/W1fV1.png

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

      <Spinner
        android:id="@+id/sp11"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/chooseCourse"
        android:layout_marginTop="20dip"
        android:layout_marginLeft="8dip"
        android:layout_marginRight="8dip"
    />
          <Spinner
        android:id="@+id/typeMessage_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/chooseCourse"
        android:layout_marginTop="20dip"
        android:layout_marginLeft="8dip"
        android:layout_marginRight="8dip"
    />

</RelativeLayout>

以下是onCreate()中的代码。看看我的拳头旋转器,在onPostExecute()我有第二个旋转器

  public class MainActivity extends Activity {
        JSONParser jParser = new JSONParser(); // class
        ArrayList<HashMap<String, String>> coursesList;


        private static String url_all_course = "http://10.0.2.2/SmsPhp/view_all_course.php";

        private static final String TAG_SUCCESS = "success";
        private static final String TAG_course = "course";
        private static final String TAG_CourseID = "CourseID";
        private static final String TAG_Name = "Name";
        JSONArray courses = null;
        // Spinner element
         SpinnerAdapter adapter,adapter1;
        Spinner spinner ,spinner1;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            coursesList = new ArrayList<HashMap<String, String>>();

            spinner= (Spinner) findViewById(R.id.typeMessage_spinner);
            spinner1= (Spinner) findViewById(R.id.sp11);
            // Create an ArrayAdapter using the string array and a default spinner layout
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( MainActivity .this,
                    R.array.typeMessage_spinner, android.R.layout.simple_spinner_item);
            // Specify the layout to use when the list of choices appears
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            // Apply the adapter to the spinner
            spinner.setAdapter(adapter);
            new LoadAllCourses().execute();

        }

        class LoadAllCourses extends AsyncTask<String, String, String> {




            @Override
            protected String doInBackground(String... args) {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                // getting JSON string from URL
                JSONObject json = jParser.makeHttpRequest(url_all_course, "GET",
                        params);

                // Check your log cat for JSON response
                Log.d("All courses: ", json.toString());

                try {
                    // Checking for SUCCESS TAG
                    int success = json.getInt(TAG_SUCCESS);

                    if (success == 1) {
                        // course found
                        // Getting Array of course
                        courses = json.getJSONArray(TAG_course);

                        // looping through All courses
                        for (int i = 0; i < courses.length(); i++)// course
                                                                    // JSONArray
                        {
                            JSONObject c = courses.getJSONObject(i); // read first

                            // Storing each json item in variable
                            String CourseID = c.getString(TAG_CourseID);
                            String Name = c.getString(TAG_Name);

                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();

                            // adding each child node to HashMap key => value
                            map.put(TAG_CourseID, CourseID);
                            map.put(TAG_Name, Name);

                            // adding HashList to ArrayList
                            coursesList.add(map);
                        }
                    } else {


                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;
            }

            /**
             * After completing background task Dismiss the progress dialog
             * **/
            protected void onPostExecute(String file_url) {
                // dismiss the dialog after getting all products


                adapter1 = new CustomAdapter(MainActivity.this,
                        android.R.layout.simple_spinner_item,
                        coursesList);

                 spinner1.setAdapter(adapter1); // Set the custom adapter to the spinner
                    // You can create an anonymous listener to handle the event when is selected an spinner item
                 spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener() {

                   /*    @Override
                       public void onItemSelected(AdapterView<?> adapterView, View view,
                                int position, long id) {
                            // Here you get the current item (a User object) that is selected by its position
                            ArrayAdapter<HashMap<String, String>> x = (ArrayAdapter<HashMap<String, String>>) adapter.getItem(position);
                            // Here you can do the action you want to...
                            Toast.makeText(MainActivity.this, "ID: "+  x ,
                                Toast.LENGTH_SHORT).show();
                        }*/
                        @Override
                        public void onNothingSelected(AdapterView<?> adapter) {  }
                    });
                }
            }

    }

1 个答案:

答案 0 :(得分:1)

您已将两个微调器定位在完全相同的位置。

尝试将一个微调器移动到其他方向

<Spinner
    android:id="@+id/sp11"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/chooseCourse"
    android:layout_marginTop="20dip"
    android:layout_marginLeft="8dip"
    android:layout_marginRight="8dip" />
<Spinner
    android:id="@+id/typeMessage_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/chooseCourse"
 add ->  android:layout_below="@+id/sp11"
    android:layout_marginTop="20dip"
    android:layout_marginLeft="8dip"
    android:layout_marginRight="8dip" />