3

I have created an application containing radiogroup in each row of listview. The problem is that when i scroll the listview, the selection of the radiogroup changes as the oncheckedevent for the radiogroup fires multiple times.

Here's my getView function :

    @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;
    }

And below is the trace that i get in the logcat when i scroll the listview :

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

Any help appreciated.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
Sandeep
  • 667
  • 7
  • 25

1 Answers1

3

I had already answered it in previous answer. So you can have a look at this answer.

Community
  • 1
  • 1
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242