1

I have a ListView in my application to show list of records. I have a Button and some RadioButton and they need to be visible or invisible based on some condition. When the list gets scrolled, the last view becomes a duplicate and the condition does not work properly. What is happening and how do I fix it?

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    ViewHolder holder = null;

    if (convertView == null) {

        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.approval_list, null);

        //<<<<<<<<<<<<<<<<<<<Initialization of ListView Components 
        //holder.indivListvLayout = (LinearLayout)convertView.findViewById(R.id.indRevLayout);
         holder.tv_com_title = (TextView) convertView.findViewById(R.id.tv_com_title);
         holder.tv_designation = (TextView) convertView.findViewById(R.id.tv_designation);
         holder.et_commnents = (EditText) convertView.findViewById(R.id.et_commnents);
         holder.rb_rev = (RadioButton) convertView.findViewById(R.id.rb_rev);
         holder.rb_rej = (RadioButton) convertView.findViewById(R.id.rb_rej);
         holder.btn_modify = (Button) convertView.findViewById(R.id.btn_modify);
                        //>>>>>>>>>>>>>>>>>>End Initialization

                        //<<<<<<<<<<<<<<<<<<Make visible radio button according to user_group code
         //List User Group Code
         String user_group_code = mDisplayedValues.get(position).user_group_code;
         //Log.e("User Group code from list", user_group_code);
         if("RO".equals(user_group_code)){
            holder.rb_app_recom.setVisibility(View.VISIBLE);
         }else if("RM".equals(user_group_code)){
            holder.rb_app_recom.setVisibility(View.VISIBLE);
         }else("BDM".equals(user_group_code)){
            holder.rb_app_recom.setVisibility(View.VISIBLE);
            holder.rb_rej_req.setVisibility(View.VISIBLE);
         }

                        //<<<<<<<<<<<<<<<<<<Selection of radio button according to flag
         //Getting flag value
         String approve_flag = mDisplayedValues.get(position).approve_flg;

         if(approve_flag.equals("A")){
            holder.rb_approved.setChecked(true);
         }else (approve_flag.equals("R")){
            holder.rb_rev.setChecked(true);
         }
         //>>>>>>>>>>>>>>>>>>>End selecting radio button

                        //<<<<<<<<<<<<<<<<<<<Make radio button click able
         //String user_group_code2 = user_session_sp.getString("user_group_code", " ");
         if(user_group_code2.equals(mDisplayedValues.get(position).user_group_code)){
            holder.rb_approved.setClickable(true);
            holder.rb_rev.setClickable(true);
         }else{
            holder.rb_approved.setClickable(false);
            holder.rb_rev.setClickable(false);
         }
         //>>>>>>>>>>>>>>>>>>>End selecting radio button clekcable Setting
         convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.tv_com_title.setText(mDisplayedValues.get(position).groucode +" "+ mDisplayedValues.get(position).comments_by+" "+ mDisplayedValues.get(position).designation);
    holder.tv_designation.setText(mDisplayedValues.get(position).user_group_code);
    holder.et_commnents.setText(mDisplayedValues.get(position).comments);

    return convertView;
}   
Daniel
  • 2,355
  • 9
  • 23
  • 30

3 Answers3

1

try like this.

