我想从适配器获取TextView值并进行更改。
我在Button内部有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();
}
答案 0 :(得分:0)
好的,这里有一些示例代码来解释我上面的评论。首先,在适配器类中声明一个委托变量:
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的方法调用