2016-10-22 50 views
-2

这是我的视图联编程序代码。这里的acceptbutton和reject按钮不起作用。在butterknife中ImageView的onclicklistener不起作用

@Layout(R.layout.item) 
public class TinderCard implements AsyncTaskCompleteListener { 

SessionManager msession; 
private static final String LIKE = "1"; 
private static final String DISLIKE = "2"; 
private int size,match; 
String suggestionid; 
private FindMatchesExecute execute; 
@View(R.id.profileImageView) 
private ImageView profileImageView; 

@View(R.id.nameAgeTxt) 
private TextView nameAgeTxt; 

@View(R.id.acceptBtn) 
private ImageView acceptbutton; 


@View(R.id.rejectBtn) 
private ImageView dislike_button; 

private Data mProfile; 
private Context mContext; 
public int count=0; 



@View(R.id.swipeView) 
private SwipePlaceHolderView mSwipeView; 
Activity activity; 
public TinderCard(Context context, Data profile, SwipePlaceHolderView swipeView,FindMatchesExecute execu) { 
    mContext = context; 
    mProfile = profile; 
    mSwipeView = swipeView; 
    this.execute=execu; 
    msession=new SessionManager(mContext); 


} 

@Resolve 
private void onResolved(){ 
    Glide.with(mContext).load(mProfile.getPicture()).into(profileImageView); 
    nameAgeTxt.setText(mProfile.getName() + ", " + mProfile.getAge()); 

    suggestionid=mProfile.getUserid(); 
    profileImageView.setOnClickListener(new android.view.View.OnClickListener() { 
     @Override 
     public void onClick(android.view.View v) { 
      Intent i=new Intent(mContext,UserProfile.class); 
      i.putExtra("suggestion_id",mProfile.getUserid()); 
      mContext.startActivity(i); 
     } 
    }); 

接受和拒绝按钮的监听器部分不工作。我在代码中缺少什么。

acceptbutton.setOnClickListener(new android.view.View.OnClickListener() { 
     @Override 
     public void onClick(android.view.View v) { 
      mSwipeView.doSwipe(true); 
     } 
    }); 
    dislike_button.setOnClickListener(new android.view.View.OnClickListener() { 
     @Override 
     public void onClick(android.view.View v) { 
      mSwipeView.doSwipe(false); 

     } 
    }); 
} 


@SwipeOut 
private void onSwipedOut(){ 

    Log.d("shan", "onSwipedOut"); 
    HashMap<String, String> map = new HashMap<>(); 
    map.put(Const.URL, Constant.like_dislike); 
    map.put(Const.Params.TOKEN, msession.getToken()); 
    map.put(Const.Params.ID, msession.getID()); 

    map.put(Const.Params.LIKE_DISLIKE_STATUS, DISLIKE); 


    map.put(Const.Params.SUGGESTION_ID, suggestionid); 

    Log.d("shanUpdatePref", "login map" + map); 

    // new MultiPartRequester(this, map, Const.ServiceCode.UPDATE_DOB, this); 
    new HttpRequester(mContext, Const.POST, map, Const.ServiceCode.LIKE_DISLIKE, this); 
    execute.onExecute(); 
} 

@SwipeCancelState 
protected void onSwipeCancelState(){ 
    Log.d("EVENT", "onSwipeCancelState"); 
} 

@SwipeIn 
private void onSwipeIn() 
{ 
    //FindMatchesFragment.size--; 
    HashMap<String, String> map = new HashMap<>(); 
    map.put(Const.URL, Constant.like_dislike); 
    map.put(Const.Params.TOKEN, msession.getToken()); 
    map.put(Const.Params.ID, msession.getID()); 

     map.put(Const.Params.LIKE_DISLIKE_STATUS, LIKE); 


    map.put(Const.Params.SUGGESTION_ID, suggestionid); 

    Log.d("shanUpdatePref", "login map" + map); 


    new HttpRequester(mContext, Const.POST, map, Const.ServiceCode.LIKE_DISLIKE, this); 
    execute.onExecute(); 
} 

@SwipeInState 
private void onSwipeInState(){ 
    Log.d("EVENT", "onSwipeInState"); 
} 

@SwipeOutState 
private void onSwipeOutState(){ 
    Log.d("EVENT", "onSwipeOutState"); 
} 

@Override 
public void onTaskCompleted(String response, int serviceCode) { 
    JSONObject jsonObject; 
    switch (serviceCode) { 
     case Const.ServiceCode.LIKE_DISLIKE: 
      Log.d("shan", "task1"); 
      Log.d("shanResponse",response); 
      try { 
       jsonObject = new JSONObject(response); 


       if (jsonObject.getString("success").equals("true")) { 

        JSONObject like= jsonObject.getJSONObject("like"); 
        Log.d("shanStatus",like.optString("status")); 

        match=like.optInt("match"); 
        if(match==1) 
        { 
         String picture=like.optString("picture"); 
         String user_pictre=like.optString("user_image"); 
         String name=like.optString("name"); 
         String userid=like.optString("suggestion_id"); 
         Fragment MatchFoundScreen = new MatchFoundScreen(); 
         Bundle args = new Bundle(); 
         args.putString("name", name); 
         args.putString("picture",picture); 
         args.putString("user_image", user_pictre); 
         args.putString("suggestion_id", userid); 
         args.putInt("match",match); 
         MatchFoundScreen.setArguments(args); 
         FragmentManager fragmentManager =((FragmentActivity) mContext).getSupportFragmentManager(); 

         fragmentManager.beginTransaction().replace(R.id.frame, MatchFoundScreen).commit(); 

        } 



       } else { 
        Utility.showShortToast(mContext, jsonObject.getString("error_message")); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      break; 
    } 
} 

}

有人请指导我解决这个问题。提前致谢。

这是我的片段类。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    msession = new SessionManager(getActivity()); 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 


    Log.i("Find Match", "oncreateview called"); 
    view = inflater.inflate(R.layout.find_matches, null); 
    rlayout=(LinearLayout) view.findViewById(R.id.likedislikebuttonlayout); 
    frame=(FrameLayout)view.findViewById(R.id.frame2); 
    LocationManager locationManagerresume = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE); 
    if (!locationManagerresume 
      .isProviderEnabled(LocationManager.GPS_PROVIDER)) { 

     showGPSDisabledAlertToUser(); 
    } 

