为什么我的Android Activity在获取权限并向其添加列表数据后仍然为空?

时间:2016-10-28 13:09:13

标签: java android android-activity android-permissions

这是我的主要活动,其中包含请求阅读日历和通话电话的权限的方法,从日历提取电话号码等获取事件,并将其显示在活动上。但是在第一次启动后它仍然为空给予权限,为下次启动所有数据出现,为什么会发生这种情况?

    public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // get the listview

            expListView = (ExpandableListView) findViewById(R.id.lvExp);

            getPermissionToDialToAContact();
            getPermissionToReadCalender();
            // preparing list data


            if (listDataHeader != null && listDataChild != null) {


                listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

                // setting list adapter
                expListView.setAdapter(listAdapter);


                // Listview Group click listener
                expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

                    @Override
                    public boolean onGroupClick(ExpandableListView parent, View v,
                                                int groupPosition, long id) {
                        return false;
                    }
                });

                // Listview Group expanded listener
                expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

                    @Override
                    public void onGroupExpand(int groupPosition) {
                    }
                });

                // Listview Group collasped listener
                expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

                    @Override
                    public void onGroupCollapse(int groupPosition) {
                    }
                });

                // Listview on child click listener
                expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

                    @Override
                    public boolean onChildClick(ExpandableListView parent, View v,
                                                int groupPosition, int childPosition, long id) {
                        // TODO Auto-generated method stub
                     callPosition=   phoneNumber.size();
                        if(childPosition<callPosition){

                            dialContact(phoneNumber,childPosition);


                        }

                        return false;
                    }
                });
            }
        }


            }}
          @Override
        public void onRequestPermissionsResult(int requestCode,
                                               String permissions[], int[] grantResults) {
            switch (requestCode) {
                case READ_CALENDER: {

                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        Log.i("mainactonreqpermethod ","mainactonreqpermethod");
                        RuntimeData.setsCalendarData(getCalender(null));
                        prepareListData();

                    } else {
                        // permission denied, boo! Disable the
                        // functionality that depends on this permission.
                    }
                    return ;
                }
                // other 'case' lines to check for other
                // permissions this app might request




                case  CALL_PHONE: {

                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {


                    } else {
                        // permission denied, boo! Disable the
                        // functionality that depends on this permission.
                    }
                    return;
                }
            }
        }

        private void getPermissionToReadCalender() {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {

                if (shouldShowRequestPermissionRationale(
                        Manifest.permission.READ_CALENDAR)) {
                }
                requestPermissions(new String[]{Manifest.permission.READ_CALENDAR},
                        READ_CALENDER);
            }else {

                RuntimeData.setsCalendarData(getCalender(null));
                prepareListData();//adding data to list
            }

        }
     private void prepareListData() {
        //   mEventDesc=mData.getEventTitle();
        if (RuntimeData.getsCalendarData()!=null){
            Log.i("runtimedata","runtimedata");
            mData = RuntimeData.getsCalendarData();
            Log.i("runtimedatatomdata","runtimedatatomdata");
        }
        Log.i("list daataheader", "list daataheader" +mData.getEventTitle());
        if(mData.getEventTitle()!=null) {
            listDataHeader = new ArrayList<String>();
            phNum=new ArrayList<String>();
            child1=new ArrayList<String>();
            //   listDataHeader = mData.getEventTitle();
            listDataChild = new HashMap<String, List<String>>();
            Log.i("list daataheader", "list daataheader" + listDataHeader);
            Log.i("list daatachild", "list daatachild" + listDataChild);
            // Adding child data

            listDataHeader.add(mData.getEventTitle().get(0));
//            listDataHeader.add(mData.getEventTitle().get(1));
//            listDataHeader.add(mData.getEventTitle().get(2));
          mEventData=  extractDescription(mData);

//            listDataHeader.add("Top 250");
//            listDataHeader.add("Now Showing");
//            listDataHeader.add("Coming Soon..");
            // Adding child data
            if(mEventData!=null) {
                for (int i = 0; i < mEventData.getPhoneNumber().size(); i++) {
                    phNum = mEventData.getPhoneNumber();
                    child1.add(phNum.get(i));
                    Log.i("list daatachild", "list daatachild" + String.valueOf(phNum));

                }
                Log.i("list daataheader", "list daataheader" +child1.size());
                child1.add(mEventData.getMeetingID().get(0));
                child1.add(mEventData.getMeetingPassWord().get(0));
                listDataChild.put(listDataHeader.get(0), child1);
            }}


        private void getPermissionToDialToAContact() {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

                if (shouldShowRequestPermissionRationale(
                        Manifest.permission.CALL_PHONE)) {
                }
                requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
                        CALL_PHONE);
            }else {


            }

        }



    }

0 个答案:

没有答案