Android将数据绑定到ListView而不使用游标

时间:2013-06-30 20:21:19

标签: android sql listview simpleadapter

我正在尝试通过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;
    }
 } 

1 个答案:

答案 0 :(得分:0)

在构建行视图的同时,在适配器的getView()方法中绑定数据: