2016-04-13 82 views
-1

我想在Spinner-1的基礎上填充Spinner-2。如何顯示鏈接數據的兩個Spinners?

例如,如果我選擇牛頓然後微調-2將只顯示蘋果,而不是其他值,如果我選擇Hiiii只有DS應在微調-2填充。這只是我在下圖中顯示的一個例子(所有的值在原始應用程序中都是動態的)。 在spinner-1中選擇了任何值的情況下,具有確定標籤的值應填充到微調器-2中。

Image Link

UPDATE-3 MainActivity.java

public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener, OnClickListener { 

    ArrayList<String> title; 
    ArrayList<String> title2; 
    Button button; 
    Spinner spinner; 
    Spinner spinner2; 
    private EditText fromDateEtxt; 
    //private EditText toDateEtxt; 

    private DatePickerDialog fromDatePickerDialog; 
    //private DatePickerDialog toDatePickerDialog; 

    private SimpleDateFormat dateFormatter; 

    ArrayAdapter<String> from_adapter; 
    ArrayAdapter<String> from_adapter2; 
    Map<String, List<String>> values = new HashMap<String, List<String>>(); 
    @TargetApi(Build.VERSION_CODES.GINGERBREAD) 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     title = new ArrayList<String>(); 
     title2 = new ArrayList<String>(); 

     button = (Button) findViewById(R.id.button1); 
     spinner = (Spinner) findViewById(R.id.spinner1); 
     spinner.setOnItemSelectedListener(this); 

     spinner2 = (Spinner) findViewById(R.id.spinner2); 
     spinner2.setOnItemSelectedListener(this); 
     findViewsById(); 

     setDateTimeField(); 
     if (android.os.Build.VERSION.SDK_INT > 9) { 
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
     } 


     button.setOnClickListener(new OnClickListener() 
     { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       parse(); 

       from_adapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item, title); 
       from_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

       from_adapter2=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item, title2); 
       from_adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 


       spinner.setAdapter(from_adapter); 
       spinner2.setAdapter(from_adapter2); 


      } 

      private Object from_adapter(int i) { 
       // TODO Auto-generated method stub 
       return null; 
      } 

     }); 


    } 



    public void onItemSelected(AdapterView<?> parent, View view, int pos, 
      long id) { 
     Toast.makeText(parent.getContext(), ""+spinner.getSelectedItem().toString().trim(), 
       Toast.LENGTH_LONG).show(); 
    } 

    public void onNothingSelected(AdapterView<?> arg0) { 
    } 

    protected void parse() { 
     // TODO Auto-generated method stub 


     try { 

      URL url = new URL(
        "WEB URL"); 
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      Document doc = db.parse(new InputSource(url.openStream())); 
      doc.getDocumentElement().normalize(); 

      NodeList nodeList = doc.getElementsByTagName("a:AAAA"); 
      for (int i = 0; i < nodeList.getLength(); i++) { 
       Element el = (Element) nodeList.item(i); 
       // get the a:W value 
       String awValue = el.getElementsByTagName("a:W").item(0).getTextContent(); 
       // add the above value in our map as key if it isn't present in the map, this key will 
       // have a list associated with it in which ALL the values for a:R will be stored, if 
       // the awValue key is present then you just add the new a:R value to its list 
       if (!values.containsKey(awValue)) { 
        values.put(awValue, new ArrayList<String>()); 
       } 
       // also add the value pointed by a:R to the list associated with a:W 
       String arValue = el.getElementsByTagName("a:R").item(0).getTextContent(); 
       values.get(awValue).add(arValue); 
       Log.d("ADebugTag", "Value: " + arValue); 
      } 
      for (int i = 0; i < nodeList.getLength(); i++) { 

       Node node = nodeList.item(i);  

       Element fstElmnt = (Element) node; 
       NodeList nameList = fstElmnt.getElementsByTagName("a:W"); 
       Element nameElement = (Element) nameList.item(0); 
       nameList = nameElement.getChildNodes();   

       NodeList websiteList = fstElmnt.getElementsByTagName("a:R"); 
       Element websiteElement = (Element) websiteList.item(0); 
       websiteList = websiteElement.getChildNodes(); 

       title.add(((Node) nameList.item(0)).getNodeValue()); 

      } 
      NodeList nodeList2 = doc.getElementsByTagName("a:AAAA"); 
      for (int i = 0; i < nodeList2.getLength(); i++) { 

       Node node = nodeList2.item(i);  

       Element fstElmnt = (Element) node; 
       NodeList nameList = fstElmnt.getElementsByTagName("a:R"); 
       Element nameElement = (Element) nameList.item(0); 
       nameList = nameElement.getChildNodes();   
       title2.add(((Node) nameList.item(0)).getNodeValue()); 

      } 
      Set<String> set = new LinkedHashSet<String>(title); 
      title = new ArrayList<String>(set); 
      // Collection<String> set = new LinkedHashSet<String>(months); 
      Set<String> set2 = new LinkedHashSet<String>(title2); 
      title2 = new ArrayList<String>(set2); 
      System.out.println("list are"); 
      System.out.println(set); 
      System.out.println("list 2 are"); 
      System.out.println(set2); 
     } catch (Exception e) { 
      System.out.println("XML Pasing Excpetion = " + e); 
     } 

    } 