the problem was your condition statements were executing for first item only (initialization time when convertedview is null) otherwise it'll not be executed.

 public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            ViewHolder holder = null;

            if (convertView == null) {

                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.approval_list, null);

                //<<<<<<<<<<<<<<<<<<<Initialization of ListView Components 
                //holder.indivListvLayout = (LinearLayout)convertView.findViewById(R.id.indRevLayout);
                holder.tv_com_title = (TextView) convertView.findViewById(R.id.tv_com_title);
                holder.tv_designation = (TextView) convertView.findViewById(R.id.tv_designation);
                holder.et_commnents = (EditText) convertView.findViewById(R.id.et_commnents);
                holder.rb_rev = (RadioButton) convertView.findViewById(R.id.rb_rev);
                holder.rb_rej = (RadioButton) convertView.findViewById(R.id.rb_rej);
                holder.btn_modify = (Button) convertView.findViewById(R.id.btn_modify);
                //>>>>>>>>>>>>>>>>>>End Initialization

                //>>>>>>>>>>>>>>>>>>>End selecting radio button clekcable Setting
                    convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            //<<<<<<<<<<<<<<<<<<Make visible radio button according to user_group code
                //List User Group Code
                String user_group_code = mDisplayedValues.get(position).user_group_code;
                //Log.e("User Group code from list", user_group_code);
                if("RO".equals(user_group_code)){
                    holder.rb_app_recom.setVisibility(View.VISIBLE);
                }else if("RM".equals(user_group_code)){
                    holder.rb_app_recom.setVisibility(View.VISIBLE);
                }else("BDM".equals(user_group_code)){
                    holder.rb_app_recom.setVisibility(View.VISIBLE);
                    holder.rb_rej_req.setVisibility(View.VISIBLE);
                }

                //<<<<<<<<<<<<<<<<<<Selection of radio button according to flag
                //Getting flag value
                String approve_flag = mDisplayedValues.get(position).approve_flg;

                if(approve_flag.equals("A")){
                    holder.rb_approved.setChecked(true);
                }else (approve_flag.equals("R")){
                    holder.rb_rev.setChecked(true);
                }
                //>>>>>>>>>>>>>>>>>>>End selecting radio button

                //<<<<<<<<<<<<<<<<<<<Make radio button click able
                //String user_group_code2 = user_session_sp.getString("user_group_code", " ");
                if(user_group_code2.equals(mDisplayedValues.get(position).user_group_code)){
                    holder.rb_approved.setClickable(true);
                    holder.rb_rev.setClickable(true);
                }else{
                    holder.rb_approved.setClickable(false);
                    holder.rb_rev.setClickable(false);
                }

            holder.tv_com_title.setText(mDisplayedValues.get(position).groucode +" "+ mDisplayedValues.get(position).comments_by+" "+ mDisplayedValues.get(position).designation);
            holder.tv_designation.setText(mDisplayedValues.get(position).user_group_code);
            holder.et_commnents.setText(mDisplayedValues.get(position).comments);

          return convertView;
        }   
Jayanth
  • 5,954
  • 3
  • 21
  • 38
  • I have apply all condition outside if (convertView == null) {convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); }. But no change I found. –  Jan 16 '17 at 11:01
  • yeah, then only those conditions executes for each row – Jayanth Jan 16 '17 at 11:04
  • see your code in question currently all conditions are inside `if(convertedView == null){}` block – Jayanth Jan 16 '17 at 11:05
  • I said ............ I have applied all condition outside of if(convertedView == null){} first time. Then when it show problem then I put it inside. So inside and outside both not working.. –  Jan 16 '17 at 11:17
  • debug and see whether you are getting correct data from `mDisplayedValues` otherwise everything is good – Jayanth Jan 16 '17 at 11:19
  • There is no problem at all............... but some times when scroll it not work properly ..........this is the problem... –  Jan 16 '17 at 11:59
  • There is no Error............ I print this log like this. .0 position view some time from if and some time form else when scroll . Whay i dont know ...Log.e("EEEEEEEEEE", "from if.. "+position); convertView.setTag(holder); } else { Log.e("EEEEEEEEEE", "from else.."+position); holder = (ViewHolder) convertView.getTag(); } –  Jan 16 '17 at 12:26
0

While using viewholder pattern you have to handle every situation:

if(approve_flag.equals("A")){
   holder.rb_approved.setChecked(true);
else {
   holder.rb_approved.setChecked(false);
}
if (approve_flag.equals("R")){
   holder.rb_rev.setChecked(true);
}
else {
   holder.rb_rev.setChecked(false);
}
nullvoid
  • 121
  • 8
  • what is the difference between if(condition){}else{} if(condition){}else{} and if(condition){}else if(condition)(condition){}else{} –  Jan 18 '17 at 04:29
0

In ViewHolder pattern, only if statement causes unexpected results

So better use, if along with else

Example:

if(condition){
   //special execution
}else{
   //default execution
}
Raj Yadav
  • 9,677
  • 6
  • 35
  • 30