zoukankan      html  css  js  c++  java
  • ListView中RadioButton实现单项选择

    1:FragmentHack5.java

    public class FragmentHack5 extends Fragment {
        View view;
        ListView lvCountries;
        Button btnShow;
        CountryListAdapter adapter;
        List<String> list;
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
    
            list = new ArrayList<String>();
            list.add("中国");
            list.add("俄罗斯");
            list.add("美国");
            list.add("德国");
            list.add("英国");
            list.add("西班牙");
            list.add("法国");
            list.add("巴西");
            list.add("印度");
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.fragment_hack5,container,false);
    
            btnShow = (Button)view.findViewById(R.id.btnShow);
            lvCountries = (ListView)view.findViewById(R.id.lvCountries);
    
            adapter = new CountryListAdapter(getActivity(),R.layout.list_country_item,list);
    
            lvCountries.setAdapter(adapter);
    
            btnShow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getActivity(),adapter.getChoiceCountry(),Toast.LENGTH_SHORT).show();;
                }
            });
    
            return view;
        }
    }

    2:CountryListAdapter.java

    public class CountryListAdapter extends ArrayAdapter<String>{
    
        int resourceId;
        int choiceId = -1;
    
        public CountryListAdapter(Context context, int resourceId, List<String> objects){
            super(context,resourceId,objects);
    
            this.resourceId = resourceId;
        }
    
    
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            String country = getItem(position);
            final ViewHolder holder;
    
            if(convertView==null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(getContext()).inflate(resourceId,null);
    
                holder.rbCountry = (RadioButton)convertView.findViewById(R.id.rbCountry);
    
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder)convertView.getTag();
            }
    
            holder.rbCountry.setText(country);
    
            if(choiceId==position){
                holder.rbCountry.setChecked(true);
            }else{
                holder.rbCountry.setChecked(false);
            }
    
    
            holder.rbCountry.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(holder.rbCountry.isChecked()){
                        choiceId = position;//记住当前选中的下标
                        CountryListAdapter.this.notifyDataSetChanged();
                    }
                }
            });
    
            return convertView;
        }
    
        static class ViewHolder{
            public RadioButton rbCountry;
        }
    
    
        public String getChoiceCountry(){
            return getItem(choiceId);
        }
    }

    3:运行结果

  • 相关阅读:
    amazonS3文件管理工具类
    Building for production... ERROR TypeError: Cannot read property ‘createHash‘ of undefined
    nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)错误解决办法
    Git命令使用总结
    html5-语义化标签(一)
    php的初步了解
    css3 实现圆角边框的border-radius属性和实现阴影效果的box-shadow属性
    css3 transform方法常用属性
    css3 transition属性实现3d动画效果
    css3 3d展示中rotate()介绍与简单实现
  • 原文地址:https://www.cnblogs.com/yshyee/p/4199143.html
Copyright © 2011-2022 走看看