【问题标题】:Manipulating RadioButton inside ListView on Android在 Android 上的 ListView 中操作 RadioButton
【发布时间】:2012-07-26 15:54:09
【问题描述】:

我使用自定义适配器在每行上创建了一个带有单选按钮的自定义 ListView。我想在检查其父视图(列表项)时以编程方式检查单选按钮,然后取消选中其他 ListView 项中的其他 RadioButtons。谁能给我解决方案?

更新

这是我的 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;
}

【问题讨论】:

    标签: android listview radio-button


    【解决方案1】:

    如果要选择单个项目,可以使用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();
    

    【讨论】:

    • 如何访问特定 listView 项目中的 rdBtn?
    • 我没有使用 RadioGroup,我为每个 ListView 项目使用了单个 RadioButton
    • 顺便说一句,RadioBUtton 是以编程方式创建的,而不是在 XML 布局中
    • 你能粘贴customAdapter的getView方法和你正在膨胀项目的xml吗?
    • 在 OnItemClickListener 中,index 是什么意思,设置 index=position 后是什么意思?
    【解决方案2】:

    要重置单选按钮,请使用getView 方法..

    ListView.onsetOnItemClickListener()

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

    【讨论】:

    • 如何访问特定 listView 项目中的 rdBtn?我没有使用 RadioGroup,我为每个 ListView 项目使用单个 RadioButton BTW RadioBUtton 是在 XML 布局中以编程方式创建的
    • findViewById 用于通过 ID 访问(从 XML 创建),而我的 RadioButton 使用 addView 以编程方式创建
    • 但是你可以使用方法,radioButton.setId()..或者你可以使用radioButton.setTag(),然后view.findViewWithTag()的另一种选择
    • 好的,这可以检查被点击项目的 RadioButton,但是如何取消其他的?
    • 可以将选中项的位置保存为全局变量,在你的getView()中,radioButton.setChecked(selectedPosition==position);
    【解决方案3】:
    <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

    它完全符合您的要求。可能这对你有帮助。

    【讨论】:

      【解决方案4】:

      您可以在列表视图的点击事件中管理它...在setOnClickListener事件中,您可以通过视图获取单选按钮..然后您可以管理它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-24
        • 2012-01-19
        • 2011-07-18
        • 1970-01-01
        • 2012-09-18
        • 1970-01-01
        • 1970-01-01
        • 2014-10-30
        相关资源
        最近更新 更多