android listview每个item里面有个radiobutton,怎么弄radibutton的单选事件

丫头嗳 2013-04-18 01:31:50
如题 android listview每个item里面有个radiobutton,怎么弄radibutton的单选事件,点击某个radio的时候它选中,其他的取消, 牛人们,,,请给出具体的代码

在网上找的 都没有效果,要么就是只能点击一个不能换成其他的了,要么就是都选上了。。。

final RadioButton mRadioButton = (RadioButton) view
.findViewById(R.id.report_item_check);
mRadioButton.setChecked(false);//先把所有的设置为false

if(mRadioButton.getId()<=position){
mRadioButton.setId(position);
}


mRadioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//在这儿实现整个listView item 中的radioButton的单选
if(isChecked)
{
if(temp!=-1)
{
RadioButton tempRadio =(RadioButton)view.findViewById(temp);
if(tempRadio !=null)
{
tempRadio.setChecked(false);
}
}
temp = buttonView.getId();
}

}
});
if (position == mRadioButton.getId()) {
mRadioButton.setChecked(true);
} else {
mRadioButton.setChecked(false);
}

// radioButton.setId(position);
// radioButton.setChecked(false);
// radioButton
// .setOnCheckedChangeListener(new OnCheckedChangeListener() {
//
// public void onCheckedChanged(
// CompoundButton buttonView, boolean isChecked) {
// if (radioButton.isChecked()==true) {
// temp=btn_id;
// btn_id = radioButton.getId();
// if (temp != -1 && temp != btn_id) {
// RadioButton tempButton = (RadioButton) view
// .findViewById(temp);
// if (tempButton != null) {
// tempButton.setChecked(false);
// }
// }
// }
//
// }
// });
// if (btn_id == position) {
// radioButton.setChecked(true);
// checked_map=map;
// } else {
// radioButton.setChecked(false);
// }
//
...全文
4077 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
GeneralAndroid 2015-01-23
  • 打赏
  • 举报
回复
哪里有讲解的博文
sam.zhang 2014-08-11
  • 打赏
  • 举报
回复
没看太懂,能不能把完整的activity 和adapter粘贴出来?
sam.zhang 2014-08-11
  • 打赏
  • 举报
回复
看着挺乱的,能不能发一个完整的adapter代码呀?
sam.zhang 2014-08-11
  • 打赏
  • 举报
回复
你好,看着有点乱,能不能给一个完整的adapter代码呀?
BorderLayout 2013-08-02
  • 打赏
  • 举报
回复
引用 9 楼 BorderLayout 的回复:
[quote=引用 6 楼 wlcw16 的回复:]


            rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        clickPosition=p;
                    }
                    else{
                        clickPosition = -1;
                    }
                    notifyDataSetChanged();
                }
            });
            if(clickPosition == position){
                rb.setChecked(true);
            }
            else{
                rb.setChecked(false);
            }
            return rb;
        }
        
    }
}

我原来的代码改了一下。改成了都是radio 你看看把 不用找之前的radio 只需要一个int保存当前选中的positon,然后刷新整个list就好了 只把当前的radio设成true,其他的都设成false 主要看一下adapter的getview方法。
按照你说的解决了,不过代码要改下,if(clickPosition == position){ rb.setChecked(true); } else{ rb.setChecked(false); } 要把else去掉,要不然都不会被选中。 感谢~[/quote] 说错了,是吧 if(isChecked){ clickPosition=p; } else{ clickPosition = -1; }这里的else去掉~·
BorderLayout 2013-08-02
  • 打赏
  • 举报
回复
引用 6 楼 wlcw16 的回复:


            rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        clickPosition=p;
                    }
                    else{
                        clickPosition = -1;
                    }
                    notifyDataSetChanged();
                }
            });
            if(clickPosition == position){
                rb.setChecked(true);
            }
            else{
                rb.setChecked(false);
            }
            return rb;
        }
        
    }
}

我原来的代码改了一下。改成了都是radio 你看看把 不用找之前的radio 只需要一个int保存当前选中的positon,然后刷新整个list就好了 只把当前的radio设成true,其他的都设成false 主要看一下adapter的getview方法。
按照你说的解决了,不过代码要改下,if(clickPosition == position){ rb.setChecked(true); } else{ rb.setChecked(false); } 要把else去掉,要不然都不会被选中。 感谢~
mmorss 2013-04-22
  • 打赏
  • 举报
回复
在adapter里面添加点击事件
李狗蛋52635 2013-04-18
  • 打赏
  • 举报
回复
引用 4 楼 enkezhang 的回复:
引用 3 楼 ueryueryuery 的回复:ListView本身自带单选和多选模式,通过 listview.setChoiceMode() 可以设置 设置了也没用,问题就在于这行代码, RadioButton tempButton = (RadioButton) view .findViewById(temp); 通过temp找不到之前设置过的……
setChoiceMode(),不需要自己放置radioButton,listView每行后面会有一个小√,用于选择。
wlcw16 2013-04-18
  • 打赏
  • 举报
