使用来自Parse数据的ListView的片段中的RatingBar

时间:2016-01-17 18:24:37

标签: java android listview android-fragments parse-platform

基本上我在Parse数据库中有两列“rank”和“rankCount”,它们都是数字。

Rank表示该行中项目的总排名,rankCount表示排名该项目的人数。

parse

我要做的是创建一个方法,将这两者之间的平均值相加,使得数字中的Int(如果是Double / Float)并在RatingBar之间显示相应的数字1-5颗星,位于指定片段内,其中包含ListView。

注意:我不想在解析数据库中进行更改,因为我正在使用它来处理这个应用程序的iphone版本已经完成了,但是我在使用android时遇到了更多困难。

特定片段类:

public class RecommendedTab extends Fragment {
ListView recommendedListView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View tab_recommended = inflater.inflate(R.layout.tab_recommended, container, false);
    recommendedListView = (ListView)tab_recommended.findViewById(R.id.recommendedList);

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Place");
    new RemoteDataTask(){
        protected void onPostExecute(List<Places> places) {
            recommendedListView.setAdapter(new PlacesAdapter(getActivity(), places, null));
        }
    }.execute(query);
    return tab_recommended;
   }
}

适配器类:

public class PlacesAdapter extends BaseAdapter{

private List<Places> places=null;
private List<Places> filteredPlaces=null;
LayoutInflater inflater;
ImageLoader imageLoader;
private Context context;
private Location loc;

public PlacesAdapter(Context context, List<Places> places, Location loc){
    this.context = context;
    this.places = places;
    inflater = LayoutInflater.from(context);
    imageLoader = new ImageLoader(context);
    resetPlaces();
}

public void resetPlaces(){
    filteredPlaces = places;
}

public void filter(String s){
    //validation
    filteredPlaces = new ArrayList<Places>();//
    for(int i=0;i<places.size();i++){
       if(places.get(i).getName().toLowerCase().contains(s.toLowerCase())){
           filteredPlaces.add(places.get(i));
       }
    }
}

public class ViewHolder {
    RatingBar ratingBar;
    TextView name;
    TextView type;
    TextView adress;
    TextView phone;
    TextView hours;
    TextView details;
    ImageView image;
}

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

@Override
public long getItemId(int position) {
    return position;
}

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

@Override
public View getView(final int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.row_layout, null);

        holder.name = (TextView)view.findViewById(R.id.placeName);
        holder.type = (TextView) view.findViewById(R.id.placeType);
        holder.image = (ImageView) view.findViewById(R.id.placeImage);
        holder.ratingBar = (RatingBar)view.findViewById(R.id.placeRate);

        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    holder.ratingBar.setOnRatingBarChangeListener(onRatingChangedListener(holder, position));
    holder.ratingBar.setTag(position);
    holder.ratingBar.setRating(places.get(position).getRatingStar());
    holder.name.setText(places.get(position).getName());
    holder.type.setText(places.get(position).getType());

    imageLoader.DisplayImage(places.get(position).getImage(),
            holder.image);

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(context, PlaceDetails.class);
            intent.putExtra("name",  (places.get(position).getName()));
            intent.putExtra("phone",  (places.get(position).getPhone()));
            intent.putExtra("hours", (places.get(position).getHours()));
            intent.putExtra("rank", (places.get(position).getRatingStar()));
            intent.putExtra("details",  (places.get(position).getDetails()));
            intent.putExtra("image", (places.get(position).getImage()));
            context.startActivity(intent);
        }
    });
    return view;
}

private RatingBar.OnRatingBarChangeListener onRatingChangedListener(final ViewHolder holder, final int position) {
    return new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
            places.get(position).setRatingStar(v);
        }
    };
  }
}

查询类:

public class RemoteDataTask extends AsyncTask<ParseQuery<ParseObject>, Void, List<Places>> {
@Override
protected List<Places> doInBackground(ParseQuery<ParseObject>... query) {
    List<Places> places = new ArrayList<Places>();
    try {
        List<ParseObject> ob = query[0].find();
        for (ParseObject place : ob) {
            ParseFile image = (ParseFile) place.get("image");
            Places p = new Places();
            p.setName((String) place.get("name"));
            p.setType((String) place.get("type"));
            p.setHours((String) place.get("hours"));
            p.setPhone((String)place.get("phone"));
            p.setDetails((String) place.get("details"));
            p.setImage(image.getUrl());
            places.add(p);
        }
    } catch (ParseException e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    return places;
    }
}

我的项目中还有很多其他类,但我很确定它们与我的问题无关。

PS:对于Swift的“NSUserDefaults”,android的equevilant是什么? 我需要检查一个项目是否已被评级并禁用RatingBar。

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到,例如你可以:

  1. 将实例变量 int rank 添加到类 Place ,并带有所需的setter&amp;吸气剂。
  2. 使用Math.round设置为每个地方排名舍入等级/ rankCount值,您可以使用相关的ParseObject.getDouble
  3. 然后在为Place创建视图时,只需使用它预定义的 rank 变量作为所需星星的计数器。
  4. 注意: 最好使用:ParseObject.getString

    而不是(String) place.get("name")获取一个Object,然后像你一样转换为String。它在Parse文档中也提到过:

      

    在大多数情况下,使用辅助函数(如ParseObject.getString(String)或ParseObject.getInt(String))会更方便。

    如果您需要进一步的帮助,请发表评论