Android listview ImageButton onclick错误

时间:2015-03-10 13:41:06

标签: android listview android-listview imagebutton android-imagebutton

我在listview中使用了一个图像按钮,一切正常。但是当我试图在其点击的图像按钮上更改背景资源时,我的应用程序崩溃了。这是我的代码。

  public View getView(final int position, View convertView,
            ViewGroup parent) {

        if (convertView == null) {

            // mCheckStates = new SparseBooleanArray(name1.size());
            mInflater = (LayoutInflater) getActivity().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);

            convertView = mInflater.inflate(R.layout.row, null);

            holder = new ViewHolder();

            holder.tv = (TextView) convertView.findViewById(R.id.textView1);
            holder.tv1 = (TextView) convertView
                    .findViewById(R.id.textView2);
            holder.b = (ImageButton) convertView
                    .findViewById(R.id.btn_invite);

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

        holder.tv.setText(name1.get(position));
        holder.tv1.setText(phno1.get(position));
         holder.b.setTag(position);

        holder.b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                int pos = (Integer) v.getTag();

                RelativeLayout rl = (RelativeLayout)v.getParent();
                holder.b = (ImageButton)rl.getChildAt(0);
                holder.b.setBackgroundResource(R.drawable.ic_invited);

               });

        return convertView;
    }

1 个答案:

答案 0 :(得分:-3)

以下是正确的流程 -

  

public View getView(final int position,View convertView,               ViewGroup父级){

    if (convertView == null) {

        // mCheckStates = new SparseBooleanArray(name1.size());
        mInflater = (LayoutInflater) getActivity().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);

        convertView = mInflater.inflate(R.layout.row, null);

        holder = new ViewHolder();

        holder.tv = (TextView) convertView.findViewById(R.id.textView1);
        holder.tv1 = (TextView) convertView
                .findViewById(R.id.textView2);
        holder.b = (ImageButton) convertView
                .findViewById(R.id.btn_invite);

    holder.tv.setText(name1.get(position));
    holder.tv1.setText(phno1.get(position));
    holder.b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            int pos = (Integer) v.getTag();

            RelativeLayout rl = (RelativeLayout)v.getParent();
            holder.b = (ImageButton)rl.getChildAt(0); // if your error is layout can't be casted to image Button, then in this line may be you are getting an unexpected layout view instead of a ImageButton
            holder.b.setBackgroundResource(R.drawable.ic_invited);

           });
        holder.b.setTag(position);

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


    return convertView;
}