listview中的radiogroup上的OnChecked事件多次被触发

时间:2011-10-02 16:15:49

标签: android

我在listview的每一行创建了一个包含radiogroup的应用程序。问题是当我滚动列表视图时,无线电组的选择会随着放射线组的oncheckedevent多次激发而改变。

这是我的getView函数:

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new WeatherHolder();

            holder.imgIcon = (ImageView)row.findViewById(R.id.icon);
            holder.txtTitle = (TextView)row.findViewById(R.id.toptext);
            holder.rdgCategory = (RadioGroup)row.findViewById(R.id.radiogroup);

            RadioGroup.OnCheckedChangeListener rdGrpCheckedListener = new RadioGroup.OnCheckedChangeListener(){

             @Override
             public void onCheckedChanged(RadioGroup group, int checkedId) {
                 // TODO Auto-generated method stub
//                   setCategoryinList(position, checkedId);
                    switch (checkedId){
                    case R.id.option1:
                        Log.i("WeatherAdapter","Data set index : " + position + " category : 1");
                        data.get(position).setCategory(1);
                        break;
                    case R.id.option2:
                        Log.i("WeatherAdapter","Data set index : " + position + " category : 2");
                        data.get(position).setCategory(2);
                        break;
                    default:
                        Log.i("WeatherAdapter","Data set index : " + position + " category : 0");
                        data.get(position).setCategory(0);
                        break;
                    }
                }
            };

            holder.rdgCategory.setOnCheckedChangeListener(rdGrpCheckedListener);

            row.setTag(holder);
        }
        else
        {
            holder = (WeatherHolder)row.getTag();
        }

        Weather weather = data.get(position);
        holder.txtTitle.setText(weather.getAppName());
        holder.imgIcon.setImageDrawable(weather.getIcon());
        int objCategory = weather.category;
        Log.i("WeatherAdapter","Data get name : " + weather.getAppName() + " index : " + position + " category : " + objCategory);
        switch (objCategory)
        {
            case option1:
                holder.rdgCategory.check(R.id.option1);
                break;
            case option2:
                holder.rdgCategory.check(R.id.option2);
                break;
            default:
                holder.rdgCategory.check(R.id.none);
                break;
        }
        return row;
    }

下面是滚动列表视图时我在logcat中获得的跟踪:

10-02 21:31:29.844: INFO/WeatherAdapter(28361): Data get name : ProgressBar index : 11 category : 0
10-02 21:31:53.399: INFO/WeatherAdapter(28361): Data set index : 10 category : 1
10-02 21:31:53.399: INFO/WeatherAdapter(28361): Data set index : 10 category : 0
10-02 21:31:53.399: INFO/WeatherAdapter(28361): Data set index : 10 category : 0

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:3)

我在之前的回答中已经回答过了。所以你可以查看this answer