我创建了一个voteUp方法,如下所示,
public class Voting {
public void onUpVote(View view) {
CharSequence VoteUpId = ((TextView) ((RelativeLayout) view.getParent()).getChildAt(1)).getText();
final RequestQueue mrequestQueue = VolleySingleton.getInstance().getRequestQueue();
final String PUT_VOTE_UP = "URL + VoteUpId + URL";
StringRequest PostVoteUp = new StringRequest(Request.Method.PUT, PUT_VOTE_UP, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response + "reponse");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
System.out.println("************Answer" + error + "error");
}
});
mrequestQueue.add(PostVoteUp);
}
这个方法接受一个视图对象,我还有另一个方法,如下图所示,在另一个类中我设置了一个onClickListner
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
VoteClass(What Do I pass In here??);
}
});
我想传递一个与同一布局中的按钮视图不同的视图的子项,并且我不想使用从xml布局到类的直接onClick。那我该怎么做呢?
答案 0 :(得分:1)
实现View.OnClickListener类并将视图作为参数传递,如此
class CustomClickListener implements View.OnClickListener{
View view;
public CustomClickListener(View view){
this.view = view;
}
@Override
public void onClick(View v) {
VoteClass(view);
}
}
然后
mButton.setOnClickListener(new CustomClickListener(the view you want to pass));