2016-10-14 180 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

只修改活动界面中的实例?谢谢。 –