2012-11-14 42 views
0

我試圖在相同的界面有2個spinners,但它不工作。爲什麼?它只顯示一個微調器。是否有可能在同一界面中有兩個微調器?

一個從數據庫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

您已經定位在同一個位置的兩個微調。

嘗試一個微調移動到其他方向

<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" /> 
+0

感謝這麼這麼多 –

相關問題