2016-03-16 4 views
1

Senario: Чтобы получить отзыв о рейтинге для нескольких нескольких продуктов.Оценка рейтинга рейтинга в списках списков

Проблема: Ratingbar значение сбрасывается на прокрутке ListView по телефону setOnRatingBarChangeListener

Ожидаемый результат:

enter image description here

Заказной код ListView adpater:

public class FeedbackAdapter extends BaseAdapter { 

    static Context context; 
    List<FeedbackRowDetails> row; 
    ViewHolder holder; 
    public FeedbackAdapter(Context context,List<FeedbackRowDetails> row) 
    { 
     this.context = context; 
     this.row =row; 
    } 

    private class ViewHolder { 
     TextView tv_slno, tv_productname, tv_time; 
     RatingBar rb_productrating; 

    } 

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

     LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 

     if (convertView == null) 
     { 

      convertView = mInflater.inflate(R.layout.custom_feedback_row_format,null); 
      holder = new ViewHolder(); 
      holder.tv_slno = (TextView) convertView.findViewById(R.id.tv_slno); 
      holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time); 
      holder.tv_productname = (TextView) convertView.findViewById(R.id.tv_productname); 
      holder.rb_productrating=(RatingBar) convertView.findViewById(R.id.rb_productrating); 
      convertView.setTag(holder); 

     } else 
     { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     FeedbackRowDetails row_pos = row.get(position); 

     holder.tv_slno.setText(String.valueOf(position + 1)); 
     holder.tv_time.setText(row_pos.gettime()); 
     holder.tv_productname.setText(row_pos.getproductsName()); 
     holder.rb_productrating.setRating(Float.valueOf(row_pos.getrating())); 

     holder.rb_productrating.setOnRatingBarChangeListener(new OnRatingBarChangeListener() 
     { 
      public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) 
      { 
       FeedbackRowDetails row_pos = row.get(position); 
       int roundoff_rating = (int)Math.round(rating); 
       ratingBar.setRating(roundoff_rating); 
       row_pos.setrating(String.valueOf(roundoff_rating)); 

      } 
     }); 

     return convertView; 

    } 

    @Override 
    public int getCount() { 
     return row.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return row.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return row.indexOf(getItem(position)); 
    } 

} 
+0

при выполнении такого рода работ в ListView, как установка значения, например. checkbox, textviews и другие поля, вы также должны установить их для else case. Таким образом, они работают нормально, иначе будет запутаться при прокрутке –

+0

, почему вы вызываете внутренний адаптер setOnRatingBarChangeListener? –

+0

, чтобы получить рейтинг конкретного продукта, я использую его внутри адаптера. Правильно ли это? или каким-либо другим способом реализовать это? Я реализовал флажок в том же формате, в котором он работает. @ Mohit suthar –

ответ

0

Эта проблема решается со ссылкой на link

public class FeedbackAdapter extends BaseAdapter { 

     static Context context; 
     List<FeedbackRowDetails> row; 
     ViewHolder holder; 
     public FeedbackAdapter(Context context,List<FeedbackRowDetails> row) 
     { 
      this.context = context; 
      this.row =row; 
     } 

     private class ViewHolder { 
      TextView tv_slno, tv_productname, tv_time; 
      RatingBar rb_productrating; 

     } 

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

      LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 

      if (convertView == null) 
      { 

       convertView = mInflater.inflate(R.layout.custom_feedback_row_format,parent, false); 
       holder = new ViewHolder(); 
       holder.tv_slno = (TextView) convertView.findViewById(R.id.tv_slno); 
       holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time); 
       holder.tv_productname = (TextView) convertView.findViewById(R.id.tv_productname); 
       holder.rb_productrating=(RatingBar) convertView.findViewById(R.id.rb_productrating); 
       convertView.setTag(holder); 

      } else 
      { 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      holder.rb_productrating.setOnRatingBarChangeListener(onRatingChangedListener(holder, position)); 
      holder.rb_productrating.setTag(position); 

      holder.tv_slno.setText(String.valueOf(position + 1)); 
      holder.tv_time.setText(getItem(position).gettime()); 

      holder.tv_productname.setText(getItem(position).getproductsName()); 
      float rating=Float.valueOf(getItem(position).getrating()); 

      holder.rb_productrating.setRating(rating); 

      return convertView; 

     } 

      private RatingBar.OnRatingBarChangeListener onRatingChangedListener(final ViewHolder holder, final int position) 
      { 
       return new RatingBar.OnRatingBarChangeListener() { 
        @Override 
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean b) 
        { 

         FeedbackRowDetails item = row.get(position); 
         int roundoff_rating = (int)Math.round(rating);     
         ratingBar.setRating(roundoff_rating); 
         item.setrating(String.valueOf(roundoff_rating)); 

        } 
       }; 
      } 
     @Override 
     public int getCount() { 
      return row.size(); 
     } 

     @Override 
     public FeedbackRowDetails getItem(int position) 
     { 
      return row.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return row.indexOf(getItem(position)); 
     } 

    } 
1

Просто обернуть коды, присутствующие внутри onRatingChanged метода с простым, если, как показано ниже

if(fromUser) { 
       FeedbackRowDetails row_pos = row.get(position); 
       int roundoff_rating = (int) Math.round(rating); 
       ratingBar.setRating(roundoff_rating); 
       row_pos.setRating(String.valueOf(roundoff_rating)); 
      } 

Проверили его с эмулятором и она работает, как ожидалось. Надеюсь, что это решит вашу проблему

Смежные вопросы