更新-4-

我正的下面值IN THE logcat的

{ 
Jackfruit=[Cabbage, Kesar, Amond], 
Datia=[Kalpi, Kesar, Brinjal], 
Brinjal=[Cabbage, Kesar, Amond] 
} 

所以,如果菠蘿蜜中選擇spinner1則 - 白菜,凱薩爾,阿蒙德spinner2

diplay以上的logcat我從的下面的代碼獲得MainActivity.java的更新-3

NodeList nodeList = doc.getElementsByTagName("a:AAAAA"); 
      for (int i = 0; i < nodeList.getLength(); i++) { 
       Element el = (Element) nodeList.item(i); 
       // get the a:W value 
       String awValue = el.getElementsByTagName("a:W").item(0).getTextContent(); 
       // add the above value in our map as key if it isn't present in the map, this key will 
       // have a list associated with it in which ALL the values for a:R will be stored, if 
       // the awValue key is present then you just add the new a:R value to its list 
       if (!values.containsKey(awValue)) { 
        values.put(awValue, new ArrayList<String>()); 

       } 
       // also add the value pointed by a:R to the list associated with a:W 
       String arValue = el.getElementsByTagName("a:R").item(0).getTextContent(); 
       values.get(awValue).add(arValue); 

       // Log.d("ADebugTag", "Value: " + arValue); 
      } 
      Log.d("ADebugTag", "Value: " + values); 

我應該如何將鍵值傳遞給微調器,並且如果微調器鍵值被改變,那麼相應的值應該在微調器2中改變。

回答

1

第一次只需填寫微調器1。爲此,請更改解析方法以返回主標籤列表(title1),並將它們設置爲微調器。

然後在spinner1上使用setOnItemSelectedListener,在其內部解析XML以找到與選定項目相關的項目並根據該項目填寫spinner2。您可以使用它的position獲取選定的項目。要做到這一點,寫另一種方法(或使用相同的方法使用if條件說這不是主要標籤),從spinner1中選擇項目作爲輸入和返回子項目。然後將此列表添加到spinner2。

最後,你必須有這樣的事情:

spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position) 
     {   
      title2=parse(spinner1.get(position) 
      //set title2 to spinner2 
     }); 
+0

XML數據從URL XML DOM解析器 –

+0

來:你能否詳細說明更多的我的代碼 –

+0

想通過散列值來微調器,如果微調數據點擊改變,然後填充spinner2中的值,即Log.d(「ADebugTag」,「Value:」+ arValue); ...所有值正確分組....所以如果微調值更改,相應的組將顯示在微調框中 –