2012-07-26 55 views
1

我已經創建了單選按鈕自定義的ListView使用自定義適配器它的每一個行。我想以編程方式檢查其父視圖(列表項)被選中時的單選按鈕,然後取消選中其他ListView項目內的其他RadioButton。任何人都可以提供解決方案嗎?操縱單選內的ListView在Android

UPDATE

這裏是我的getView()

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LinearLayout v = null; 

    final Item i = items.get(position); 

    EntryItem ei = (EntryItem) i; 
    v = (LinearLayout) vi.inflate(R.layout.list_item_entry, null); 
    final TextView title = (TextView) v 
        .findViewById(R.id.list_item_entry_title); 
    final TextView subtitle = (TextView) v 
        .findViewById(R.id.list_item_entry_summary); 


    RadioButton radioButton = new RadioButton(context); 
    radioButton.setFocusable(false); 
    radioButton.setFocusableInTouchMode(false); 
    v.addView(radioButton); 

    if (title != null) 
       title.setText(ei.title); 
    if (subtitle != null) 
       subtitle.setText(ei.subtitle); 


    convertView = v; 
    return convertView; 
} 

回答

2

要在ListView.onsetOnItemClickListener()復位單選按鈕,這樣做在getView方法..

listView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     //get the radio button and check it 
        view.findViewById(); .... 
     } 
    }); 
+1

我怎樣才能訪問特定的ListView項裏面的rdBtn? 我沒有使用RadioGroup,我爲每個ListView項目使用單個RadioButton 順便說一句RadioBUtton是以編程方式創建的,在XML佈局上 – 2012-07-26 07:13:10

+0

在創建單選按鈕時,您可以設置一個ID ..然後執行上述操作.. – Nermeen 2012-07-26 07:19:32

+1

I用我的getView()方法更新了我的帖子 – 2012-07-26 07:22:48

4

您可以使用一個int變量,如果單個項目應選擇與-1初始化;

int index =-1; 
在添加一行getView方法

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LinearLayout v = null; 

    final Item i = items.get(position); 

    EntryItem ei = (EntryItem) i; 
    v = (LinearLayout) vi.inflate(R.layout.list_item_entry, null); 
    final TextView title = (TextView) v 
        .findViewById(R.id.list_item_entry_title); 
    final TextView subtitle = (TextView) v 
        .findViewById(R.id.list_item_entry_summary); 


    RadioButton radioButton = new RadioButton(context); 
    radioButton.setFocusable(false); 
    radioButton.setFocusableInTouchMode(false); 
    radioButton.setChecked(index==position); 
    radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {      
        @Override 
        public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 

        } 
       }); 
       v.addView(radioButton); 
      } 
      if (title != null) 
       title.setText(ei.title); 
      if (subtitle != null) 
       subtitle.setText(ei.subtitle); 
     } 

    convertView = v; 
    return convertView; 
} 

和在收聽者OnItemClick方法,執行:

index=position; 
adapter.notifyDataSetInvalidated(); 
+0

這是正確的。你可以使用上述解決方案 – 2012-07-26 07:07:58

+0

我怎麼可以訪問rdBtn特定listView項內? – 2012-07-26 07:09:40

+1

我沒有使用RadioGroup中,我用單單選按鈕用於BTW的單選按鈕被編程方式創建,而不是在XML佈局 – 2012-07-26 07:10:48

1
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    style="?android:attr/spinnerDropDownItemStyle" 
    android:singleLine="true" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:ellipsize="marquee" /> 

以上代碼從android.R.layout .simple_spinner_dropdown_item

它正是你想要達到的。可能這會對你有所幫助。

1

您可以在列表視圖中的單擊事件進行管理......在setOnClickListener事件,你可以得到Radiobutton雖然查看..然後你可以管理它