-1

I have seen many examples but none is according to my problem...in my case i have a custom listview with custom adapter the checkboxex are primarily hidden and when option menu button is clicked the checkboxes get visible and the focusability is also set to true and listview no more receive list item click listener...i also have code to save the states of the checkboxes in SparseBooleanArray but the biggest problem is that in the getView() as it doesnt call on checkbox check so the check state is wrongly saved for example if i have firstly 5 items in my list view...the position from the public View getView(int position,View convertView,ViewGroup parent) will be 6 beacuse the last item will be at position 6.so in my code

@Override
    public View getView(final int position,View convertView,ViewGroup parent)
    {
        //currentPosition=position;
        Log.v("Position",""+position);
        Log.v("ConvertView", ""+convertView);
        View row=convertView;

        if(row==null)
        {
            LayoutInflater inflater=((Activity)context).getLayoutInflater();
            row=inflater.inflate(layoutResourceId, parent,false);
            holder=new Feedbacks();
            holder.lvGuestName=(TextView)row.findViewById(R.id.guestName);
            holder.checkBox=(CheckBox)row.findViewById(R.id.clearCheck);

            holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
            {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    if(isChecked)
                    {                           
                        checks.put(position, true);
                        System.out.println("checking at pos:"+position);
                    }
                    else
                    {                           
                        checks.put(position, false);
                        System.out.println("unchecking at pos:"+position);
                    }

                }                   

            });


            row.setTag(holder);

        }       
        else
        {               
            holder=(Feedbacks)row.getTag();

        }

        if(showCheckbox)
        {
            holder.checkBox.setVisibility(View.VISIBLE);        
        }
        else
        {
            holder.checkBox.setVisibility(View.GONE);
        }
        holder.lvGuestName.setText(feedbackList.get(position).getGuestName());
        holder.checkBox.setChecked((checks.get(position)));
        System.out.println("seting check at pos:"+position);    
        return row;                                     

    }

}                   

now when on if(isChecked) {
checks.put(position, true); System.out.println("checking at pos:"+position); }
now even if i click on the 0 position item it will get the position to be 6 i know it because when checkbox is checked/unchecked the getView() will not called and hence checks.put(position,true); will not get the respective position... i know i can easily do it with on listitem click with checkbox focusability false but im curious to learn new things thats why just want to know if any one know the according to my way how can i get the position of the respective checkbox checked in the listview...Any kind of help will be most appreciated and valuable for me... Thanks in advance

Raja Babar
  • 53
  • 3
  • 11

1 Answers1

1

In your code, you have wrong if condition so changed your code like and changed OnCheckedChangeListener to onClickListener.

 @Override
    public View getView(final int position,View convertView,ViewGroup parent)
    {
        View row=convertView;
        Feedbacks holder = null;

        if(row==null)
        {
            LayoutInflater inflater=((Activity)context).getLayoutInflater();
            row=inflater.inflate(layoutResourceId, parent,false);
            holder=new Feedbacks();
            row.setTag(holder);
        }       
        else
        {               
            holder=(Feedbacks)row.getTag();
        }

        holder.lvGuestName=(TextView)row.findViewById(R.id.guestName);
        holder.checkBox=(CheckBox)row.findViewById(R.id.clearCheck);

        holder.checkBox.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if(holder.checkBox.isChecked())
                {                           
                    checks.put(position, true);
                    System.out.println("checking at pos:"+position);
                }
                else
                {                           
                    checks.put(position, false);
                    System.out.println("unchecking at pos:"+position);
                }

            }
        });

        if(showCheckbox)
        {
            holder.checkBox.setVisibility(View.VISIBLE);        
        }
        else
        {
            holder.checkBox.setVisibility(View.GONE);
        }

        holder.lvGuestName.setText(feedbackList.get(position).getGuestName());
        holder.checkBox.setChecked((checks.get(position)));
        System.out.println("seting check at pos:"+position);    
        return row;                                     

    }
