我有一个带有ratingbars的列表视图。最初评级为零。当我对任何一个评级栏给出评级时,所选评级会上传到服务器,并且在成功上传后,我会调用notifyDataSetChanged()
以便listview
更新最新值。但它没有设置上传到服务器的值的评级栏,而是像以前一样显示零评级。当我转到其他页面并返回时,现在评级已正确设置。我在getView()
中做错了吗?
public class OrderAdapter extends ArrayAdapter<OrderModel> {
private Context mContext;
int resource;
private ArrayList<OrderModel> mListData = new ArrayList<OrderModel>();
private String userid, prdid;
SharedPreferences sharedpreferences;
public OrderAdapter(Context mContext, int resource, ArrayList<OrderModel> mListData) {
super(mContext, resource, mListData);
this.resource = resource;
this.mContext = mContext;
this.mListData = mListData;
sharedpreferences = mContext.getSharedPreferences("MyPREFERENCES", Context.MODE_PRIVATE);
}
@Override
public int getCount() {
return super.getCount();
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View v = convertView;
final ViewHolder holder;
if (v == null) {
holder = new ViewHolder();
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
v = inflater.inflate(resource, parent, false);
holder.rate = (RatingBar)v.findViewById(rate);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.rate.setRating(Float.parseFloat(item.getRate()));
if(holder.rate.getRating()>0) {
holder.rate.setIsIndicator(true);
}else{
holder.rate.setIsIndicator(false);
}
holder.rate.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
uploadrating(String.valueOf(rating), position, ratingBar);
}
});
return v;
}
class ViewHolder {
ImageView image;
TextView titletxt, ordereddttxt, delvrdttxt, ordernotxt, statustxt;
RatingBar rate;
}
private void uploadrating(final String rating, final int position, final RatingBar rate){
userid=sharedpreferences.getString("userid","");
prdid = mListData.get(position).getPrdid();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URLUtil.UPLOADRATING,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
if(obj.getInt("Ack")==1){
Toast.makeText(mContext, obj.getString("msg"), Toast.LENGTH_SHORT).show();
rate.setIsIndicator(true);
notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("user_id", userid);
params.put("product_id", prdid);
params.put("score", rating);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(mContext);
requestQueue.add(stringRequest);
}
}
答案 0 :(得分:0)
您需要在某个时刻调用item.setRate(),否则该项目不会保存新的评级。