android - 根据 Json 的响应更改 onClick 适配器中的图像

标签 android json listview adapter

我正尝试在 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);
        }
    }

最佳答案

您可以使用以下方法更新列表中的项目:

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 等同于创建您实际想要更新的 View 的实例。

关于android - 根据 Json 的响应更改 onClick 适配器中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30412966/

相关文章:

javascript - Chrome 扩展的 Manifest.json 中的自定义值?

java - 通过CUSTOM listview显示SQLite信息

左侧带有投票按钮的Android Listview

带 Tiles 的 ListView 在 Flutter 中不起作用

android - TensorFlow-Lite 预训练模型在 Android 演示中不起作用

android - 哪一个最适合计算两点之间的距离?

java - 使用 Java 和 JSON 向服务器 API 发出请求

java - 如何使用java运行JSON

android - 如何在 ConstraintLayout 中制作具有最小高度的可拉伸(stretch) View ?

Android child Activity 和方向锁定