2014-11-04 16 views
1

下午好,findViewByIdAndroid的電話從適配器自定義類

所以我對此有一個爲每個行的複選框的項目的自定義適配器的自定義的ListView。在我的getView中,我爲我的複選框實現了setOnCheckedChangeListener和onCheckedChanged處理程序。現在

,問題是:

每當我選中/取消選中列表中的項目之一,我想更新我想(假設每個項目的值的外部的TextView有關聯,使得價格我想在名單下面顯示總價格)。

我該如何從getView的適配器到達「外部」視圖?我還有其他什麼解決方法?

我離開這裏我的代碼一些部分我的自定義適配器的getView功能:

CheckBox name = (CheckBox) view.findViewById(R.id.product); 
    name.setText(content[i][0]); 
    final View v = view; 
    final int position = i; 
    name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton group, boolean isChecked) { 
      setCheckedItem(position); 
      EditText quantity = (EditText) v.findViewById(R.id.product_quantity); 
      content[position][3] = quantity.getText().toString(); 
      Iterator<String> it = getCheckedItems().values().iterator(); 
      Double total = 0.0; 
      for (int i=0;i<getCheckedItems().size();i++){ 
       Integer quantity_p = Integer.parseInt(getItem(position)[3]); 
       Double price = Double.parseDouble(getItem(position)[2]); 
       total += quantity_p*price; 
      } 
      TextView total_price = (TextView) findViewById(R.id.total_products_price); 
      total_price.setText(total.toString()); 
     } 
    }); 

注意最後兩行:我知道我不能調用findViewById,但我不知道是什麼現在要做。任何建議都會很好,謝謝。

+0

可以把你的類代碼和xml文件放在這裏嗎? – 2014-11-04 11:55:30

+0

將您的適配器類放入活動類中。 – 2014-11-04 11:58:51

回答

1

您可以將您的TextView傳遞給構造函數中的適配器。在此之後,你可以有一個private static類將實施CompoundButton.OnCheckedChangedListener這樣的:

private static class MyOnCheckedChangedListener 
        implements CompoundButton.OnCheckedChangedListener { 
    TextView myView; 
    public MyOnCheckedChangedListener (TextView viewToChange) { 
     myView = viewToChange; 
    } 

    @Override 
    public void onCheckedChanged(CompoundButton group, 
           boolean isChecked) { ... } 
} 

這只是setOnCheckedChangedListenernew MyOnCheckedChangedListener (myTextView)(你傳遞給Adapter的)後,你準備好去。

+0

我所做的就是在構造器中傳遞TextView,然後每當我喜歡時就使用它,它完美地工作:D – user3424226 2014-11-06 12:06:14

0

我可以做這樣的:

  1. 你可以通過TextView的參考適配器的構造,並用它來改變它的值。

  2. 但好方法是在您的活動和回調方法更新textview中實現interface。爲您在構造函數中傳遞適配器活動引用並使用其引用來調用interface回調方法。

一)有interace作爲

public Interface UpdateTextviewInterface { void updateTextview(String value); }

二)讓你的活動實現這個接口

public MainActivity extends Activity implments UpdateTextviewInterface

並覆蓋方法。

c)將活動引用傳遞給適配器構造函數。使用活動參考調用updateTextview方法來更新您的textview。

0

這只是一個思想和理論在這裏。但是,你可以在你的活動中創建一個靜態方法,在那裏你想改變的TextView被聲明爲,並且在那個方法中寫一個代碼來改變你的文本,你希望它改變的方式。

然後從BaseAdapter調用該靜態方法。 我說靜態,所以你不必做一個整個類的新實例。

+0

我試着添加調用findViewById的靜態方法para,它是不允許的。 (「非靜態方法findViewById不能從靜態上下文中引用。」) – user3424226 2014-11-06 12:05:16

2

將您的適配器類放入活動類中。在你上創建的主要活動類 然後

total_price = (TextView) findViewById(R.id.total_products_price); 

聲明TextView total_price。 然後你可以訪問onCheckedChanged裏面的total_price。試試這個,它可能會奏效。

+0

它可能工作,但我真的很想從該解決方案中逃離,因爲它會混淆在單個文件中混合的所有東西:/ – user3424226 2014-11-06 12:05:35

+0

Plz分享完整的代碼,我怎樣才能以這種方式得到身份證? – 2017-10-18 10:27:29