CustomListViewAdapter具有illegalStateException

时间:2014-07-08 02:26:45

标签: android onclicklistener onitemclicklistener custom-adapter

我目前正在使用CustomListViewAdapter,这里是我在CustomListViewAdapter.java中的代码中的getView方法的摘录。我认为问题出在这里,大多数可能在行{final View view = super.getView(...)}中,因为我已经使用调试模式进行测试。怎么解决?

描述:我想要做的是,当AND只在行中的switchImage上单击时,相应的switchImage和lockImage会改变它们的图像。 我试过了,但是我的MainActivity.class中的OnItemClickListener不允许我指定只有在单击switchImage时才会出现效果(单击行中的任何项目后会出现效果)。

THX获取任何帮助和评论。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final View view = super.getView(position, convertView, parent);
    final ImageView switchImage = (ImageView) view.findViewById(R.id.onOffSwitch);
    final ImageView lockImage = (ImageView) view.findViewById(R.id.lock);
    switchImage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (flag == false) {
                flag = true;
...
...
...
            }
        }
    });

    RowItem rowItem = getItem(position);

    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.lock = (ImageView) convertView.findViewById(R.id.lock);
        holder.appName = (TextView) convertView.findViewById(R.id.appName);
...
...
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.lock.setImageResource(rowItem.getLockId());
...
...
    return view;
}

1 个答案:

答案 0 :(得分:0)

问题解决了。我是这样做的。

@Override
public View getView(int position, View convertView, ViewGroup parent) {     
    RowItem rowItem = getItem(position);

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

        holder = new ViewHolder();
        holder.onOffSwitch = (ImageView) convertView.findViewById(R.id.onOffSwitch);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // Newly added code that works.
    final ImageView onOffSwitch = (ImageView)
    convertView.findViewById(R.id.onOffSwitch);
    onOffSwitch.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            } else {
            }
        }
    });

    return convertView;
}