/**
   * 添加路线
   *
   * @param route 路线
   * @param listener 回调
   */
  public void addRoute(final Route route, final OnRouteListener listener) {
    BmobQuery<Route> query1 = new BmobQuery<>();
    query1.addWhereEqualTo("name", route.getName());
    BmobQuery<Route> query2 = new BmobQuery<>();
    query2.addWhereEqualTo("creator", UserManager.getInstance(mContext).getUser());
    List<BmobQuery<Route>> andQuerys = new ArrayList<>();
    andQuerys.add(query1);
    andQuerys.add(query2);

    BmobQuery<Route> mainQuery = new BmobQuery<>();
    mainQuery.and(andQuerys);
    mainQuery.findObjects(
        mContext,
        new FindListener<Route>() {
          @Override
          public void onSuccess(List<Route> list) {
            if (list.size() > 0) {
              listener.onError(null);
            } else {
              saveRoute(route, listener);
            }
          }

          @Override
          public void onError(int i, String s) {
            listener.onError(null);
          }
        });
  }
  // 查询符合时间和用户id条件的记录
  public void getData(
      int year_c, int month_c, int jumpYear, int jumpMonth, final Handler handler, String id) {

    String dateString =
        String.valueOf(year_c + jumpYear)
            .concat("-")
            .concat(String.valueOf(month_c + jumpMonth))
            .concat("-")
            .concat("31");
    String dateString2 =
        String.valueOf(year_c + jumpYear)
            .concat("-")
            .concat(String.valueOf(month_c + jumpMonth))
            .concat("-")
            .concat("01");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date1 = null;
    Date date2 = null;
    try {
      date1 = sdf.parse(dateString);
      date2 = sdf.parse(dateString2);
    } catch (ParseException e) {
    }

    BmobQuery<Sign> my_sign = new BmobQuery<Sign>();
    my_sign.addWhereLessThanOrEqualTo("createdAt", new BmobDate(date1));

    BmobQuery<Sign> my_sign2 = new BmobQuery<Sign>();
    my_sign2.addWhereGreaterThanOrEqualTo("createdAt", new BmobDate(date2));

    BmobQuery<Sign> my_sign3 = new BmobQuery<Sign>();
    my_sign3.addWhereEqualTo("user_id", id);

    List<BmobQuery<Sign>> andQuerys = new ArrayList<BmobQuery<Sign>>();
    andQuerys.add(my_sign);
    andQuerys.add(my_sign2);
    andQuerys.add(my_sign3);

    BmobQuery<Sign> sign = new BmobQuery<Sign>();
    sign.and(andQuerys);
    sign.findObjects(
        this.mContext,
        new FindListener<Sign>() {
          @Override
          public void onSuccess(List<Sign> list) {

            Message message = handler.obtainMessage();
            message.what = 1;
            message.obj = list;
            handler.sendMessage(message);
          }

          @Override
          public void onError(int i, String s) {}
        });
  }
  @Override
  public View bindView(int arg0, View convertView, ViewGroup arg2) {
    // TODO Auto-generated method stub
    if (convertView == null) {
      convertView = mInflater.inflate(R.layout.item_answer_in_list_elinc, null);
    }
    final Answer contract = getList().get(arg0);
    final Integer[] like_number = new Integer[1];
    final TextView answer_number = ViewHolder.get(convertView, R.id.answer_number);

    TextView answer_content = ViewHolder.get(convertView, R.id.answer_content);
    TextView answer_date = ViewHolder.get(convertView, R.id.answer_date);
    final TextView answer_responder = ViewHolder.get(convertView, R.id.answer_responder);
    CircleImageView avatar_for_responder = ViewHolder.get(convertView, R.id.avatar_for_responder);
    ImageView avatar_for_answer_iv = ViewHolder.get(convertView, R.id.avatar_for_answer_iv);
    answer_content.setText(contract.getAnswerContent());
    answer_responder.setText(contract.getResponder().getUsername());
    answer_date.setText(Tool.showdate(contract.getCreatedAt()));
    String avatar = contract.getResponder().getAvatar();
    final ImageView like_answer;
    like_answer = ViewHolder.get(convertView, R.id.like_answer);
    refreshNumber(contract, answer_number, R.drawable.no_like, like_answer);

    // 初始化点赞按钮
    BmobQuery<User> query1 = new BmobQuery<User>();
    BmobQuery<User> query2 = new BmobQuery<User>();
    Answer ans = new Answer();
    ans.setObjectId(contract.getObjectId());
    User u = BmobUser.getCurrentUser(context, User.class);
    query1.addWhereEqualTo("objectId", u.getObjectId());
    query2.addWhereRelatedTo("likedBy", new BmobPointer(ans));
    List<BmobQuery<User>> queries = new ArrayList<BmobQuery<User>>();
    queries.add(query1);
    queries.add(query2);
    BmobQuery<User> mainQuery = new BmobQuery<User>();
    mainQuery.and(queries);
    mainQuery.count(
        context,
        User.class,
        new CountListener() {
          @Override
          public void onSuccess(int count) {
            if (count > 0) {

              refreshNumber(contract, answer_number, R.drawable.like, like_answer);
              like_answer.setBackgroundResource(R.drawable.like);
              Log.i("life", "多对多关联添加成功");
            } else {
              /*还没有点赞*/
              refreshNumber(contract, answer_number, R.drawable.no_like, like_answer);
              // ShowToast("点赞成功");
              like_answer.setBackgroundResource(R.drawable.no_like);
            }
          }

          @Override
          public void onFailure(int code, String msg) {
            // TODO Auto-generated method stub
            // ShowToast("count failure:"+msg);
            ShowToast("亲,断网了呢");
          }
        });

    like_answer.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            BmobQuery<User> query1 = new BmobQuery<User>();
            BmobQuery<User> query2 = new BmobQuery<User>();
            Answer ans = new Answer();
            ans.setObjectId(contract.getObjectId());
            User u = BmobUser.getCurrentUser(context, User.class);
            query1.addWhereEqualTo("objectId", u.getObjectId());
            query2.addWhereRelatedTo("likedBy", new BmobPointer(ans));
            List<BmobQuery<User>> queries = new ArrayList<BmobQuery<User>>();
            queries.add(query1);
            queries.add(query2);
            BmobQuery<User> mainQuery = new BmobQuery<User>();
            mainQuery.and(queries);
            mainQuery.count(
                context,
                User.class,
                new CountListener() {
                  @Override
                  public void onSuccess(int count) {
                    if (count > 0) {
                      /*如果已经点赞了*/
                      User user = BmobUser.getCurrentUser(context, User.class);
                      Answer ans = new Answer();
                      ans.setObjectId(contract.getObjectId());
                      BmobRelation relation = new BmobRelation();
                      relation.remove(user);
                      ans.setLikedBy(relation);
                      ans.update(
                          context,
                          new UpdateListener() {
                            @Override
                            public void onSuccess() {
                              // TODO Auto-generated method stub
                              refreshNumber(
                                  contract, answer_number, R.drawable.no_like, like_answer);
                              Log.i("life", "多对多关联添加成功");
                            }

                            @Override
                            public void onFailure(int arg0, String arg1) {
                              // TODO Auto-generated method stub
                              ShowToast("亲,怎么没网了呢");
                              Log.i("life", "多对多关联添加失败");
                            }
                          });
                    } else {
                      /*还没有点赞*/
                      User user = BmobUser.getCurrentUser(context, User.class);
                      Answer ans = new Answer();
                      ans.setObjectId(contract.getObjectId());
                      BmobRelation relation = new BmobRelation();
                      relation.add(user);
                      ans.setLikedBy(relation);
                      ans.update(
                          context,
                          new UpdateListener() {
                            @Override
                            public void onSuccess() {
                              // TODO Auto-generated method stub
                              refreshNumber(contract, answer_number, R.drawable.like, like_answer);
                              // ShowToast("点赞成功");
                              Log.i("life", "多对多关联添加成功");
                            }

                            @Override
                            public void onFailure(int arg0, String arg1) {
                              // TODO Auto-generated method stub
                              ShowToast("亲,怎么没网了呢");
                              Log.i("life", "多对多关联添加失败");
                            }
                          });
                    }
                  }

                  @Override
                  public void onFailure(int code, String msg) {
                    // TODO Auto-generated method stub
                    // ShowToast("count failure:"+msg);
                    ShowToast("亲,断网了呢");
                  }
                });
          }
        });

    // =======================================================================
    if (avatar != null && !avatar.equals("")) { // 加载头像-为了不每次都加载头像
      ImageLoader.getInstance()
          .displayImage(
              avatar, avatar_for_responder, ImageLoadOptions.getOptions(), animateFirstListener);
    } else {
      avatar_for_responder.setImageResource(R.drawable.head);
    }

    final String answeravatar = contract.getAnswerAvatar();
    if (answeravatar != null && !answeravatar.equals("")) { // 加载头像-为了不每次都加载头像
      ImageLoader.getInstance()
          .displayImage(
              answeravatar,
              avatar_for_answer_iv,
              ImageLoadOptions.getOptions(),
              animateFirstListener);
      avatar_for_answer_iv.setVisibility(View.VISIBLE);
    }
    avatar_for_answer_iv.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(context, ImageBrowserActivity.class);
            ArrayList<String> photos = new ArrayList<String>();
            photos.add(answeravatar);
            intent.putStringArrayListExtra("photos", photos);
            intent.putExtra("position", 0);
            context.startActivity(intent);
          }
        });
    answer_responder.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            User u = BmobUser.getCurrentUser(context, User.class);
            bundle.putString("username", answer_responder.getText().toString());
            if (answer_responder.getText().toString().equals(u.getUsername().toString())) {
              bundle.putString("from", "me");
            } else {
              bundle.putString("from", "add");
            }
            intent.putExtras(bundle);
            intent.setClass(context, SetMyInfoActivity.class);
            context.startActivity(intent);
          }
        });

    avatar_for_responder.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            User u = BmobUser.getCurrentUser(context, User.class);
            bundle.putString("username", answer_responder.getText().toString());
            if (answer_responder.getText().toString().equals(u.getUsername().toString())) {
              bundle.putString("from", "me");
            } else {
              bundle.putString("from", "add");
            }
            intent.putExtras(bundle);
            intent.setClass(context, SetMyInfoActivity.class);
            context.startActivity(intent);
          }
        });
    return convertView;
  }