回复

package com.example.zdemo1;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

public class ListBackground extends Activity {

    private List<String> dataList = new ArrayList<String>();
    
    private ListView lv;
    
    private int clickPosition = -1;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_background);
        for (char tempChar = 'a' ; tempChar <= 'z'; tempChar++){
            dataList.add(String.valueOf(tempChar));
        }
        lv = (ListView)findViewById(R.id.listView1);
        final MyAdapter ma = new MyAdapter();
        lv.setAdapter(ma);
        /*lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                if(position != clickPosition){
                    clickPosition = position;
                }
                else{
                    clickPosition = -1;
                }
                ma.notifyDataSetChanged();
            }
        });*/
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_list_background, menu);
        return true;
    }
    
    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public Object getItem(int position) {
            return dataList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            /*TextView tv = new TextView(ListBackground.this);
            tv.setText(dataList.get(position));
            tv.setBackgroundColor(Color.WHITE);
            if(position == clickPosition){
                tv.setBackgroundColor(Color.BLACK);
            }
            return tv;
*/
            final int p = position;
            RadioButton rb = new RadioButton(ListBackground.this);
            rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        clickPosition=p;
                    }
                    else{
                        clickPosition = -1;
                    }
                    notifyDataSetChanged();
                }
            });
            if(clickPosition == position){
                rb.setChecked(true);
            }
            else{
                rb.setChecked(false);
            }
            return rb;
        }
        
    }
}

我原来的代码改了一下。改成了都是radio 你看看把 不用找之前的radio 只需要一个int保存当前选中的positon,然后刷新整个list就好了 只把当前的radio设成true,其他的都设成false 主要看一下adapter的getview方法。
丫头嗳 2013-04-18
  • 打赏
  • 举报
回复
引用 1 楼 wlcw16 的回复:
发一下你的adapter的代码。 http://bbs.csdn.net/topics/390422655 我回答过的一个帖子,你们的需求类似。
public void setItemView(final View view, int position) { final HashMap<String, Object> map = (HashMap<String, Object>) helper .getPageData().getList().get(position); ImageView img = (ImageView) view .findViewById(R.id.report_item_img); TextView name = (TextView) view.findViewById(R.id.report_item_name); TextView money = (TextView) view.findViewById(R.id.report_item_money); TextView type = (TextView) view.findViewById(R.id.report_item_type); TextView content = (TextView) view .findViewById(R.id.report_item_content); if (map != null) { imageloader.DisplayImage(map.get("img").toString(), img); name.setText(map.get("name").toString()); money.setText(map.get("money").toString()); type.setText(map.get("mailway").toString()); content.setText(!"".equals(map.get("repay_content").toString())&&map.get("repay_content").toString().length()>140?map.get("repay_content").toString().substring(0, 140).concat("..."):map.get("repay_content").toString()); final RadioButton radioButton = (RadioButton) view .findViewById(R.id.report_item_check); radioButton.setChecked(false);//先把所有的设置为false if(radioButton.getId()<=position){ radioButton.setId(position); } // radioButton // .setOnCheckedChangeListener(new OnCheckedChangeListener() { // // public void onCheckedChanged( // CompoundButton buttonView, boolean isChecked) { // if (radioButton.isChecked()==true) { // temp=btn_id; // btn_id = radioButton.getId(); // if (temp != -1 && temp != btn_id) { // RadioButton tempButton = (RadioButton) view // .findViewById(temp); // if (tempButton != null) { // tempButton.setChecked(false); // } // } // } // // } // }); // if (btn_id == position) { // radioButton.setChecked(true); // checked_map=map; // } else { // radioButton.setChecked(false); // } } else { Utils.showToast(ReportListActivity.this, "没有相关数据"); } } 问题在于通过temp找不到之前选中过的radio。。。
丫头嗳 2013-04-18
  • 打赏
  • 举报
回复
引用 3 楼 ueryueryuery 的回复:
ListView本身自带单选和多选模式,通过 listview.setChoiceMode() 可以设置
设置了也没用,问题就在于这行代码, RadioButton tempButton = (RadioButton) view .findViewById(temp); 通过temp找不到之前设置过的那个radio。。。导致每个都可以选中却不能取消
李狗蛋52635 2013-04-18
  • 打赏
  • 举报
回复
ListView本身自带单选和多选模式,通过 listview.setChoiceMode() 可以设置
wlcw16 2013-04-18
  • 打赏
  • 举报
回复
就是在adapter设置一个isClickedPosition 然后在getview的时候 只对isClickedPosition的view的radio设置isChecked(true) 在oncheckedchange中对adapter进行刷新
wlcw16 2013-04-18
  • 打赏
  • 举报
回复
发一下你的adapter的代码。 http://bbs.csdn.net/topics/390422655 我回答过的一个帖子,你们的需求类似。

80,348

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