0

I use an imagebutton in a listview and everything was working fine. But when i tried to change the background resource on the imagebutton on its click i got my app crashed. here is my code.

  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;
    }
since k saji
  • 893
  • 9
  • 20

1 Answers1

-3

This following would be right flow-

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);

    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;
}
Amit K. Saha
  • 5,871
  • 2
  • 27
  • 35
  • Can you explain how your code is different than the OP's and *why* it should work instead of just pasting code? – codeMagic Mar 10 '15 at 13:52
  • its not just pasting the code. the whole part about setting text & setting click listener have been moved when the convertview is null. Plus, I just wrote almost the same thing just a few mins ago. found a silly mistake like the question while we were solving a bug. Even if my solution may not be 100% correct[he didn't provide the stack trace], can you please describe why did I get downgraded? – Amit K. Saha Mar 10 '15 at 13:55
  • Sure looks like just pasting code when your whole explanation is "This following would be right flow-" – codeMagic Mar 10 '15 at 13:57
  • Did you have any look on the flow of my pasted code? – Amit K. Saha Mar 10 '15 at 13:57
  • 1
    I've told you why *I* downvoted but I can't speak for anyone else. An explanation helps the OP a lot more than code and, believe it or not, it can also help you. No, I didn't look at your flow of pasted code nor do I need to just to know that that's all it is. – codeMagic Mar 10 '15 at 14:00
  • What I tried was to fix code by using his pasted code. So what you are meaning that instead of doing so we should always give theoretical answers first, afterward , if we get time we may help him actually by solving the problem. I agree with you in this point. Explaining brings better help. But not explaining bringing downvotes? do you agree with that? Pardon my poor English – Amit K. Saha Mar 10 '15 at 14:05
  • What I am saying is just dropping code and not explaining it for someone who already doesn't understand the problem is not that helpful. You should at least explain *what* you changed and *why*. Once more, I did explain my downvote with my very first comment even though, while it isn't that helpful to not explain them, users are free to downvote and not explain if they wish. – codeMagic Mar 10 '15 at 14:14
  • Why are u guys arguing instead helping him to solve the issue? :P – Kavin Prabhu Mar 10 '15 at 14:20
  • Lets say, we aren't arguing @KevinChris, a new comer is getting some experience from some experienced one :) And about the problem, you & I , both of us pointed the problem already in the comment, didn't we? :) – Amit K. Saha Mar 10 '15 at 14:23
  • @KevinChris I wasn't arguing but explaining how to provide better answers instead. – codeMagic Mar 10 '15 at 14:42
  • Okie okie guys let's check +sinceksaji resolved the issue or not! – Kavin Prabhu Mar 10 '15 at 14:43