gpt4 book ai didi

android - ListView 中的评级栏评级重置

转载 作者:行者123 更新时间:2023-11-30 01:28:52 25 4
gpt4 key购买 nike

Senario:获取多个产品的评级反馈。

问题:通过调用 setOnRatingBarChangeListener

在滚动 ListView 上重置 Ratingbar 值

预期输出:

enter image description here

自定义 ListView 适配器代码:

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

}

最佳答案

只需用一个简单的 if 将您的代码包装在您的 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));
}

已使用模拟器对其进行测试,它按预期工作。希望这能解决您的问题

关于android - ListView 中的评级栏评级重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36037402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com