我有一个自定义列表视图,数据正在填充,它工作正常。在每个项目中,我都包含了一个类似按钮。通过单击数据将传递给服务器并返回响应,但它不会在我单击的位置更新,而是在其他位置更新。在从响应中更新文本之前,我验证位置是否正确。
private int positionOfButtonClick = -1;
viewholder.likelay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
positionOfButtonClick = position;
if(mp==null)
{
liked(ids,user.username,ds,position);
}
else
{
liked(ids,user.username,mp,position);
}
}
});
我正在使用的方法是
private void liked(final String ids, final String uname, final Activity a, final int position) {
String UPLOAD_URL = "http://xxxxxx.com/xxxxx.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
if (positionOfButtonClick == position) {
if (Integer.parseInt(s) == 0) {
viewholder.like.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like, 0, 0, 0);
viewholder.like.setText("like");
} else {
viewholder.like.setCompoundDrawablesWithIntrinsicBounds(R.drawable.likeblack, 0, 0, 0);
viewholder.like.setText(s);
}
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(a, volleyError.getMessage(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new Hashtable<String, String>();
params.put("id",ids);
params.put("username",uname);
return params;
}
};
我在这做什么错?
修改
根据评论解决问题。
StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
if (positionOfButtonClick == p) {
dusers.get(p).setLikecount(Integer.parseInt(s));
if(dusers.get(p).getLike() == 1)
{
dusers.get(p).setLike(0);
}
else
{
dusers.get(p).setLike(1);
}
notifyDataSetChanged();
}
}
}