Vivek Kumar Srivastava
  • 2,158
  • 1
  • 16
  • 23
  • no it wont work i have tried it...the position you will always get is the no of items currently visible in your listview its because getView() will be refresh if u scroll down... @Vivek – Raja Babar Jun 13 '12 at 10:00
  • 1
    I am using same on my project and its working fine, please test this code by implementing this – Vivek Kumar Srivastava Jun 13 '12 at 10:08
  • @raja I do not understand why this is not working for you. I am sure this will work and this is tested code. And you can downvote for answer. – Vivek Kumar Srivastava Jun 13 '12 at 10:19
  • ok might be you are right but can you please tell how **position** will provide the correct integer value for the position... – Raja Babar Jun 13 '12 at 10:27
  • i have tried the onClickListener before,,,and one thing more i dont understand why you are finding the view after the if condition?? – Raja Babar Jun 13 '12 at 10:29
  • getView() will be called firstly one time just to set the adapter so suppose if you have 5 items in your adapter the position will be 5 untill your view got refreshed(Recycled) – Raja Babar Jun 13 '12 at 10:31
  • one last thing are you sure you have implemented checkboxes in your listview and you haven't check/uncheck using onItemClick..because i guess what you are saying would be rather possible using onItemClick because when item got click you get the relevant position – Raja Babar Jun 13 '12 at 10:33
  • ListView internally manage the value of position, So don't worry about position. In your code, holder.lvGuestName.setText(feedbackList.get(position).getGuestName()); you are getting guestName from feedbackList on basis of position. – Vivek Kumar Srivastava Jun 13 '12 at 10:34
  • Yes I have done lot of time checkBox in ListView. And posted the code from my live project – Vivek Kumar Srivastava Jun 13 '12 at 10:35
  • by the way what does checkbox.isSelected() do??is it same as isChecked() @Vivek – Raja Babar Jun 13 '12 at 11:10
  • sorry change isSelected() to isChecked(). isSelected return the focus state of view – Vivek Kumar Srivastava Jun 13 '12 at 11:35
  • sorry man i didn't try it yet because im currently stuck with arrayList=null surely will try after that and do let you know...thanks for the interest and concern... :) – Raja Babar Jun 13 '12 at 13:18
  • sure..and will accept answer if this will resolve your problem – Vivek Kumar Srivastava Jun 13 '12 at 13:19
  • and finally im just going to implement it your way...and can you please tell me whats the trick behind doing holder.lvGuestName=(TextView)row.findViewById(R.id.guestName); after if(row==null) @Vivek – Raja Babar Jun 13 '12 at 13:43
  • i dont know why my isChecked() is not working fine always else condition executes whether i check/uncheck the checkbox?? – Raja Babar Jun 13 '12 at 14:20
  • its really a mess some time isChecked() execute right and some time not for item 0 and 13 its working fine... :( – Raja Babar Jun 13 '12 at 14:28
  • are u using same code given by me or only chcekBox click code? I suggest you to use all GetView() code code by me – Vivek Kumar Srivastava Jun 14 '12 at 05:10
  • yes im using your code i have 14 items in my list view...and only item at position 0 and 13 are working right with the code public void onClick(View v) { if(holder.checkBox.isChecked()) { checks.put(position, true); System.out.println("checking at pos:"+position); } else { checks.put(position, false); System.out.println("unchecking at pos:"+position); – Raja Babar Jun 14 '12 at 05:36
  • all other items ran into else body whether i check or uncheck my log cat says unchecking at pos:... – Raja Babar Jun 14 '12 at 05:37
  • one thing im surprise about onClick thing its atleast providing the right position??i dont unserstand why didn't checkedChangeListener not doing that?? – Raja Babar Jun 14 '12 at 05:40
  • congratulations i have tried myCheckedChange listner instead of onClick like you did after the else and its perfectly working right :).....thanks alot you solved my problem man... – Raja Babar Jun 14 '12 at 05:44
  • but please can you explain a bit how that magic happend after the else body..??what actually hapening i still didnt know – Raja Babar Jun 14 '12 at 05:45
  • In ListView, same row reused when user scroll the ListView. And position internally managed by ListView. So if you only put if and do not put else body then next time row display the wrong content when row reused. You will better understand when you play with some example of listView. – Vivek Kumar Srivastava Jun 14 '12 at 06:02
  • thanks alot vivek...one more thing if you know and can help me..now i want to remove the list items(Which are checked) when i click clear button i have implementation for that...it almost working fine but some items wont get remove...can i edit my question or post a new one?? – Raja Babar Jun 14 '12 at 06:19
  • You are already maintain the checked items array, so just get checked position from that array and then remove those items from feedbackList and reinitialize checked array and new size. And in last called NotifydatasetChanged in your adapter. And I think, you need to solve this type of problem yourself. Because this is a logical problem, So developer on stackOverFlow do not take interest on this type of question. – Vivek Kumar Srivastava Jun 14 '12 at 06:26
  • i have posted the question if you can have a look on that i have done what you are saying – Raja Babar Jun 14 '12 at 06:27