我有一个有两个CardViews的活动布局: CardView1有1个ListView CardView2有1个整数(最终金额)
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;
}
}
在Activity onCreate中,我为CardView1的ListView设置了适配器。 我的ListView有一个删除按钮。一旦我单击删除按钮,CardView1表现完美,但我想每次从ListView中删除项目时更新CardView2中的整数。
我试过了:
1)在RemoveButton onClick中,保存上下文对象并尝试context.findViewById(R.id.final_amount)。上下文没有findViewById方法
2)创建了一个名为amountUpdatedListener的接口,在Activity中实现了该方法。将侦听器传递给适配器并调用listener.amountUpdated(finalAmount),但仍然没有说出ResourceNotFoundException。
答案 0 :(得分:1)
您正在将Integer设置为TextView
textViewAmountPayable.setText(grandTotal);
更改为
textViewAmountPayable.setText(String.valueOf(grandTotal.intValue()));
setText查找具有id的资源。 Id是整数。如果没有找到,你会得到ResourceNorFoundException。
你需要的是CharacerSequence。看看@ setText方法@ http://developer.android.com/reference/android/widget/TextView.html