更新Custom Adapter android中的特定ViewHolder项值

时间:2016-02-11 11:26:12

标签: android listview android-viewholder

我正在使用ViewHolder绑定自定义ListView。一切都很好。现在,我在AsyncTask点击时调用Button,我想更新特定项目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...
        }
    }

2 个答案:

答案 0 :(得分:1)

   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);
            }
        }

答案 1 :(得分:0)

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