访问GridView Adapter内部的父Activity以获取TextView值并更改它



我想从适配器中获取TextView值并更改它。

适配器中有Button,当我按下该按钮时,我想更改适配器中的TextView值。这可能吗?

这是我的适配器:

public class CustomGridView3 extends BaseAdapter {
    private ArrayList<ListItem> listData = new ArrayList<ListItem>();
    private LayoutInflater layoutInflater;
    private Context context;
    private int count = 0;
    CustomGridView3 adapter = this;
    int hargax,qtyx,totalx;
    public CustomGridView3(Context context, ArrayList<ListItem> listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(context);
        this.context = context;
    }
    @Override
    public int getCount() {
        return listData.size();
    }
    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.afterlogin_product_gridview, null);
            holder = new ViewHolder();
            holder.headlineView = (TextView) convertView.findViewById(R.id.nama_produk);
            holder.teaserView = (TextView) convertView.findViewById(R.id.harga);
            holder.imageView = (ImageView) convertView.findViewById(R.id.img_produk);
            holder.cmdMinus = (Button) convertView.findViewById(R.id.btn_min);
            holder.cmdPlus = (Button) convertView.findViewById(R.id.btn_plus);
            holder.qty = (TextView) convertView.findViewById(R.id.lbl_qty);
            holder.layout1 = (LinearLayout) convertView.findViewById(R.id.layout1);
            holder.harga = (TextView) convertView.findViewById(R.id.harga);
            holder.satuan = (TextView) convertView.findViewById(R.id.satuan);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final ListItem newsItem = listData.get(position);
        String satuan = newsItem.getSatuan().toString();
        String harga = newsItem.getReporterName().toString();
        harga = "Rp. " + harga + " / " + satuan;
        holder.headlineView.setText(newsItem.getHeadline().toUpperCase());
        holder.teaserView.setText(harga);
        SharedPreferences pref = context.getSharedPreferences("MyPref", 0); // 0 - for private mode
        Integer qtys = Integer.parseInt(pref.getString(newsItem.getHeadline() + "_003", "0"));
        newsItem.setQuantity(qtys);
        holder.qty.setText(String.valueOf(newsItem.getQuantity()));
        String a = newsItem.getUrl();
        holder.cmdMinus.setTag(position);
        holder.cmdPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count = newsItem.getQuantity();
                count++;
                newsItem.setQuantity(count);
                holder.qty.setText("" + count);
                SharedPreferences pref = context.getSharedPreferences("MyPref", 0); // 0 - for private mode
                SharedPreferences.Editor editor = pref.edit();
                editor.putString(newsItem.getHeadline() + "_001", newsItem.getReporterName()); // harga
                editor.putString(newsItem.getHeadline() + "_002", newsItem.getHeadline());  //nama
                editor.putString(newsItem.getHeadline() + "_003", String.valueOf(count));  //quantity
                editor.putString(newsItem.getHeadline() + "_004", newsItem.getSatuan());  //satuan
                editor.putString(newsItem.getHeadline() + "_005", newsItem.getUrl());  //url
                editor.commit();
            }
        });
        holder.cmdMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count = newsItem.getQuantity();
                count--;
                    newsItem.setQuantity(count);
                    holder.qty.setText("" + count);
                    SharedPreferences pref = context.getSharedPreferences("MyPref", 0); // 0 - for private mode
                    SharedPreferences.Editor editor = pref.edit();
                    editor.putString(newsItem.getHeadline() + "_001", newsItem.getReporterName()); // harga
                    editor.putString(newsItem.getHeadline() + "_002", newsItem.getHeadline());  //nama
                    editor.putString(newsItem.getHeadline() + "_003", String.valueOf(count));  //quantity
                    editor.putString(newsItem.getHeadline() + "_004", newsItem.getSatuan());  //satuan
                    editor.putString(newsItem.getHeadline() + "_005", newsItem.getUrl());  //url
                    editor.commit();
                    if(count == 0)
                    {
                        SharedPreferences prefs = context.getSharedPreferences("MyPref", 0); // 0 - for private mode
                        SharedPreferences.Editor editors = prefs.edit();
                        editors.remove(newsItem.getHeadline() + "_001");
                        editors.remove(newsItem.getHeadline() + "_002");
                        editors.remove(newsItem.getHeadline() + "_003");
                        editors.remove(newsItem.getHeadline() + "_004");
                        editors.remove(newsItem.getHeadline() + "_005");
                        editors.commit();
                        listData.remove(position);
                        adapter.notifyDataSetChanged();
                    }
            }
        });
        holder.qty.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                hargax = Integer.parseInt(newsItem.getReporterName());
                qtyx = newsItem.getQuantity();
                hargax = hargax * qtyx;
                if (context instanceof AfterLogin_Cart)
                {
                    ((AfterLogin_Cart)context).getPrice();
                }
            }
            @Override
            public void afterTextChanged(Editable s) {
            ***** Change TextView from Parent Activity *******
            }
        });
        if (holder.imageView != null) {
            //new ImageDownloaderTask(holder.imageView).execute(newsItem.getUrl());
            Picasso
                    .with(context)
                    .load(a)
                    .fit()
                    .noFade()
                    .into(holder.imageView);
        }
        return convertView;
    }
    static class ViewHolder {
        TextView headlineView;
        TextView teaserView;
        ImageView imageView;
        TextView qty;
        Button cmdPlus,cmdMinus;
        LinearLayout layout1;
        TextView harga,satuan;
    }

从addTextChangeListener,我想更改父活动的TextView值。我已经在搜索的只是执行一些方法。

 if (context instanceof AfterLogin_Cart)
 {
    ((AfterLogin_Cart)context).getPrice();
 }

好的,这里有一些示例代码来解释我上面的评论。因此,首先在适配器类中声明一个委托变量:

public class CustomGridView3 extends BaseAdapter {
private ArrayList<ListItem> listData = new ArrayList<ListItem>();
private LayoutInflater layoutInflater;
private Context context;
private TextWatcher textWatcherDelegate;
private int count = 0;
CustomGridView3 adapter = this;
int hargax,qtyx,totalx;
public CustomGridView3(Context context, TextWatcher textWatcherDelegate, ArrayList<ListItem> listData) {
    this.listData = listData;
    this.textWatcherDelegate = textWatcherDelegate;
    layoutInflater = LayoutInflater.from(context);
    this.context = context;
}

当然,你需要从活动中传递它(或者从任何创建适配器的地方):

CustomGridView3 adapter = new CustomGridView3(your_context, this, your_list)

(这里我假设您的调用者类实现了TextWatcher,所以我们可以使用this

然后在适配器的getView方法中委托方法调用:

holder.qty.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            textWatcherDelegate.beforeTextChanged(s, start, count, after);
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            hargax = Integer.parseInt(newsItem.getReporterName());
            qtyx = newsItem.getQuantity();
            hargax = hargax * qtyx;
            if (context instanceof AfterLogin_Cart)
            {
                ((AfterLogin_Cart)context).getPrice();
            }
            textWatcherDelegate.onTextChanged(s, start, before, count);
        }
        @Override
        public void afterTextChanged(Editable s) {
        ***** Change TextView from Parent Activity *******
            textWatcherDelegate.afterTextChanged(s);
        }
    });

因此,您可以在活动

中处理TextWatcher的方法调用

最新更新