2017-04-19 5 views
1

カスタムアレイアダプタ、CustomListCartItemがあります。カートアイテムが削除されるたびに、私の活動を更新したいと思います。これまでのところカートリストのアイテムを削除し、ArrayAdapterのアイテムも削除できます。しかし、自分のアクティビティでTextViewという名前のtxtSubtotalを更新したいと思います。カスタムアレイアダプタからアクティビティを更新

  1. btnRemove.setOnClickListenerは正しく取り付けられていますか?
  2. 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); 

    } 
} 
+0

あなたのデータが変更された後にアダプターを通知する必要があります。 –

+0

インターフェイスを作成し、それをアクティビティクラスに実装することができます。アダプタクラスのリスナーを追加します。 –

答えて

1

私は自分のアクティビティでリスナーを実装しました。

CartActivity:getViewメソッドの内部CustomListCartItemについては

public class CartActivity extends AppCompatActivity implements CustomListCartItem.ItemListener { 

@Override 
    public void onRemove(ModelCartItem cartItem) { 
     Controller.removeCartItem(cartItem.modelItem); 
     updateSubtotal(); 
    } 

if (itemListener != null) { 
    itemListener.onRemove(cartItem); 
} 

インタフェース:

private ItemListener itemListener; 

public void setItemListener(ItemListener listener) { 
    this.itemListener = listener; 
} 

public interface ItemListener { 
    void onRemove(ModelCartItem cartItem); 
} 
0

1. Addメソッド

updateSubTotal(String str) { 
    txtSubtotal.setTExt(str); //str OR whatvever you need to set. 
} 

2.(カスタムアダプタから)ボタンクリックリスナーのonClickの方法でコールupdateSubTotal

((CartActivity)context).updateSubTotal("yourvalue"); 
1

あなたCartActivity中のTextViewのデータを更新するためのメソッドを追加します。

public class CartActivity extends AppCompatActivity { 

    private TextView txtSubtotal; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_cart); 

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

    } 

    public void updateData(String data){ 
     txtSubtotal.setText(data); 
    } 
} 

あなたのアダプタでそのメソッドにアクセスし、削除すると値を変更します。

btnRemove.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Controller.removeCartItem(cartItem.modelItem); //remove from cart 
       remove(cartItem); //remove from adapter 
       ((CartActivity)context).updateData("Your updated data") 
      } 
     }); 

希望すると、これが役に立ちます。

関連する問題