更新自定义适配器中的特定 ViewHolder 项目值 android

Update particular ViewHolder item value in Custom Adapter android

我正在使用 ViewHolder 绑定自定义 ListView。一切正常。现在,我在 Button 单击时调用 AsyncTask,我想更新特定项目 TextView。我怎样才能做到这一点 ?

我的代码:

    static class ViewHolder {
        TextView tvMRP;
        ImageView imgUpdate;
    }

    public View getView(int position, final View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;

            vi = convertView;
            if (convertView == null) {
                vi = inflater.inflate(R.layout.my_cart_item, parent, false);
                viewHolder = new ViewHolder();

                viewHolder.tvMRP = (TextView) vi.findViewById(R.id.tvMRP);
                viewHolder.imgUpdate = (ImageView) vi.findViewById(R.id.imgUpdate);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            vi.setTag(viewHolder);

        viewHolder.imgUpdate.setTag(cart.get("UserId"));
        viewHolder.imgUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // your code here

                UserId = (String) viewHolder.imgUpdate.getTag();

                new UpdateTask().execute();

                notifyDataSetChanged();
            }
        });
    }

    public class UpdateTask extends AsyncTask<String, String, String> {
        @Override
        protected void onPostExecute(String result) {
              // If I click on first row item imgButton then I want to update tvMRP value of that first row item...
        }
    }

在适配器构造函数中传递 TextView。 运行 单击按钮时的任务。任务完成后,更新您的 TextView.. 就像普通的 TextView 一样....

   public void onClick(View view) {
                // your code here

                UserId = (String) viewHolder.imgUpdate.getTag();
               update(viewHolder.tvMRP);
            }
        });

方法 -

 private void update(TextView mTvMRP) {

            AsyncTask<String, String, String> _Task = new AsyncTask<String, String, String>() {


                @Override
                protected void onPreExecute() {

                }

                @Override
                protected String doInBackground(String... arg0) {
                    return null;

                }

                @Override
                protected void onPostExecute(String result) {
                     // do your stuff here and set the value.


                }
            };
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                _Task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (String[]) null);
            } else {
                _Task.execute((String[]) null);
            }
        }