2014-05-19 1 views
0

나는 세 개의 회 전자를 보여주는 안드로이드 활동이 있습니다. 똑같은 세 가지는 똑같은 정보를 가지고 있습니다. 왜냐하면 당신이 다섯 가지 중에서 세 가지를 선택하기로되어 있기 때문입니다. 내가하고 싶은 것은 지금 선택된 것을 기반으로 정보를 표시하는 것입니다. 예를 들어 첫 번째 회 전자에서 "코인"을 선택한 경우 두 개의 회 전자에서 동전을 선택할 수 있기를 원하지 않습니다. 두 번째 회 전자에서 "자본"을 선택하면 자본과 동전을 표시하지 않으려 고합니다. 마지막 하나.회 전자 사이 디스플레이 정보를 저장하는 방법

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:id="@+id/LinearLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:background="#449def" 
     android:gravity="center_horizontal" > 

     <TextView 
      android:id="@+id/typeDrinks" 
      style="@style/textStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Select three question for ask" 
      android:textSize="22sp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/LinearLayout" > 

     <Spinner 
      android:id="@+id/spinner_type1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="8sp" 
      android:gravity="center" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/textRangeOfPrices" 
     style="@style/textStyle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/linearLayout1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="10dp" 
     android:background="#449def" 
     android:gravity="center" 
     android:text="Select another one" 
     android:textSize="22sp" /> 

    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/textRangeOfPrices" 
     android:orientation="horizontal" > 

     <Spinner 
      android:id="@+id/spinner_type2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="8sp" 
      android:gravity="center" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/textViewDistance" 
     style="@style/textStyle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/linearLayout2" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="10dp" 
     android:background="#449def" 
     android:gravity="center" 
     android:text="The last one" 
     android:textSize="22sp" /> 

    <LinearLayout 
     android:id="@+id/linearLayout3" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/textViewDistance" 
     android:orientation="horizontal" > 

     <Spinner 
      android:id="@+id/spinner_type3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="8sp" 
      android:gravity="center" /> 
    </LinearLayout> 

    <Button 
     android:id="@+id/buttonGenerateRestaurant" 
     style="@style/ButtonText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:background="@layout/blue_button" 
     android:onClick="generateQuery" 
     android:text="Generate" /> 

</RelativeLayout> 

이 지금까지 내 코드입니다 :

이 활동에 대한 내 XML이다. 이 코드와 같은 정보는 세 가지 스피너에 나타납니다, 당신이 내가 범위

<string-array name="range_types"> 
    <item>Complete Name</item> 
    <item>Coin</item> 
    <item>Capital</item> 
    <item>Area(Km)</item> 
    <item>Political system</item> 
</string-array> 
<string-array name="range_types2"> 
    <item>Complete Name</item> 
    <item>Coin</item> 
    <item>Capital</item> 
    <item>Area(Km)</item> 
    <item>Political system</item> 
</string-array> 
<string-array name="range_type3"> 
    <item>Complete Name</item> 
    <item>Coin</item> 
    <item>Capital</item> 
    <item>Area(Km)</item> 
    <item>Political system</item> 
</string-array> 

에 무엇을 가지고 세 번에게 같은 일

/* 
     * this part of the code is for displaying the spinner with the selected 
     * choice 
     */ 
     spinnerType1 = (Spinner) findViewById(R.id.spinner_type1); 
     spinnerType2 = (Spinner) findViewById(R.id.spinner_type2); 
     spinnerType3 = (Spinner) findViewById(R.id.spinner_type3); 

     ArrayAdapter<CharSequence> adapterType1 = ArrayAdapter 
       .createFromResource(this, R.array.range_types, 
         android.R.layout.simple_spinner_item); 

     ArrayAdapter<CharSequence> adapterType2 = ArrayAdapter 
       .createFromResource(this, R.array.range_types2, 
         android.R.layout.simple_spinner_item); 

     ArrayAdapter<CharSequence> adapterType3 = ArrayAdapter 
       .createFromResource(this, R.array.range_type3, 
         android.R.layout.simple_spinner_item); 

     adapterType1 
       .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     adapterType2 
       .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     adapterType3 
       .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     spinnerType1.setAdapter(adapterType1); 
     spinnerType2.setAdapter(adapterType2); 
     spinnerType3.setAdapter(adapterType3); 

     spinnerType1.setOnItemSelectedListener(this); 
     spinnerType2.setOnItemSelectedListener(this); 
     spinnerType3.setOnItemSelectedListener(this); 
    } 

    @Override 
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, 
      long arg3) { 
     // TODO Auto-generated method stub 
     // An item was selected. You can retrieve the selected item using 
     // parent.getItemAtPosition(pos) 
     mType1 = (String) spinnerType1.getSelectedItem().toString(); 
     mType2 = (String) spinnerType2.getSelectedItem().toString(); 
     mType3 = (String) spinnerType3.getSelectedItem().toString(); 

    } 

를 선택할 수 있습니다

나는 Google과 stackoverflow에서 검색을 해왔지만 정확히 내가 무엇을 찾고 있는지, 아니면 정확하게 묻는 법을 모르겠다.

정말 고마워요!

답변

0

정확히 내가 무엇을 찾고 있는지 발견했습니다. 문제는 내가 배열에 의해 초기화 될 때 ArrayAdapter, The ArrayAdapter를 가지고, 배열을 수정할 수없는 AbstractList (List)로 변환한다는 것입니다.

해결 방법 ArrayAdapter를 초기화하는 동안 배열을 사용하는 대신 ArrayList를 사용하십시오.

String[] arrayShow = getResources().getStringArray(R.array.range_types); 

      ArrayList<String> lst = new ArrayList<String>(); 
      lst.addAll(Arrays.asList(arrayShow)); 


      final ArrayAdapter<String> adapterType1 = new ArrayAdapter<String>(
        this, android.R.layout.simple_spinner_item, lst); 

     adapterType1 
       .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 


     spinnerType1.setAdapter(adapterType1); 

spinnerType1.setOnItemSelectedListener(new OnItemSelectedListener() { 

      @Override 
      public void onItemSelected(AdapterView<?> arg0, View arg1, 
        int arg2, long arg3) { 
       // TODO Auto-generated method stub 
       adapterType2.remove(adapterType1.getItem(arg2)); 
       adapterType2.notifyDataSetChanged(); 

      } 

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


      } 
     }); 

지금이 문제 만약 내가 .. 그래서 다음 회에 추가하고 대한

를 내가 삭제하고 무엇 추적을 보존 할
0

세트 onitemselected 메서드에서 동적으로 스피너에 어댑터