之後未找到我有具有2個CardViews一個活動佈局: CardView1具有1周的ListView CardView2具有1整數(最終量)的Android資源充氣定製適配器
public class CheckoutActivity extends BaseAppCompatActivity implements AmountUpdateListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkout);
populateSevicesInCart();
}
private void populateSevicesInCart() {
ListView view = (ListView) findViewById(R.id.listView_content_checkout_cartItemsListView);
List<Service> services = CartDataAccessUtil.getServicesInCart();
CheckoutListServicesAdapter checkoutListServicesAdapter = new CheckoutListServicesAdapter(this, this, services);
view.setAdapter(checkoutListServicesAdapter);
}
public void updateAmountPayable(Integer grandTotal){
TextView textViewAmountPayable = (TextView) findViewById(R.id.textView_content_checkout_amount_payable);
textViewAmountPayable.setText(grandTotal);
}
佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.callsalon.activity.CheckoutActivity"
tools:showIn="@layout/activity_checkout"
android:orientation="vertical"
android:id="@+id/linearLayout_content_checkout_mainContent">
<android.support.v7.widget.CardView
android:layout_margin="10dp"
android:paddingTop="10dp"
card_view:cardElevation="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_margin="8dp"
android:id="@+id/listView_content_checkout_cartItemsListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_margin="10dp"
android:paddingTop="10dp"
card_view:cardElevation="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView_content_checkout_amount_payable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0.5"
android:gravity="right"
android:text="Rs. 0"
android:textColor="@color/grey" />
</android.support.v7.widget.CardView>
</LinearLayout>
適配器
public class CheckoutListServicesAdapter extends BaseAdapter {
private List<Service> services;
private static LayoutInflater inflater;
private Context context;
private CostAmounts costAmounts;
private AmountUpdateListener listener;
public CostAmounts getCostAmounts() {
return costAmounts;
}
public CheckoutListServicesAdapter(Context context, AmountUpdateListener listener, List<Service> services) {
super();
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.services = services;
this.listener = listener;
updateCostAmounts(services);
}
private void updateCostAmounts(List<Service> services){
costAmounts = new CostAmounts(services);
listener.updateAmountPayable(costAmounts.getGrandTotal());
User currentUser = UserDataAccessUtil.getUser();
currentUser.setAmountPayable(costAmounts.getGrandTotal());
UserDataAccessUtil.updateUser(currentUser);
}
@Override
public int getCount() {
...
}
@Override
public Object getItem(int position) {
..
}
@Override
public long getItemId(int position) {
..
}
@Override
public View getView(final int position, final View convertView, ViewGroup parent) {
removeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
services = CartDataAccessUtil.getServicesInCart();
updateCostAmounts(services);
notifyDataSetChanged();
}
});
}
return serviceListRow;
}
}
在該法案ivity onCreate,我爲CardView1的ListView設置適配器。 我的ListView有一個刪除按鈕。一旦我點擊刪除按鈕,CardView1的表現完美,但我想每次從ListView中刪除一個項目時更新CardView2中的整數。
我嘗試:
1)在RemoveButton的onClick,保存上下文對象,並試圖context.findViewById(R.id.final_amount)。上下文沒有findViewById方法
2)在Activity中創建一個名爲amountUpdatedListener的接口,實現的方法。將偵聽器傳遞給適配器並調用listener.amountUpdated(finalAmount),但仍然失敗,說ResourceNotFoundException。
部分代碼會幫助我們發現問題 – Raghunandan
問題 – rjdthegreat
中添加的代碼上下文沒有findViewById。 http://developer.android.com/reference/android/content/Context.html。而且你發佈棧跟蹤也 – Raghunandan