【问题标题】:Android: ArrayAdapter with RadioButtonAndroid:带有 RadioButton 的 ArrayAdapter
【发布时间】:2012-03-11 06:10:11
【问题描述】:

我发现这段代码效果很好,我必须创建一些非常相似但使用 RadioButton 的东西。我需要改变什么?

    public class InteractiveArrayAdapter extends ArrayAdapter<Model> {

    private final List<Model> list;
    private final Activity context;

    public InteractiveArrayAdapter(Activity context, List<Model> list) {
        super(context, R.layout.rowbuttonlayout, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.rowbuttonlayout, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
            viewHolder.checkbox
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            Model element = (Model) viewHolder.checkbox
                                    .getTag();
                            element.setSelected(buttonView.isChecked());

                        }
                    });
            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox.setChecked(list.get(position).isSelected());
        return view;
    }
}



    public class Model {

    private String name;
    private boolean selected;

    public Model(String name) {
        this.name = name;
        selected = false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

}

“rowbuttonlayout.xml”。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="30px" >
    </TextView>

    <CheckBox
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px" >
    </CheckBox>

</RelativeLayout>

我在website 上找到了所有这些代码。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    我想你会在这个已经回答的问题中找到所有必需的信息:

    listview with radio group error

    @Dante 接受的答案包括您重写自定义数组适配器以使用单选按钮所需的所有代码。

    编辑

    根据您的评论,我不确定您是否真的知道如何使用自定义适配器、视图持有者和对象编写自定义列表。

    Romain Guy 在这里详细解释列表视图

    http://www.google.com/events/io/2010/sessions/world-of-listview-android.html

    以及相关的pdf

    http://dl.google.com/googleio/2010/android-world-of-listview-android.pdf

    一旦您处理 customadapter viewholder 等,您只需让您的列表实现可检查。例如这里

    How to use RadioGroup in ListView custom adapter?

    如果您不知道如何设置自定义列表视图,这里是一个示例,但它与您的问题没有直接关系:

    // The Adapter
    class myObjectAdapter extends ArrayAdapter<myObject> {
        myObjectAdapter() {
            super(MyActivity.this, R.layout.my_row_layout,
                    listItems);
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            myObjectHolder holder = null;
            if (convertView == null) {
                LayoutInflater inflater = getLayoutInflater();
                convertView = inflater.inflate(R.layout.my_row_layout,
                        parent, false);
                holder = new myObjectHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (myObjectHolder) convertView.getTag();
            }
            holder.populateFrom(listItems.get(position));
            return (convertView);
        }
    }
    
    // The Holder
    static class myObjectHolder {
        private TextView myText = null;        
    
        myObjectHolder(View row) {
            myText = (TextView) row.findViewById(R.id.MyTextView);            
        }
    
        void populateFrom(myObject r) {
            myText.setText(r.getText());            
        }
    }
    
    // The Object
    class myObject {        
        public final String myText;        
    
        public myObject(String myText) {            
            this.myText = myText;            
        }
    
        public String getText() {
            return myText;
        }                
    }
    

    假设您有一个名为 my_row_layout 的 xml 文件,其中包含一个名为 MyTextView 的文本视图。在您的情况下,您将用单选按钮替换文本视图,并根据上面给出的链接实现可检查,它会起作用。

    希望对你有帮助

    【讨论】:

    • Dante 的解决方案有问题。他正好创建了 5 个 RadioButton!我必须动态创建相同的 RadioButton,因为我不知道数字。我该怎么办?
    • stackoverflow.com/questions/7329856/… google.com/events/io/2010/sessions/… 有助于理解这个问题。现在我用单选按钮创建了同样的东西,但我不知道为什么可以检查多个按钮,而不仅仅是一个......我想问题是我没有设置单选按钮,但我没有知道我可以在哪里设置它!对于新手的问题,我很抱歉,但我只从 2 周开始使用 android...
    • 好的,你现在有一个带有单选按钮的列表,一次只需要检查一个。即使您说“stackoverflow.com/questions/7329856/...
    猜你喜欢
    • 1970-01-01
    • 2021-03-24
    • 2012-10-15
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    相关资源
    最近更新 更多