0

我将Array中的Togglebuttons动态添加到我的RadioGroup中。添加工作正常,每个不同的列表项工作正常,但是当我滚动时,检查状态变回了。我怎样才能解决这个问题?

这是我的ListAdapter类:

public class BezoekverslagDetailsListAdapter extends ArrayAdapter<Vraag>{
public Context context;
public List<Vraag> vragen;
ViewHolder holder;;

public BezoekverslagDetailsListAdapter(Activity context, List<Vraag> vragen){
    super(context, R.layout.vraag_item, vragen);
    this.context = context;
    this.vragen = vragen;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {     
    convertView = null;
    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.vraag_item, null, false);

        holder = new ViewHolder();
        holder.vraag = (TextView)convertView.findViewById(R.id.tv_vraag);
        holder.group = (RadioGroup)convertView.findViewById(R.id.toggleGroup);
        holder.position = position;
        holder.toggle = new ToggleButton(context);

        convertView.setTag(holder); 
    }
    else{
        holder = (ViewHolder) convertView.getTag(); 
    }       
    final Vraag vraag = vragen.get(position);
    holder.vraag.setText(vraag.getVraag());

    holder.group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
        public void onCheckedChanged(RadioGroup group, int checkedId){
            for (int j = 0; j < group.getChildCount(); j++) {
                final ToggleButton view = (ToggleButton) group.getChildAt(j);
                view.setChecked(view.getId() == checkedId);    

                vraag.getAntwoorden().get(view.getId()).setSelected(view.getId() == checkedId);

            }
        }
    });

    for(final Antwoord a : vraag.getAntwoorden()){
        holder.toggle = new ToggleButton(context);
        holder.toggle.setText(a.getAntwoord());
        holder.toggle.setTextOn(a.getAntwoord());
        holder.toggle.setTextOff(a.getAntwoord());
        holder.toggle.setId(a.getId());
        holder.toggle.setChecked(a.getSelected());

        if(a.getVisible() == false){
            holder.toggle.setVisibility(ToggleButton.INVISIBLE);
        }       

        holder.toggle.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                // your click actions go here
                ((RadioGroup)view.getParent()).check(view.getId());
            }
        });

        holder.group.addView(holder.toggle);        
    }   

    return convertView;
}

static class ViewHolder {
      TextView vraag;
      RadioGroup group;
      ToggleButton toggle;
      int position;
    }

}

4

3 回答 3

0

而不是这个:

final Vraag vraag = vragen.get(position);
holder.vraag.setText(vraag.getVraag());

尝试这个 :

final Vraag vraag = vragen.get(holder.position);
holder.vraag.setText(vraag.getVraag());

在滚动列表 View 时,它有一个奇怪的行为,即根据当前存在于 View 中的 Items 计算位置。我曾经遇到过这种奇怪的 View 。我将该特定视图的标签动态设置为视图的实际位置,然后执行View.get(tag).

于 2013-09-05T12:02:06.633 回答
0

我找到了另一种方式,现在它保留了选中的切换按钮,但现在的问题是按钮从位置改变了。

这是代码:

