2016-10-14 129 views
-1

從片段獲取HashMap值我想使用接口來實現從片段傳遞數據到包含onClick事件的活動按鈕的通信。我可以看到HashMap可以在editText字段中寫入有效的數據,但這些值不能發送到活動。它會顯示錯誤並在我觸發活動上的onClick事件後停止。通過接口

我對界面的使用感到困惑。嘗試進行調試後出現如下錯誤,浪費大約3天時間處理,仍然無法解決。任何人都可以推薦或討論如何解決它,謝謝。

的錯誤:

Error:(77, 5) error: method does not override or implement a method from a supertype 
Error:(39, 8) error: Fragment_step_2 is not abstract and does not override abstract method onPassValueStep2() in onPassValue2 
Error:(231, 32) error: method onPassValueStep1 in class Fragment_step_1 cannot be applied to given types; 
required: HashMap 
found: no arguments 
reason: actual and formal argument lists differ in length 
Error:(232, 32) error: method onPassValueStep2 in class Fragment_step_2 cannot be applied to given types; 
required: HashMap 
found: no arguments 
reason: actual and formal argument lists differ in length 
Error:(78, 5) error: method does not override or implement a method from a supertype 
Error:(36, 8) error: Fragment_step_1 is not abstract and does not override abstract method onPassValueStep1() in onPassValue 

Calling Fragments' HashMap to activity

主要活動:

public interface onPassValue{ 
    Map<Object, String> onPassValueStep1(); 
} 

public interface onPassValue2{ 
    Map<Object, String> onPassValueStep2(); 
} 

protected void onCreate(Bundle savedInstanceState) { 
    ...... 
    btn_sendInsureInfo.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View view){ 
      //CALL here 
      //Fragment_step_1.onPassValueStep1(); 
      //Fragment_step_2.onPassValueStep2(); 
      ...... 
     } 
} 
...... 

Fragment_step_1:(xxx是活動的名字)

public class Fragment_step_1 extends Fragment implements xxx.onPassValue { 
    ...... 
    HashMap insureApplicant = new HashMap<>(4); 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public void onAttach(Context xxx){ 
    super.onAttach(xxx); 

    /*try { 
     passValue = (onPassValue) xxx; 
    } catch (ClassCastException e) { 
     throw new ClassCastException(pingan_insure_info.toString() 
       + " didn't implement onPassValue"); 
    }*/ 
} 

@Override 
public Map<Object, String> onPassValueStep1(HashMap insureResult) { 
    for (Object key : insureResult.entrySet()) { 
     //System.out.println(key + " fragment_1 : " + insureResult.get(key)); 
     System.out.println(" fragment_1 : " + key); 
     Log.e("Hashmap", String.valueOf(insureResult)); 
    } 
    return insureResult; 
} 
    ...... 

Fragment_s tep_2:(xxx是活動的名字)

public class Fragment_step_2 extends Fragment implements xxx.onPassValue2{ 
...... 
RelativeLayout correspondence; 
HashMap insureApplicant2 = new HashMap<>(3); 

@Override 
public void onAttach(Context pingan_insure_info){ 
    super.onAttach(pingan_insure_info); 

    /*try { 
     passValueStep2 = (onPassValueStep2) xxx; 
    } catch (ClassCastException e) { 
     throw new ClassCastException(xxx.toString() 
       + " didn't implement onPassValue"); 
    }*/ 
} 

@Override 
public Map<Object, String> onPassValueStep2(HashMap insureApplicantStep2){ 
    for (Object key : insureApplicantStep2.entrySet()) { 
     System.out.println("fragment_2 : " + key); 
     Log.e("Hashmap2", String.valueOf(insureApplicantStep2)); 
    } 
    return insureApplicant2; 
} 

所有片段EDITTEXT會後EDITTEXT是用戶有效,打字,填寫發送的功能和存儲在HashMap中。

例如:(AddTextChangedListener與TextWatcher)

residentAddress.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} 

     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} 

     @Override 
     public void afterTextChanged(Editable editable) { 
      residentAddress.setOnFocusChangeListener(new View.OnFocusChangeListener(){ 
       @Override 
       public void onFocusChange(View view, boolean isFocus){ 
        if(!isFocus){ 
         if("".trim().equals(residentAddress.getText().toString())){ 
          rAddress.setError("Resident Address is required."); 
          strAddress = ""; 
          insureApplicant2.put(2, strAddress); 
         } else { 
          rAddress.setErrorEnabled(false); 
          rAddress.setError(null); 
          strAddress = residentAddress.getText().toString().trim(); 
          insureApplicant2.put(2, strAddress); 

          onPassValueStep2(insureApplicant2); 

         } 
        } 
       } 
      }); 

     } 
    }); 

回答

0

在你的界面的方法Fragment簽名是錯誤的。您聲明如下界面:

public interface onPassValue{ 
    Map<Object, String> onPassValueStep1(); 
} 

public interface onPassValue2{ 
    Map<Object, String> onPassValueStep2(); 
} 

,並在片段你有公共

public Map<Object, String> onPassValueStep1(HashMap insureResult) { 

Map<Object, String> onPassValueStep2(HashMap insureApplicantStep2){

,你可以注意到,在您的接口中聲明的方法沒有參數。

你可以改變你的方法聲明在你的界面,添加缺少的參數,或更改Fragment

+0

感謝方法的幫助。我在這裏犯了一個粗心的錯誤。 :')我怎樣才能通過按鈕點擊這些HashMap值? @Blackbelt是否在activity上聲明HashMap並在activity onClick事件上調用getHashMapStep1.onPassValueStep1(insureApplicant)......? –

+0

您必須修改接口。例如'Map onPassValueStep1(Map insureResult);'(對另一個做相同的操作)。 – Blackbelt

+0

只修改活動界面中的實例?謝謝。 –