2017-08-10 6 views
0

カスタムアダプタでlistviewを使用していますが、画面の向きを変更しているときにラジオボタンの項目であるラジオボタンを選択解除すると問題が発生します。私は、画面の向きを変えても選択された項目を選択したい。ここに私のコード、listviewカスタムアダプタの項目が画面の向きを変更すると選択解除されました

public class MainActivity extends AppCompatActivity implements AdapterCallback { 
    ListView listView; 
    DataClass data; 
    ArrayList<DataClass> arrayList; 
    Button button; 
    Adapter adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     listView = (ListView) findViewById(R.id.list); 
     button = (Button) findViewById(R.id.button); 

     arrayList = new ArrayList<>(); 
     for (int i = 0; i < 10; i++) { 
      data = new DataClass(i, "std " + i); 
      arrayList.add(data); 

     } 


     adapter = new Adapter(this, arrayList, MainActivity.this); 
     listView.setAdapter(adapter); 

    } 


    @Override 
    public void onMethodCallback(int pos, String name, String atten) { 

    } 
} 

Adapter.java

public class Adapter extends BaseAdapter { 

    Context context; 
    ArrayList<DataClass> arrayList; 
    private AdapterCallback mAdapterCallback; 


    public Adapter(Context context, ArrayList<DataClass> arrayList, AdapterCallback callback) { 
     this.context = context; 
     this.arrayList = arrayList; 
     this.mAdapterCallback = callback; 
    } 

    @Override 
    public int getCount() { 
     return arrayList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return arrayList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     final DataClass rowItem = (DataClass) getItem(position); 

     if (convertView == null) { 
      LayoutInflater layoutInflater = (LayoutInflater) this.context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = layoutInflater.inflate(R.layout.list_item, null); 
     } 

     TextView tv_name = (TextView) convertView.findViewById(R.id.tv_name); 
     tv_name.setText(rowItem.getName()); 

     final RadioGroup radioGroup = (RadioGroup) convertView.findViewById(R.id.rdbt_group); 
     radioGroup.setOnCheckedChangeListener(null); 

     radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { 

       RadioButton checkedRadioButton = (RadioButton) group.findViewById(checkedId); 

       if (null != checkedRadioButton && checkedId > -1) { 

       } 
      } 
     }); 


     return convertView; 
    } 


} 

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/tv_name" 
     android:layout_width="0dp" 
     android:layout_gravity="center" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <RadioGroup 
     android:id="@+id/rdbt_group" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_marginRight="20dp" 
     android:orientation="horizontal" 
     android:padding="5dp"> 

     <RadioButton 
      android:id="@+id/radio_no" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="A" 
      android:textSize="15sp" /> 

     <RadioButton 
      android:id="@+id/radio_clr" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="B" 
      android:textSize="15sp" /> 

     <RadioButton 
      android:id="@+id/radio_yes" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="C" 
      android:textSize="15sp" /> 


    </RadioGroup> 

</LinearLayout> 
+0

場合(ヌル!= checkedRadioButton && checkedId> -1){}のために他のケースを書き、もう一度 –

答えて

0

画面が断片や活動が再作成されます回転するたびにあります。あなたは州を救う必要があります。 See this

+0

を試してみてください。このリンクは質問に答えるかもしれないが、ここでは答えの重要な部分を含めた方がよいとしてください参照のためのリンクを提供する。リンクされたページが変更された場合、リンクのみの回答は無効になります。 - [レビューの投稿](レビュー/低品質の投稿/ 16990793) – derape

+0

ああ、ok :) sry私はこれに新しいです。 – YueMuc

+0

それは大丈夫です、我々はすべてそこにいた。それを正しく行う方法のリンクがここにあります:https://stackoverflow.com/help/how-to-answer – derape

関連する問題