onClick of Image按钮值改变但在10 20处重复

时间:2015-03-31 20:38:44

标签: android listview

我有6个图像按钮,点击它来改变,但是在第10个位置重复。我很无能,但我确信它与适配器的getView有关。因此,我发布了适配器的代码。帮助将不胜感激。

public class ListAvailibilityAdapter extends BaseAdapter {

     Context context;
     int layoutResourceId;

     protected List<UserAvailibilityModel> listAvailibility;
     LayoutInflater inflater;

     public ListAvailibilityAdapter(Context context, List<UserAvailibilityModel> listAvailibility) {
         this.listAvailibility = listAvailibility;
         this.inflater = LayoutInflater.from(context);
         this.context = context;
      }

     public int getCount() {
         if(listAvailibility.size()<=0)
            return 1;
         Log.d("SIZE OF ARRAY-->", String.valueOf(listAvailibility.size()));
         return listAvailibility.size();
     }

     public UserAvailibilityModel getItem(int position) {
         return listAvailibility.get(position);
     }

    /* public List<UserAvailibilityModel> getList(int position) {
         return listAvailibility;
     }*/

     @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
         return listAvailibility.get(position).getImageMorning();
        }

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

         ViewHolder holder;
         View vi = convertView;
         if (convertView == null) {

         holder = new ViewHolder();
         convertView = this.inflater.inflate(R.layout.layout_list_availability_item,parent, false);

         holder.txtDate = (TextView) convertView.findViewById(R.id.lbl_date);
         holder.txtDay = (TextView) convertView.findViewById(R.id.lbl_day);
         holder.imgBtnMorning = (ImageButton) convertView.findViewById(R.id.img_btn_morning);
         holder.imgBtnAfterNoon = (ImageButton) convertView.findViewById(R.id.img_btn_afernoon);
         holder.imgBtnEvening = (ImageButton) convertView.findViewById(R.id.img_btn_evening);

         holder.imgBtnMorningChecked = (ImageButton) convertView.findViewById(R.id.img_btn_morning_checked);
         holder.imgBtnAfterNoonChecked = (ImageButton) convertView.findViewById(R.id.img_btn_afernoon_checked);
         holder.imgBtnEveningChecked = (ImageButton) convertView.findViewById(R.id.img_btn_evening_checked);

         holder.txtMorning = (TextView) convertView.findViewById(R.id.txtMorning);
         holder.txtAfternoon = (TextView) convertView.findViewById(R.id.txtafernoon);
         holder.txtEveing = (TextView) convertView.findViewById(R.id.txtEvening);


         holder.imgBtnMorningChecked.setVisibility(View.GONE);
         holder.imgBtnAfterNoonChecked.setVisibility(View.GONE);
         holder.imgBtnEveningChecked.setVisibility(View.GONE);


         convertView.setTag(holder);
         }

         else {
             holder = (ViewHolder) convertView.getTag();
             }
         if(listAvailibility.size()<=0)
         {
             holder.txtDate.setText("No Data");
             holder.txtDay.setText("No Data");

         }
         else{

             UserAvailibilityModel userAvailModel = listAvailibility.get(position);
             holder.txtDate.setText(userAvailModel.getDisaplyDate());
             holder.txtDay.setText(userAvailModel.getDisaplyDay());
             holder.imgBtnMorning.setImageResource(userAvailModel.getImageMorning());
             holder.imgBtnAfterNoon.setImageResource(userAvailModel.getImageAfterNoon());
             holder.imgBtnEvening.setImageResource(userAvailModel.getImageEvening());

             final ViewHolder fianlHolder = holder;
             final int finalposition = position;


             fianlHolder.imgBtnMorning.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View convertView) {
                        // if image source equals this drawable then...
                        // else change the image source to this drawable...
                        Toast.makeText( context,"Morning Btn Clicked"+finalposition,Toast.LENGTH_SHORT).show();  

                        //if(((ImageButton)convertView).getDrawable()==drawableMorning){
                            fianlHolder.imgBtnMorningChecked.setVisibility(View.VISIBLE);
                            fianlHolder.imgBtnMorningChecked.setImageResource(R.drawable.check);
                            getItem(finalposition).setMornigBtnClick(true);
                        //}
                    }
                });



             fianlHolder.imgBtnMorningChecked.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View convertView) {
                        // if image source equals this drawable then...
                        // else change the image source to this drawable...
                        Toast.makeText( context,"Morning Btn Checked Clicked"+finalposition,Toast.LENGTH_SHORT).show();  
                        //if(((ImageButton)convertView).getDrawable()==drawableCheck){
                            fianlHolder.imgBtnMorningChecked.setVisibility(View.GONE);
                            fianlHolder.imgBtnMorning.setVisibility(View.VISIBLE);
                            fianlHolder.imgBtnMorning.setImageResource(R.drawable.morning);
                            getItem(finalposition).setMornigBtnCheckClick(true);
                       // }

                    }
                });




             fianlHolder.imgBtnAfterNoon.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View convertView) {
                        // if image source equals this drawable then...
                        // else change the image source to this drawable...
                        Toast.makeText( context,"AfterNoon Image Clicked"+finalposition,Toast.LENGTH_SHORT).show();
                        //if(convertView.getId()==R.id.img_btn_afernoon){
                        fianlHolder.imgBtnAfterNoonChecked.setVisibility(View.VISIBLE);
                            //fianlHolder.imgBtnAfterNoon.setVisibility(View.GONE);
                        fianlHolder.imgBtnAfterNoonChecked.setImageResource(R.drawable.check);
                      //  }

                    }
                });

             fianlHolder.imgBtnAfterNoonChecked.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View convertView) {
                        // if image source equals this drawable then...
                        // else change the image source to this drawable...
                        Toast.makeText( context,"AfterNoon Btn Checked Clicked"+finalposition,Toast.LENGTH_SHORT).show();  
                        //if(convertView.getId()==R.id.img_btn_afernoon_checked){
                        fianlHolder.imgBtnAfterNoonChecked.setVisibility(View.GONE);
                        fianlHolder.imgBtnAfterNoon.setVisibility(View.VISIBLE);
                        fianlHolder.imgBtnAfterNoon.setImageResource(R.drawable.afternoon);

                       // }

                    }
                });

             fianlHolder.imgBtnEvening.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View convertView) {
                        // if image source equals this drawable then...
                        // else change the image source to this drawable...
                        Toast.makeText( context,"Evening Image Clicked"+finalposition,Toast.LENGTH_SHORT).show();
                        //if(convertView.getId()==R.id.img_btn_evening){
                        fianlHolder.imgBtnEveningChecked.setVisibility(View.VISIBLE);
                           // fianlHolder.imgBtnEvening.setVisibility(View.GONE);
                        fianlHolder.imgBtnEveningChecked.setImageResource(R.drawable.check);
                       // }

                    }
                });

             fianlHolder.imgBtnEveningChecked.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View convertView) {
                        // if image source equals this drawable then...
                        // else change the image source to this drawable...
                        Toast.makeText( context,"Eveing Btn Checked Clicked"+finalposition,Toast.LENGTH_SHORT).show();  
                       // if(convertView.getId()==R.id.img_btn_evening_checked){
                        fianlHolder.imgBtnEveningChecked.setVisibility(View.GONE);
                        fianlHolder.imgBtnEvening.setVisibility(View.VISIBLE);
                        fianlHolder.imgBtnEvening.setImageResource(R.drawable.night);
                       // }

                    }
                });



         }

         return convertView;
     }



     private static class ViewHolder {
         TextView txtDate;
         TextView txtDay;
         ImageButton imgBtnMorning;
         ImageButton imgBtnAfterNoon;
         ImageButton imgBtnEvening;
         ImageButton imgBtnMorningChecked;
         ImageButton imgBtnAfterNoonChecked;
         ImageButton imgBtnEveningChecked;
         TextView txtMorning;
         TextView txtAfternoon;
         TextView txtEveing;

         }



}

1 个答案:

答案 0 :(得分:0)

我不清楚你的问题或问题。但是看起来你在ListView上正确显示数据时遇到了一些特殊问题。在其中一个onClick():

中的getView()中尝试此示例代码
fianlHolder.imgBtnMorning.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View convertView) {
      ...
      ImageButton imgBtn = (ImageButton) convertView;
      imgBtn.setVisibility(View.VISIBLE);
      ...
      notifyDataSetChanged();
      ...

注意:

  • 使用查看参数更安全,因为这是焦点/点击的视图。而且我认为它看起来更简单和通用。
  • 将View视图转换为适当的类类型。否则可能会崩溃。
  • 也许是最重要的。通常,如果受影响的数据可见,则notifyDataSetChanged强制调用强制刷新ListView。