onActivityResult在片段中不起作用

时间:2018-05-20 13:18:36

标签: java android

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        v = inflater.inflate(R.layout.fragment_fragment_home, container, false);


        FloatingActionButton floatingActionButton=(FloatingActionButton) v.findViewById(R.id.picker);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ContextCompat.checkSelfPermission(getActivity() , Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
                    new MultiContactPicker.Builder(getActivity()) //Activity/fragment context
                            .hideScrollbar(false) //Optional - default: false
                            .showTrack(true) //Optional - default: true
                            .searchIconColor(Color.WHITE) //Optional - default: White
                            .setChoiceMode(MultiContactPicker.CHOICE_MODE_MULTIPLE) //Optional - default: CHOICE_MODE_MULTIPLE
                            .handleColor(ContextCompat.getColor(getActivity() , R.color.colorPrimary)) //Optional - default: Azure Blue
                            .bubbleColor(ContextCompat.getColor(getActivity() , R.color.colorPrimary)) //Optional - default: Azure Blue
                            .bubbleTextColor(Color.WHITE) //Optional - default: White
                            .showPickerForResult(CONTACT_PICKER_REQUEST);
                }else{
                    Toast.makeText(getActivity(), "Remember to go into settings and enable the contacts permission.", Toast.LENGTH_LONG).show();
                }
            }
        });
        recyclerView=(RecyclerView) v.findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        listItems = new ArrayList<>();

        return v;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == CONTACT_PICKER_REQUEST){
            if(resultCode == RESULT_OK) {
                List<ContactResult> results = MultiContactPicker.obtainResult(data);
                for (int i=0;i<results.size();i++){
                    ListItem listItem = new ListItem(
                            "Name:  "+results.get(i).getDisplayName(),
                            "Contact No: "+results.get(i).getPhoneNumbers()
                    );
                    listItems.add(listItem);
                }

                adapter = new MyRecycleAdapter(listItems,this.getActivity());
                recyclerView.setAdapter(adapter);

                Toast.makeText(getActivity(), "MyTag"+results.get(0).getDisplayName(), Toast.LENGTH_SHORT).show();
                Log.d("MyTag", results.get(0).getDisplayName());
            } else if(resultCode == RESULT_CANCELED){
                System.out.println("User closed the picker without selecting items.");
            }
        }
    }

如何在片段中使用此onActivityResult?每当我最后运行此代码时,&#34;适配器未连接&#34;如果这个方法被用作受保护它可以工作,但片段不支持受保护的任何人都可以帮助我,请。

2 个答案:

答案 0 :(得分:1)

不确定您正在使用哪个MultiContactPicker,但如果是这个https://github.com/broakenmedia/MultiContactPicker,看起来您可以/应该在实例化Builder时传递该片段。所以改变这一行:

new MultiContactPicker.Builder(getActivity())

为:

new MultiContactPicker.Builder(this)

答案 1 :(得分:0)

请勿在片段super中致电onActivityResult,请删除此行:

super.onActivityResult(requestCode, resultCode, data);

并覆盖onActivityResult中的Activity并在那里致电super

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}

修改

在查看你的项目之后,我发现了问题,这是你得到的两个答案的组合。首先在MainActivity添加onActivityResult,因此您的MainActivity将如下所示:

public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private viewPagerAdapter viewPagerAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPagerAdapter = new viewPagerAdapter(getSupportFragmentManager());

//        Add Fragment Here

        viewPagerAdapter.addFragment(new FragmentHome(),"Home");
        viewPagerAdapter.addFragment(new FragmentRent(),"Rent");

        viewPager.setAdapter(viewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

然后在FragmentHome floatingActionButton方法的onClick中更改此行:

new MultiContactPicker.Builder(getActivity())

到这一行:

new MultiContactPicker.Builder(FragmentHome.this)

它会起作用,我测试过,之后会出现联系。