カスタムアレイアダプタ、CustomListCartItemがあります。カートアイテムが削除されるたびに、私の活動を更新したいと思います。これまでのところカートリストのアイテムを削除し、ArrayAdapterのアイテムも削除できます。しかし、自分のアクティビティでTextViewという名前のtxtSubtotalを更新したいと思います。カスタムアレイアダプタからアクティビティを更新
btnRemove.setOnClickListener
は正しく取り付けられていますか?txtSubtotal
の値を変更するにはどうすればよいですか?
マイカスタムアレイアダプタ:
public class CustomListCartItem extends ArrayAdapter<ModelCartItem> {
public CustomListCartItem(Activity context, ArrayList<ModelCartItem> cartItems) {
super(context, R.layout.single_cart_item, cartItems);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(this.getContext())
.inflate(R.layout.single_cart_item, parent, false);
}
TextView txtItemName = (TextView) convertView.findViewById(R.id.txtItemName);
ImageView imgItem = (ImageView) convertView.findViewById(R.id.imgItem);
EditText txtQuantity = (EditText) convertView.findViewById(R.id.txtQuantity);
Button btnRemove = (Button) convertView.findViewById(R.id.btnRemove);
final ModelCartItem cartItem = getItem(position);
if (cartItem!= null) {
txtItemName.setText(cartItem.modelItem.itemName);
Integer imageRes = getContext().getResources().getIdentifier(cartItem.modelItem.imageName, "drawable", getContext().getPackageName());
imgItem.setImageResource(imageRes);
txtQuantity.setText(Integer.toString(cartItem.quantity));
btnRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Controller.removeCartItem(cartItem.modelItem); //remove from cart
remove(cartItem); //remove from adapter
}
});
}
return convertView;
}
}
私の活動:活動の
public class CartActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
TextView txtSubtotal = (TextView)findViewById(R.id.txtSubtotal);
txtSubtotal.setText(Controller.getCartSubtotal());
CustomListCartItem adapter = new
CustomListCartItem(CartActivity.this, Controller.getCartItems());
ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
}
}
あなたのデータが変更された後にアダプターを通知する必要があります。 –
インターフェイスを作成し、それをアクティビティクラスに実装することができます。アダプタクラスのリスナーを追加します。 –