public class BezoekverslagDetailsListAdapter extends ArrayAdapter<Vraag>{
public Context context;
public List<Vraag> vragen;
ViewHolder holder;
public BezoekverslagDetailsListAdapter(Activity context, List<Vraag> vragen){
    super(context, R.layout.vraag_item, vragen);
    this.context = context;
    this.vragen = vragen;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {     
    View view;
    //convertView = null;

    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.vraag_item, null, false);

        holder = new ViewHolder();
        holder.vraag = (TextView)view.findViewById(R.id.tv_vraag);
        holder.group = (RadioGroup)view.findViewById(R.id.toggleGroup);
        holder.position = position;
        holder.toggle = new ToggleButton(context);

        for(final Antwoord a : vragen.get(holder.position).getAntwoorden()){
            holder.toggle = new ToggleButton(context);
            holder.toggle.setText(a.getAntwoord());
            holder.toggle.setTextOn(a.getAntwoord());
            holder.toggle.setTextOff(a.getAntwoord());
            holder.toggle.setId(a.getId());
            holder.toggle.setChecked(a.getSelected());

            if(a.getVisible() == false){
                holder.toggle.setVisibility(ToggleButton.INVISIBLE);
            }       

            holder.toggle.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    // your click actions go here
                    ((RadioGroup)view.getParent()).check(view.getId());
                    a.setSelected(holder.toggle.isChecked());
                }
            });

            holder.group.addView(holder.toggle);    

        }

        holder.group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            public void onCheckedChanged(RadioGroup group, int checkedId){
                for (int j = 0; j < group.getChildCount(); j++) {
                    final ToggleButton view = (ToggleButton) group.getChildAt(j);
                    view.setChecked(view.getId() == checkedId);      
                }
            }
        });
        view.setTag(holder);    
    }
    else{
        view = convertView;
    }       

    ViewHolder viewholder = (ViewHolder)view.getTag();

    viewholder.vraag.setText(vragen.get(holder.position).getVraag());

    return view;
}

static class ViewHolder {
      TextView vraag;
      RadioGroup group;
      ToggleButton toggle;
      int position;
    }

}

于 2013-09-06T06:56:08.320 回答
0

我终于解决了这一切!

这是我的最终代码:

public class BezoekverslagDetailsListAdapter extends ArrayAdapter<Vraag>{
public Context context;
public List<Vraag> vragen;
ViewHolder holder;
int i;
public BezoekverslagDetailsListAdapter(Activity context, List<Vraag> vragen){
    super(context, R.layout.vraag_item, vragen);
    this.context = context;
    this.vragen = vragen;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {     
    View view = null;
    convertView = null;
    final Vraag vraag = vragen.get(position);

    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.vraag_item, null, false);

        holder = new ViewHolder();
        holder.vraag = (TextView)view.findViewById(R.id.tv_vraag);
        holder.group = (RadioGroup)view.findViewById(R.id.toggleGroup);
        holder.position = position;
        holder.toggle = new ToggleButton(context);

        for(final Antwoord a : vraag.getAntwoorden()){
            holder.toggle = new ToggleButton(context);
            holder.toggle.setText(a.getAntwoord());
            holder.toggle.setTextOn(a.getAntwoord());
            holder.toggle.setTextOff(a.getAntwoord());
            holder.toggle.setId(i);
            holder.toggle.setChecked(a.getSelected());

            if(a.getVisible() == false){
                holder.toggle.setVisibility(ToggleButton.INVISIBLE);
            }       

            holder.toggle.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    // your click actions go here
                    ((RadioGroup)view.getParent()).check(view.getId());

                    if(a.getSelected()==true){
                        a.setSelected(false);
                    }
                    else if(a.getSelected()==false){
                        for(Antwoord b: vraag.getAntwoorden()){
                            if(b.getSelected()==true){
                                b.setSelected(false);
                            }
                        }
                        a.setSelected(true);
                        vraag.setAntwoord(a.getAntwoord());
                    }
                }
            });
            i++;
            holder.group.addView(holder.toggle);                
        }

        view.setTag(holder);
    }
    else{
        view = convertView;
        holder = (ViewHolder) view.getTag();
    }
    ViewHolder viewholder = (ViewHolder) view.getTag();

    viewholder.vraag.setText(vraag.getVraag());

    holder.group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
        public void onCheckedChanged(RadioGroup group, int checkedId){
            for (int j = 0; j < group.getChildCount(); j++) {
                final ToggleButton view = (ToggleButton) group.getChildAt(j);
                view.setChecked(view.getId() == checkedId);
                //vragen.get(holder.position).getAntwoorden().get(view.getId()).setSelected(view.getId() == checkedId);
            }
        }
    });

    view.setTag(viewholder);    

    return view;
}

static class ViewHolder {
      TextView vraag;
      RadioGroup group;
      ToggleButton toggle;
      int position;
    }

}

于 2013-09-10T08:43:56.727 回答