简体   繁体   English

如何在Android应用中集成搜索功能

[英]How to integrate search functionality in Android app

I'm trying to create an application to display the call logs based on the call type (incoming calls, outgoing calls, or missing calls). 我正在尝试创建一个应用程序,以根据呼叫类型(来电,拨出电话或缺少电话)显示通话记录。 In addition I'm trying to add search and delete functionalities so the user can search for the call (by number) and delete the call. 此外,我正在尝试添加搜索和删除功能,以便用户可以搜索呼叫(按号码)并删除呼叫。 The layout of the app is shown here: 应用程序的布局如下所示:

应用程序的布局

The search functionality works if I hit the 'All' button to display all calls and search by number there, but upon going to other sections to search for a number, such as received or missed, the app crashes. 搜索功能有效,如果我点击“全部”按钮显示所有呼叫并按号码搜索,但在转到其他部分搜索某个号码(例如已接收或未接收)时,应用程序崩溃。

So far I have managed to run the logcat on Android Studio, and found that the main issue is in my main activity file. 到目前为止,我已经设法在Android Studio上运行logcat,发现主要问题出在我的主要活动文件中。 I have attached the logcat image here: 我在这里附加了logcat图像:

logcat的

There seems to be issues with these pieces of code: my getCalls Method, afterTextChanged Method, and my updateCursor Method. 这些代码似乎存在问题:我的getCalls方法,afterTextChanged方法和我的updateCursor方法。

private Cursor getCalls(int type, String searchString) {
        Uri callUri = CallLog.Calls.CONTENT_URI;
        ContentResolver contentResolver = getContentResolver();

        String[] projection = new String[]{Calls._ID, Calls.NUMBER, Calls.DURATION, Calls.TYPE};

        String selection=null;
        String[] selectionArgs=null;

    if(type != 0){
        // filter type calls
        selection = Calls.TYPE + "=?";
        selectionArgs = new String[]{String.valueOf(type)};
    }

    if(!TextUtils.isEmpty(searchString)) {
        // has some search string
        if(TextUtils.isEmpty(selection)) {
            // all call types
            selection = Calls.NUMBER + " LIKE ?";
            selectionArgs = new String[]{"%"+searchString+"%"};
        } else {
            // some type of call and add search String
            selection = selection+" && " + Calls.NUMBER+" LIKE ?";
            selectionArgs = new String[]{selectionArgs[0],"'%"+searchString+"%'"};
        }
    }

    String order = Calls.DATE + " DESC ";

    //verify permissions to access the user's call log
    int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG);

    if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
        cursor = contentResolver.query(callUri,   // URI content provider
                projection,
                selection,
                selectionArgs,
                order);
    }
    return cursor;
}

@Override
public void afterTextChanged(Editable s) {
    updateCursor();
}

//updates the search
void updateCursor() {
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
        cursor = null;
    }
    cursor = getCalls(currentCallType, searchET.getText().toString());
    adapter.swapCursor(cursor);
}

Upon running the app, I expected to be able to go to different sections (for example, I go to the received call section) and search for a number, however upon going to the section and tapping on the search bar, the app crashes. 在运行应用程序时,我希望能够转到不同的部分(例如,我转到收到的呼叫部分)并搜索一个号码,但是在转到该部分并点击搜索栏时,应用程序崩溃了。 I do not understand how there could be issues with these methods. 我不明白这些方法有什么问题。

SQLException says the error is near the & symbol, so the error should be in this line: SQLException表示错误在&符号附近,因此错误应该在这一行:

selection = selection+" && " + Calls.NUMBER+" LIKE ?";

Let's replace it with this one, because 'AND' should be used in SQL instead of double &: 让我们用这个替换它,因为'AND'应该用在SQL而不是double&:

selection = selection+" AND " + Calls.NUMBER+" LIKE ?";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM