Android的 - 单选按钮的ListView? [英] Android - Radio Button in listView?

查看:97
本文介绍了Android的 - 单选按钮的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我们应该在列表视图单一选择模式单​​选按钮的场景。当我点击单选按钮就应该去使能状态。当我点击整个项目,那么它应该重定向到新的活动。我refered以下链接

I have a scenario that we should have a single choice mode radio button in listview. when i am click on the radiobutton it should go to enable state. when i am click on the whole item then it should redirect to the new activity. i refered below link

LINK1 ,<一个href="http://vikaskanani.word$p$pss.com/2011/07/20/android-custom-image-gallery-with-checkbox-in-grid-to-select-multiple/"相对=nofollow>链接2

我能找到的出路多选模式。但并非为单一的选择模式。对任何想法?

i can find out the way for multiple choice mode. but not for the single choice mode. Any Idea on this ?

???编辑??? 我怎样才能找到一个项目的相应标签的标签,在列表视图

???Edit??? How can i find the tag of the corresponding tag of an item in the listview

推荐答案

你可以,但我不知道它的完善与否。一个方法

One way you can but I don't know it's perfect or not.

跟踪列表视图的位置ID上的单选按钮是现在选中,当您点击另一个单选按钮,然后执行 setOnCheckedChangeListener(听众)确认位置这已经检查并取消选中单选按钮。

track the position id of the listview on which the radio button was checked now when you click on the another radio button then implement the setOnCheckedChangeListener(listener) and check the position which was already check and uncheck that radiobutton.

您可以单选按钮的状态存入定制机型(型号包含像TextView的,ImageView的,单选按钮等的列表视图单行的控件),将其添加到列表视图

You can store the status of radio button into the custom model(Model which contains the controls like textview, imageview, radiobutton etc for listview single row) which was added into the listview

检查文章使用模型和组件处理成列表视图这给复选框的例子

check this article for using model and handle the component into the listview in this given an example of checkbox

更新

我认为你可以得到像这样的标签

I think you can get tag like this way

((View)((ViewGroup)listview.getItemAtPosition(0)).getTag()).getTag();
or
((Button)l.getItemAtPosition(0)).getTag();

更新2

想这是你的适配器和ArrayList对象

suppose this is your adapter and arraylist object

private List<Model> list_model = new ArrayList<Model>();

private ArrayAdapter<Model> modelAdapter;

您Model类看起来像这样

you Model class look like this

private class Model{
    private String text1 = "";
    private boolean isChecked   = false;

    public Model(String text1){
        this.text1 = text1;
        isChecked = false;
    }
}

您viewholder

your viewholder

private static class ViewHolder{
    TextView textView;
        RadioButton radioBtn;
}

传递的ListModel此CustomAdapter类的构造函数

pass the listmodel to this CustomAdapter class in constructor

private List<Model> list;
private Context context;

public CustomAdapter(Context context, List<Model> list){
    super(context,R.layout.list_layout,list);
    this.list = list;
    this.context = context;
}

现在 getView()

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

    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.gcalendar_list_layout, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.textView = (TextView) view.findViewById(R.id.text1);
        viewHolder.radioBtn = (RadioButton) view.findViewById(R.id.radioBtn);
        viewHolder.radioBtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Model element = (Model) viewHolder.checkBox.getTag();
                element.isChecked = buttonView.isChecked();
                boolean isChecked = true;
                for(int i=0;i<list.size();i++){
                    if(!list.get(i).isChecked){
                        list.get(i).isChecked=false; // more implement here or may be this work
                        break;
                    }
                }
            }
        });
        view.setTag(viewHolder);
        viewHolder.radioBtn.setTag(list.get(position));
    }else{
        view = convertView;
        ((ViewHolder)view.getTag()).radioBtn.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.textView.setText(list.get(position).name);
    holder.radioBtn.setChecked(list.get(position).isChecked);
    return view;
}

这篇关于Android的 - 单选按钮的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