2011-08-15 56 views
4

我想膨脹我的佈局與一個有兩個TextViews和一個RadioButton的行的佈局。我希望所有的RadioButton都以這種方式加入同一個RadioGroup。這是我到目前爲止已經完成:如何用RadioButton充氣佈局然後添加到RadioGroup?

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     ViewHolder holder; 
     if (v == null) {   
      LayoutInflater vi = 
       (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.config_grid_item, null); 
      holder = new ViewHolder(); 
      holder.item1 = (TextView) v.findViewById(R.id.config_item1); 
      holder.item2 = (TextView) v.findViewById(R.id.config_item2); 
      holder.item3 = (RadioButton) v.findViewById(R.id.config_item3); 
      group.addView(holder.item3); 
      v.setTag(holder); 
     } 
     else 
      holder=(ViewHolder)v.getTag(); 

     final Profile profile = profiles.get(position); 
     if (profile != null) { 
      holder.item1.setText(profile.IP); 
      holder.item2.setText(profile.name); 
     } 
     return v; 
    } 

和行XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="fill_parent" 
    android:minHeight="60dip"> 

    <TextView android:id="@+id/config_item2" android:text="Name" 
     android:layout_height="wrap_content" android:layout_width="wrap_content" 
     android:layout_gravity="center_vertical" android:width="130dip" 
     android:padding="10dip" /> 

    <TextView android:id="@+id/config_item1" android:text="IP" 
     android:layout_height="wrap_content" android:layout_width="wrap_content" 
     android:layout_gravity="center_vertical" android:width="130dip" 
     android:padding="10dip" /> 
    <RadioButton android:id="@+id/config_item3" android:width="40dip" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:checked="false" android:focusable="false" 
     android:focusableInTouchMode="false" /> 
</LinearLayout> 

它崩潰的group.addView(holder.item3)。錯誤日誌是在這裏:

08-15 13:37:21.178: ERROR/AndroidRuntime(19749): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.view.ViewGroup.addViewInner(ViewGroup.java:1993) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.view.ViewGroup.addView(ViewGroup.java:1888) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.widget.RadioGroup.addView(RadioGroup.java:139) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.view.ViewGroup.addView(ViewGroup.java:1845) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.view.ViewGroup.addView(ViewGroup.java:1825) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at com.bluerisc.main.ConfigRowAdapter.getView(ConfigRowAdapter.java:67) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.widget.AbsListView.obtainView(AbsListView.java:1357) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1200) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.widget.ListView.onMeasure(ListView.java:1111) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.view.View.measure(View.java:8222) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3155) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.view.View.measure(View.java:8222) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3155) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:381) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.view.View.measure(View.java:8222) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3155) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.view.View.measure(View.java:8222) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3155) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.view.View.measure(View.java:8222) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.view.ViewRoot.performTraversals(ViewRoot.java:805) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1764) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.os.Looper.loop(Looper.java:143) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at android.app.ActivityThread.main(ActivityThread.java:5068) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at java.lang.reflect.Method.invoke(Method.java:521) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):  at dalvik.system.NativeStart.main(Native Method) 

從錯誤日誌,我可以猜測,因爲單選按鈕已經是它不希望被添加到radioGroup中的行XML中的LinearLayout的孩子。任何想法如何解決這個問題?我基本上只想要一次選擇某個單選按鈕,這就是爲什麼我將它們添加到一個radiogroup。

編輯:我剛剛聽到了錯誤消息,並試圖從LinearLayout中刪除視圖的天才想法。它不會崩潰,但現在RadioButton不會出現在我的行​​中。略有進展?以下是我添加/更改的內容:

 LinearLayout test = (LinearLayout) v.findViewById(R.id.LinearRadio); 
     test.removeView(holder.item3); 
     group.addView(holder.item3); 

回答

0

您無法同時向2個佈局添加視圖。
閱讀完編輯之後:是的,您將無法查看該單選按鈕,因爲該單選按鈕已從其可見父項中移除並添加到不可見的radiogroup中。

看來你需要一個列表視圖與單行兩列在每一行。 listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);與項目行爲simple_list_item_2將滿足需要。

+0

我很困惑。將我目前的ListView更改爲此?或者在我的LinearLayout中添加一個新的ListView,然後添加到我的ListView中?另外,爲什麼我需要兩行? –

+0

您的要求是每行中的名稱字段和相應的IP。因此,使用兩個訂單項列表視圖看起來會比將這兩個視圖放在一行中看起來更好。你也會得到更多的名稱字段的寬度。想想看。 更改當前的列表視圖。將其選擇模式設置爲single,並將simple_list_item_2作爲資源傳遞給適配器構造函數。 – Ronnie

+0

setChoiceMode無法使用。 :\我認爲無論使用哪種列表類型,radiobutton仍然需要放在一個radiogroup中。另外,佈局是我想要的。更多的配置文件可以適應行較薄的屏幕上。 –

0

您需要將無母版LinearLayout添加到RadioGroup。

 LayoutInflater vi = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.config_grid_item, null); 
     holder = new ViewHolder(); 
     holder.item1 = (TextView) v.findViewById(R.id.config_item1); 
     holder.item2 = (TextView) v.findViewById(R.id.config_item2); 
     holder.item3 = (RadioButton) v.findViewById(R.id.config_item3); 
     group.addView(v); 
+0

這導致崩潰:ListView.measureScrapChild(View,int,int)line:1119 –

-1

不好的解決方案...只需取消選中所有的單選按鈕。 (Radiogroup根本沒有必要) private void uncheckRadioBtn(){for(int i = 0; i < contactMaxNum; ++ i)if(rb [i] .isChecked())rb [i] .setChecked(false) ;}