如何将更新的电话号码放入我为Android应用选择的字段中?

时间:2015-02-23 18:41:56

标签: android android-fragments android-contacts android-fragmentactivity

如何将更新的电话号码放入我选择的字段?我有选择器返回电话号码,但在错误的字段中。

enter image description here

选择联系人后,电话号码将被放置在错误的字段中。

enter image description here

负责此联系人选择器逻辑的我的片段在我的回购中 https://github.com/jackygrahamez/MayDay/blob/gradle2/app/src/main/java/com/mayday/md/common/ContactPickerFragment.java

我如何重构此代码以将数字放在正确的字段中?

02-23 12:47:59.217  12360-12360/com.mayday.md E/WizardActivity﹕ onUserInteraction
02-23 12:47:59.347  12360-12360/com.mayday.md E/WizardActivity﹕ onUserInteraction
02-23 12:47:59.347  12360-12360/com.mayday.md E/WizardActivity.onPause﹕ page = setup-contacts
02-23 12:47:59.347  12360-12360/com.mayday.md E/>>>>>>﹕ assert flagRiseFromPause = true
02-23 12:47:59.387  12360-12360/com.mayday.md D/AbsListView﹕ unregisterIRListener() is called
02-23 12:48:00.657  12360-12360/com.mayday.md D/WizardActivity.onStop﹕ page = setup-contacts
02-23 12:48:00.657  12360-12360/com.mayday.md D/AbsListView﹕ onVisibilityChanged() is called, visibility : 4
02-23 12:48:00.657  12360-12360/com.mayday.md D/AbsListView﹕ unregisterIRListener() is called
02-23 12:48:02.107  12360-12360/com.mayday.md E/ContactPickerFragment﹕ onActivityResult requestCode 65636
02-23 12:48:02.107  12360-12360/com.mayday.md E/ContactPickerFragment﹕ onActivityResult resultCode -1
02-23 12:48:02.107  12360-12360/com.mayday.md E/ContactPickerFragment﹕ onActivityResult data Intent { dat=content://com.android.contacts/data/2369 flg=0x1 }
02-23 12:48:02.117  12360-12360/com.mayday.md E/ContactPickerFragment﹕ onActivityResult id 2369
02-23 12:48:02.117  12360-12360/com.mayday.md E/ContactPickerFragment﹕ onActivityResult name XXX XXXX
02-23 12:48:02.147  12360-12360/com.mayday.md D/dalvikvm﹕ GC_FOR_ALLOC freed 598K, 22% free 29241K/37368K, paused 15ms, total 15ms
02-23 12:48:02.147  12360-12360/com.mayday.md E/WizardActivity﹕ onActivityResult pCur android.content.ContentResolver$CursorWrapperInner@4313d048
02-23 12:48:02.147  12360-12360/com.mayday.md E/ContactPickerFragment﹕ onActivityResult phone xxxxxxxxxx
02-23 12:48:02.147  12360-12360/com.mayday.md E/ContactPickerFragment﹕ onActivityResult phone xxxxxxxxxx
02-23 12:48:02.147  12360-12360/com.mayday.md E/ContactPickerFragment﹕ onActivityResult phoneNumberEditText android.widget.EditText{42c580b0 VFED..CL ........ 0,0-944,156 #7f0b0016 app:id/contact_edit_text}
02-23 12:48:02.147  12360-12360/com.mayday.md E/??????﹕ text changed
02-23 12:48:02.147  12360-12360/com.mayday.md D/WizardActivity.onStart﹕ page = setup-contacts
02-23 12:48:02.147  12360-12360/com.mayday.md E/WizardActivity.onResume﹕ pageId = setup-contacts and flagRiseFromPause = true
02-23 12:48:02.147  12360-12360/com.mayday.md E/WizardActivity.onResume﹕ back button pressed
02-23 12:48:02.147  12360-12360/com.mayday.md D/AbsListView﹕ onVisibilityChanged() is called, visibility : 0
02-23 12:48:02.147  12360-12360/com.mayday.md D/AbsListView﹕ unregisterIRListener() is called
02-23 12:48:02.167  12360-12360/com.mayday.md D/AbsListView﹕ unregisterIRListener() is called

NEW CLUE :我注意到请求代码根据我选择的字段返回不同的值:第一个字段requestCode 65636,第二个字段requestCode 131172,第三个字段requestCode 196708

1 个答案:

答案 0 :(得分:2)

您当前的设置有WizardActivity作为父活动,SetupContactsFragment作为片段,ContactPickerFragment作为子片段。当ContactPickerFragment发出startActivityForResult(...)来电时,onActivityResult(...)会收到WizardActivity来电。

问题:

首先,永远不会使用WizardActivity's成员变量contactPickerFragment。它不是你的ui的一部分。因此,在contactPickerFragment.onActivityResult(....)内调用WizardActivity#onActivityResult(...)除了打印几个日志语句之外什么都不做。此外,对super.onActivityResult(...)的调用完全丢失。正确的方法是检查请求代码是否由WizardActivity发出。如果不是,则调用super方法会将onActivityResult(..)调用路由到片段SetupContactsFragment

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // currently, WizardActivity does not deal with 
    // any onActivityResult callbacks
    super.onActivityResult(requestCode, resultCode, data);
}

SetupContactsFragment现在可以收到onActivityResult(...)回调。但是,我们需要识别onActivityResult(...)并将其发送到正确的子片段。这样做的一种方法是为每个子片段分配不同的requestCode。在SetupContactsFragment#onActivityResult(...)内,我们遍历所有子片段并发出对其onActivityResult(...)方法的调用。由于我们为每个片段分配了不同的requestCode,因此只会处理其中一个调用。

但是,我不明白为什么你需要三个相同的儿童片段,每个片段都有一个输入区域和一个输入区域。一个按钮。这些小部件都可以是SetupContactsFragemets' ui的一部分。即使规范在未来从3个联系人变为10个,您也可以实施一种方法,该方法可以多次膨胀和添加每一行。

在这种情况下,您需要3个唯一requestCodes。根据按下的ImageButtonrequestCode使用不同的startActivityForResult(...)。在onActivityResult(...)内,requestCode将指出哪些EditText需要更新。