0

在自定义适配器列表视图中单击重复项反应时遇到问题。实际上,onClick 中的方法应用了单击的元素,如果我向下滚动列表,则某些元素会重复相同。
因此,当我尝试使用 Tag 解决它时,它对我没有帮助。我正在尝试使用在 Stack 和 Google 上找到的不同实现,但它也无济于事。那是我的欲望实现代码

public class CAChanal extends BaseAdapter {
    private Context context;

    protected MainActivity MAcontext;

    public CAChanal(MainActivity _context){
        MAcontext = _context;
    }

    public CAChanal(Context context) { 
        this.context = context;
    }

    ArrayList<DMChanal> listArray;
    public CAChanal() {
        listArray = new ArrayList<DMChanal>();
    }
    @Override
    public int getCount() {
        return listArray.size(); 
    }

    @Override
    public Object getItem(int i) {
        return listArray.get(i); 
    }

    @Override
    public long getItemId(int i) {
        return i; 
    }

    @Override
    public View getView(int index,View view, ViewGroup parent) {
         final ViewHolder holder ; 
        if (view == null) { 
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            view = inflater.inflate(R.layout.chanal_template, null);
            holder = new ViewHolder(); 
            holder.chanalElement=(ChanalView)view.findViewById(R.id.chanalView_forList);
            view.setTag(holder);
        }else
        {
             holder = (ViewHolder)view.getTag();
        }


         holder.chanalElement.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(!holder.chanalElement.getChanalStatus())
                {
                     holder.chanalElement.enableChanal();
                }else
                {
                     holder.chanalElement.disableChanal();
                }
            }
        });     
        return view;
    } 
    static class ViewHolder {
         ChanalView chanalElement;
        }

    public Object getFilter() {

        return null;
    } 


}  

任何人都可以帮助我解决我的问题并解释我的代码中可能出现的问题。PS:ChanalView 是我的自定义视图

4

1 回答 1

0
public class MyAdapter extends BaseAdapter {

     List<DMChanal> listArray;
     List<Boolean> chanalElementStates = new ArrayList<Boolean>();

     ...

     @Override
     public View getView(final int index,View view, ViewGroup parent) {
         final ViewHolder holder ; 
         if (view == null) { 
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            view = inflater.inflate(R.layout.chanal_template, null);
            holder = new ViewHolder(); 
            holder.chanalElement = (ChanalView)view.findViewById(R.id.chanalView_forList);
            view.setTag(holder);
        } else {
            holder = (ViewHolder)view.getTag();
        } 

        holder.chanalElement.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean enabled = !chanalElementStates[index];
                chanalElementStates[index] = enabled;
                updateEnabledState(holder, enabled);
            }
        }); 

        // What you were missing. You always have to reset the state to 
        // what you want because the views are recycled.
        updateEnabledState(holder, chanalElementStates[index]);

        return view;
    } 

    private void updateEnabledState(ViewHolder holder, boolean enable) {
        if (enable)
            holder.chanalElement.enableChanal();
        else
            holder.chanalElement.disableChanal();    
    }
}
于 2014-10-28T16:30:00.603 回答