【问题标题】:ClearCallLog not updating listviewClearCallLog 不更新列表视图
【发布时间】:2017-05-21 02:26:29
【问题描述】:

我是一名学习安卓的学生。我正在做一个简单的联系人应用程序!当我点击清除通话记录按钮时,我最近的联系人列表视图保持不变。它没有被清除。但是当我关闭应用程序并重新打开时,最近的联系人片段正在被清除!我调试了代码,当我单击 clearcall 日志时,在 cursor.movetoNext() 行之后没有输入代码。请技术人员帮助我!

当我清除菜单中的通话记录按钮时,我编写的 updateFragment2ListView() 会被调用!

public class RecentContacts extends Fragment {
    HashMap contactMap = new HashMap();
    View rootView;
    RecentAdapter rr;
    ListView list;
    private static final int PERMISSIONS_REQUEST_READ_CALLLOG = 100;
    Cursor cursor;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_recent_contacts, container, false);
        list = (ListView) rootView.findViewById(R.id.customlist);
        getRecentContacts();
        rr = new RecentAdapter(contactMap, getActivity());
        list.setAdapter(rr);
        return rootView;
    }

    public void updateFragment2ListView() {
        getRecentContacts();
        rr.notifyDataSetChanged();
        list.setAdapter(rr);
        System.out.println("Fragment recent updated-updateFragment2listview");
    }

    @Override
    public void onResume() {
        getRecentContacts();
        rr.notifyDataSetChanged();
        System.out.println("Fragment recent updated- onresume");
        super.onResume();
    }

    @Override
    public void onStart() {
        System.out.println("Fragment recent onstart");
        super.onStart();
    }

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

    void getRecentContacts() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && getContext().checkSelfPermission(Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_CALL_LOG}, PERMISSIONS_REQUEST_READ_CALLLOG);
            //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method

            System.out.println("Security check ok");
        } else {
            System.out.println("Entered recent onstart");
            int i=0;

            Uri queryUri = android.provider.CallLog.Calls.CONTENT_URI;
            String[] projection = new String[]{
                    ContactsContract.Contacts._ID,
                    CallLog.Calls._ID,
                    CallLog.Calls.NUMBER,
                    CallLog.Calls.CACHED_NAME,
                    CallLog.Calls.DATE,
                    CallLog.Calls.TYPE};

            String sortOrder = String.format("%s limit 500 ", CallLog.Calls.DATE + " DESC");

            try {
                System.out.println("Entering cursor in recent contacts");
                cursor = getActivity().getContentResolver().query(queryUri, projection, null, null, sortOrder);
                System.out.println("Entered cursor in recent contacts");

            } catch (SecurityException e) {
                Log.e("", "");
            }

            while (cursor.moveToNext()) {
                System.out.println("Entered cursor.movetoNext recent contacts");
                String phoneNumber = cursor.getString(cursor
                        .getColumnIndex(CallLog.Calls.NUMBER));
                System.out.println("Entered phone number in recent contacts");
                String title = (cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)));

                int duration1 = cursor.getColumnIndex(CallLog.Calls.DURATION);
                System.out.println("Duration" + duration1);
                System.out.println("Entered duration in recent contacts");

                int date = cursor.getColumnIndex(CallLog.Calls.DATE);
                String callDate = cursor.getString(date);
                Date callDayTime = new Date(Long.valueOf(callDate));
                System.out.println("Call Date" + callDayTime);

                int type = cursor.getColumnIndex(CallLog.Calls.TYPE);
                String callType = cursor.getString(type);

                String dir = null;
                int dircode = Integer.parseInt(callType);
                switch (dircode) {
                    case CallLog.Calls.OUTGOING_TYPE:
                        dir = "OUTGOING";
                        break;

                    case CallLog.Calls.INCOMING_TYPE:
                        dir = "INCOMING";
                        break;

                    case CallLog.Calls.MISSED_TYPE:
                        dir = "MISSED";
                        break;
                }

                System.out.println("Call type" + dir);

               // if (phoneNumber == null || title == null) continue;

                String uri = "tel:" + phoneNumber;
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse(uri));
                String intentUriString = intent.toUri(0);

                contactMap.put(i, new RecentPojo(title, phoneNumber, duration1, false, callDayTime, dir));
                //  Toast.makeText(this,title,Toast.LENGTH_SHORT).show();

                i++;

            }
            cursor.close();
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        if ((requestCode == PERMISSIONS_REQUEST_READ_CALLLOG)) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission is granted
                System.out.println("");
                getRecentContacts();
            } else {
                Toast.makeText(getContext(), "Until you grant the permission, we cannot display the names", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

【问题讨论】:

  • 我认为您需要尝试在 while 循环之前清除您的contactMap。从数据库中删除所有数据后,我认为游标返回为 null 并且不能 moveToNext(),因此它不会进入 while 循环。希望对您有所帮助!
  • 是的,已经做到了,并且得到了 o/p !谢谢@I_A_Mok!

标签: android listview android-adapter android-contacts


【解决方案1】:

您必须在清除它时调用rr.notifyDataSetChanged();,以便刷新 ListView。

【讨论】:

  • 我已经在 updateFragment2ListView() 下称它为 bro@Aubtin Samai,点击 MENU 中的 clearcalllog 就会调用它!
  • 删除 list.setAdapter(rr);从 updateFragment2ListView() ...你不需要它两次。
猜你喜欢
  • 2011-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多