2017-08-10 91 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

雖然這個鏈接可能回答這個問題,最好是在這裏有答案的關鍵部位和提供鏈接供參考。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/16990793) – derape

+0

啊,好的:) sry我是新手。 – YueMuc

+0

沒關係,我們都去過那裏。以下是有關如何正確執行此操作的鏈接:https://stackoverflow.com/help/how-to-answer – derape

相關問題