Android的 - 取得一个ListView单选按钮的值 [英] Android - Get values of Radio Buttons in a ListView

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

问题描述

我设计的,据我的Java的理解,应该已经制定了罚款的情景,但遗憾的是没有。该方案在code解释:

I designed a scenario which, according to my understanding of Java, should have worked out fine but unfortunately didn't. The scenario is explained in code:

ListViewAdapter

public final class ListViewAdapter extends BaseAdapter {

    private Context context;
    private RadioGroup[] radioGroups;
    private List<String> listOfData;

    public OneForAllListViewAdapter(Context context, List<String> listOfData) {
        super();
        this.context = context;
        this.radioGroups = new RadioGroup[listOfData.size()];
        this.listOfData = listOfData;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final String timelyOfferedStr = "Yes";
        final String lateOfferedStr = "Yes but late";
        final String notOfferedStr = "No";
        final String pName = listOfData.get(position);

        if(convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.one_for_all_list_item, parent, false);
        }

        TextView pNameTextView = (TextView) convertView.findViewById(R.id.one_for_all_p_name_TextView);
        RadioButton timelyOffered = (RadioButton) convertView.findViewById(R.id.one_for_all_timely_offered);
        RadioButton lateOffered = (RadioButton) convertView.findViewById(R.id.one_for_all_late_offered);
        RadioButton notOffered = (RadioButton) convertView.findViewById(R.id.one_for_all_not_offered);

        this.radioGroups[position] = (RadioGroup) convertView.findViewById(R.id.one_for_all_radio_group);

        pNameTextView.setText(pName);
        timelyOffered.setText(timelyOfferedStr);
        lateOffered.setText(lateOfferedStr);
        notOffered.setText(notOfferedStr);

        return convertView;
    }

    @Nullable
    public ThatStatus[] getThoseStatuses()
    {
        ThatStatus[] thoseStatuses = new ThatStatus[radioGroups.length];

        for(int i=0; i<radioGroups.length; i++) {
            int selectedRadioButton = radioGroups[i].getCheckedRadioButtonId();

            switch (selectedRadioButton) {
                case R.id.one_for_all_timely_offered:
                    thoseStatuses [i] = ThatStatus.TimelyOffered;
                    break;
                case R.id.one_for_all_late_offered:
                    thoseStatuses [i] = ThatStatus.Offered;
                    break;
                case R.id.one_for_all_not_offered:
                    thoseStatuses [i] = ThatStatus.NotOffered;
                    break;
                default:
                    return null;
            }
        }

        return thoseStatuses;
    }
}

以上code到注意的重要一点是这一行:

The important thing to note in above code is this line:

this.radioGroups [位置] =(RadioGroup中)convertView.findViewById(R.id.one_for_all_radio_group);

我保存所有 RadioGroup中 S IN RadioGroup中的数组,并在 getThoseStatuses()我试图让检查单选期从那些 RadioGroup中秒。但 radioGroups [I] .getCheckedRadioButtonId()总是返回-1我

I am saving all the RadioGroups in an array of RadioGroup, and in getThoseStatuses() I am trying to get the checked RadioButtons from those RadioGroups. But radioGroups[i].getCheckedRadioButtonId() always returns me -1.

我失去了一些Java概念?什么似乎是这里的问题?

Am I missing some Java concept? What seems to be the problem here?

推荐答案

尝试创建一个视图海达模式为您Apapter。

Try to create a View Houlder Pattern for your Apapter.

例如:改变你的getView为

Eg : change your getView to

 @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            Viewhoulder viewHoulder = null;
            final String timelyOfferedStr = "Yes";
            final String lateOfferedStr = "Yes but late";
            final String notOfferedStr = "No";
            final String pName = listOfData.get(position);
            if(convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.one_for_all_list_item, parent, false);
                viewHoulder  = new Viewhoulder ();
                viewHoulder.pNameTextView = (TextView) convertView.findViewById(R.id.one_for_all_p_name_TextView);
                viewHoulder.timelyOffered = (RadioButton) convertView.findViewById(R.id.one_for_all_timely_offered);
                viewHoulder.lateOffered = (RadioButton) convertView.findViewById(R.id.one_for_all_late_offered);
                viewHoulder.notOffered = (RadioButton) convertView.findViewById(R.id.one_for_all_not_offered);
                viewHoulder.radioGroup = (RadioGroup) convertView.findViewById(R.id.one_for_all_radio_group);
            }
            else
              viewHoulder  = (Viewhoulder )convertView .getTag();
            this.radioGroups[position] = viewHoulder .radioGroup ;
            viewHoulder . pNameTextView.setText(pName);
            viewHoulder .timelyOffered.setText(timelyOfferedStr);
            viewHoulder .lateOffered.setText(lateOfferedStr);
            viewHoulder .notOffered.setText(notOfferedStr);
            return convertView;
        }

和创建您的适配器这里面Viewhoulder类

And create this Viewhoulder class inside your adapter

static class Viewhoulder {
   private TextView pNameTextView;
   private RadioButton timelyOffered;
   private RadioButton lateOffered ;
   private RadioButton notOffered;
   private RadioGroup radioGroup;
}

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

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