ClearCallLog 不更新列表视图

Posted

技术标签:

【中文标题】ClearCallLog 不更新列表视图【英文标题】:ClearCallLog not updating listview 【发布时间】: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! 【参考方案1】:

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

【讨论】:

我已经在 updateFragment2ListView() 下称它为 bro@Aubtin Samai,点击 MENU 中的 clearcalllog 就会调用它! 删除 list.setAdapter(rr);从 updateFragment2ListView() ...你不需要它两次。

以上是关于ClearCallLog 不更新列表视图的主要内容,如果未能解决你的问题,请参考以下文章

如何从 postgresql 视图表中获取要显示的对象列表

无法将查询另存为视图表

从视图表中获取数据时出现 Laravel 错误

文本文件字符串拆分然后将每个元素输出到视图表中

SQL 如何查询指定架构中所有表(或视图)的名称

mysql中的视图--数据操作