2016-01-23 64 views
-1

之後未找到我有具有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。

+0

部分代碼會幫助我們發現問題 – Raghunandan

+0

問題 – rjdthegreat

+0

中添加的代碼上下文沒有findViewById。 http://developer.android.com/reference/android/content/Context.html。而且你發佈棧跟蹤也 – Raghunandan

回答

1

要設置整數到一個TextView

textViewAmountPayable.setText(grandTotal); 

更改爲

textViewAmountPayable.setText(String.valueOf(grandTotal.intValue())); 

的setText查找具有ID的資源。 Id是一個整數。如果沒有找到你會得到ResourceNorFoundException。

你需要的是CharacerSequence。 Look @ setText methods @http://developer.android.com/reference/android/widget/TextView.html

+0

找到它。失誤錯誤。我認爲整數字符串應該是微不足道的。由於編譯器沒有哭,我沒有打擾。但事實證明(int)具有不同的含義 – rjdthegreat