    cd = new ConnectionDetector(getActivity()); 
    if (!cd.isConnectingToInternet()) { 

     Toast.makeText(getActivity(), "No Internet", Toast.LENGTH_SHORT) 
       .show(); 

    } 

    mSwipeView = (SwipePlaceHolderView)view.findViewById(R.id.swipeView); 
    rippleBackground = (RippleBackground) view.findViewById(R.id.content); 
    rippleBackground.startRippleAnimation(); 

    noUsers=(TextView)view.findViewById(R.id.noUsers); 
    profileimage = (CircleImageView) view.findViewById(R.id.profileimage); 
    Glide.with(getActivity()).load(msession.getProfileImage()) 
    .into(profileimage); 
    mGoogleApiClient = new GoogleApiClient.Builder(getActivity()) 
       .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 


    mLocationRequest = LocationRequest.create() 
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
      .setInterval(10 * 1000) 
      .setFastestInterval(1 * 1000); 
    al = new ArrayList<>(); 
    mContext = getActivity(); 


    mSwipeView.getBuilder() 
      .setDisplayViewCount(1) 
      .setSwipeDecor(new SwipeDecor() 
        .setPaddingTop(20) 
        .setRelativeScale(0.01f) 
        .setSwipeInMsgLayoutId(R.layout.tinder_swipe_in_msg_view) 
        .setSwipeOutMsgLayoutId(R.layout.tinder_swipe_out_msg_view)); 




    return view; 
} 
+0

你加'ButterKnife.bind(本)的方式;''来onCreate' ? –

+0

我也添加了我的片段类。你能不能更具体些?这行ButterKnife.bind(this); ,我无法将其添加到片段的onCreate方法 – Shan

+0

你好,如果你使用片段。在'inflater'视图之后的'onCreateView',你可以使用'ButterKnife.bind(this,view);'你可以检查[here](http://jakewharton.github.io/butterknife/)。 –

回答

0

使用ButterKnife.bind(getActivity())当过你在活动中使用this和片段使用getActivity()

这是在不同的地方for more details使用上下文的

0
@OnClick(R.id.iv_image) 
void ivBack() { 
    //TODO For Back Action 
    finish(); 
} 

使用最新

compile 'com.jakewharton:butterknife:8.1.0' 
apt 'com.jakewharton:butterknife-compiler:8.1.0' 

more details about lib

+0

请不要链接到Google结果。如果您要链接到任何内容,请输入实际的网址 –