在BaseAdapter中的getView内部条件不正常?

时间:2017-01-16 10:39:37

标签: android listview baseadapter

我的应用程序中有一个ListView来显示记录列表。我有一个Button和一些RadioButton,根据某些条件,它们需要是可见的或不可见的。滚动列表时,最后一个视图变为重复,条件无法正常工作。发生了什么,我该如何解决?

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

3 个答案:

答案 0 :(得分:0)

尝试这样。

问题是您的条件语句仅针对第一项执行(convertview为null时的初始化时间)否则它将不会被执行。

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

答案 1 :(得分:0)

使用视图模式时,您必须处理所有情况:

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

答案 2 :(得分:0)

ViewHolder模式下,只有if语句会导致意外结果

更好地使用ifelse

示例:

if(condition){
   //special execution
}else{
   //default execution
}