我正在尝试通过 PHP 将 MySQL 数据库中的数据绑定到 ListView 中的 Android RatingBar。使用 SQLite,我可以只使用 SimpleCursorAdapter,但由于 PHP 返回 JSONArray,我将数据加载到 HashMap 中,无法弄清楚如何将该数据绑定到 RatingBar。我已经尝试过这段代码:
class MyBinder implements ViewBinder{
@Override
public boolean setViewValue(View view, String data, String textRepresentation) {
if(view.getId() == R.id.ratingBar){
String stringval = data;
float ratingValue = Float.parseFloat(stringval);
RatingBar ratingBar = (RatingBar) view;
ratingBar.setRating(ratingValue);
return true;
}
return false;
}
}
ListAdapter simpleadapter = new SimpleAdapter(getApplicationContext(), values, R.layout.review_info, columns, toView);
simpleadapter.setViewBinder(new MyBinder());
listview.setAdapter(simpleadapter);
不幸的是,我收到一个编译时错误,说 setViewBinder 不是 ListAdapter 中的方法,并建议我将其转换为 SimpleCursorAdapter。那么如何在没有游标的情况下绑定数据呢?或者如何从 MySQL 获取游标?谢谢
最终工作代码:
public class MyArrayAdapter extends ArrayAdapter<Locations>{
Context context;
ArrayList<Locations> data;
int resource;
public MyArrayAdapter(Context context, int resource, ArrayList<Locations> data
) {
super(context, resource, data);
this.context = context;
this.data = data;
this.resource = resource;
}
static class ViewHolder {
public TextView text;
public RatingBar ratingbar;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder viewHolder;
Locations l = data.get(position);
if (rowView == null)
{
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//get review_info which is the info inside list_review
rowView = inflater.inflate(R.layout.review_info, null);
viewHolder.text = (TextView) rowView.findViewById(R.id.displayReview);
viewHolder. ratingbar = (RatingBar) rowView.findViewById(R.id.ratingReview);
rowView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) rowView.getTag();
}
viewHolder.text.setText(l.getReview());
viewHolder.ratingbar.setRating(l.getClean());
return rowView;
}
}