我试图在onClick上更改适配器中的图像。我需要根据Json的响应更改特定行的图标。你能帮我解决这个问题吗?这是我的参考代码
holder.favourites_imageView
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cpsId = favouritesLists.get(position).getFavId();
new MarkUnFavourite_WS().execute();
}
});
private class MarkUnFavourite_WS extends AsyncTask<Void, Void, String> {
ViewHolder holder = null;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
holder = new ViewHolder();
ArrayList<NameValuePair> postData;
postData = new ArrayList<NameValuePair>();
JSONObject parentData = new JSONObject();
JSONObject childData = new JSONObject();
try {
childData.put("userId",
sharedpreferences.getInt("userId_sp", 0));
childData.put("favId", cpsId);
childData.put("favType", "O");
childData.put("markFavouriteStatus", "U");
// System.out.println(childData);
parentData.put("User", childData);
System.out.println(parentData);
} catch (JSONException e) {
e.printStackTrace();
}
// postData.add(new BasicNameValuePair("User",
// childData.toString()));
InputStream is = null;
String jsonResponse = "";
JSONObject jObj = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(Httppost_Links.MARK_FAVOURITE);
request.setHeader("Content-Type", "application/json");
request.setEntity(new StringEntity(parentData.toString()));
HttpResponse httpResponse = httpClient.execute(request);
System.out.println(httpResponse);
// HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
System.out.println(httpEntity);
is = httpEntity.getContent();
System.out.println(is);
System.out.println(is.toString());
try {
Log.e("@@@@@@@@@@", "@@@@@@@@@@@");
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
jsonResponse = sb.toString();
Log.e("JSON", "Check Json: " + jsonResponse);
} catch (Exception e) {
Log.e("Buffer Error",
"Error converting result " + e.toString());
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonResponse;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.i("result", "result: "+result);
if(result == "1"){
holder.favourites_imageView.setImageResource(R.drawable.icon_fav_off);
}
}
答案 0 :(得分:1)
您可以使用以下方法更新列表中的项目:
private void updateView(int index){
View v = yourListView.getChildAt(index -
yourListView.getFirstVisiblePosition());
if(v == null)
return;
TextView someText = (TextView) v.findViewById(R.id.sometextview);
someText.setText("Hi! I updated you manually!");
}
以下定义的textview相当于创建您实际想要更新的视图实例。
答案 1 :(得分:0)
为MarkUnFavourite_WS
public MarkUnFavourite_WS(ViewHolder viewHolder){
this.holder=viewHolder;
}
并删除
holder = new ViewHolder();
初始化
在doInBackground
任务的MarkUnFavourite_WS
中。