Android将新光标设置为listView

Android将新光标设置为listView,android,listview,adapter,Android,Listview,Adapter,当用户在EditText中输入字母时,我对将新的适配器设置为列表视图有点问题。所以,我正在做的事情是,当我的活动第一次启动时,我正在用游标和自定义适配器从数据库填充listView。当用户在EditText中输入一些文本时,我正在创建新的sql语句并使用新的游标获取数据。之后,我将创建新的自定义适配器,并尝试将其设置为我的列表视图 问题是,当我开始键入编辑文本时,我可以在LogCat的日志中看到sql语句是正确的,并且光标大小正确,但是我的ListView没有填充新数据。它保持不变。我是这样做的

当用户在
EditText
中输入字母时,我对将新的
适配器
设置为
列表视图
有点问题。所以,我正在做的事情是,当我的
活动第一次启动时,我正在用游标和自定义适配器从数据库填充listView。当用户在EditText中输入一些文本时,我正在创建新的sql语句并使用新的游标获取数据。之后,我将创建新的自定义适配器,并尝试将其设置为我的列表视图

问题是,当我开始键入编辑文本时,我可以在LogCat的日志中看到sql语句是正确的,并且光标大小正确,但是我的ListView没有填充新数据。它保持不变。我是这样做的:

这是我第一次填充listView的方式:

cursor = userDbHelper.executeSQLQuery(sqlQuery);

        if (cursor.getCount() == 0) {
            noCards.setVisibility(View.VISIBLE);
            noCards.setText(getString(R.string.no_cards));
        } else if (cursor.getCount() > 0) {
            noCards.setVisibility(View.GONE);
                for (cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
                    objectId = Integer.parseInt(cursor.getString(cursor.getColumnIndex("objectId")));
                    cardId.add(objectId);

                    title = cursor.getString(cursor.getColumnIndex("title"));
                    catTitle = cursor.getString(cursor.getColumnIndex("catTitle"));
                    int repeats = Integer.parseInt(cursor.getString(cursor.getColumnIndex("repeatsCount")));
                    count.add(repeats);

                    if(extra != 0){
                                String cardsm = "SELECT objectId FROM cardmedias "
                                                + "WHERE cardId="
                                                + objectId
                                                + " AND mediaType=" 
                                                + 5012;

                                Cursor cardsM = userDbHelper.executeSQLQuery(cardsm);
                                if (cardsM.getCount() == 0) {


                                } else if (cardsM.getCount() > 0) {
                                    for (cardsM.move(0); cardsM.moveToNext(); cardsM.isAfterLast()) {
                                        objectID = Integer.parseInt(cardsM.getString(cardsM.getColumnIndex("objectId")));

                                        String filename = "mediacard-"+objectID;
                                        if(storageID==1){
                                            path = RPCCommunicator.getImagePathFromInternalStorage(servername, userId, filename, this);
                                        } else if(storageID==2){
                                            path = RPCCommunicator.getImagePathFromExternalStorage(servername, userId, filename);
                                        }
                                        hm.put(objectID, path);

                                        path = hm.get(objectID);
                                        Log.i("","path : "+path);
                                        paths.add(path);
                                        names.add(title);
                                        categories.add(catTitle);
                                    }
                                }
                    } else if (extra==0){
                        names.add(title);
                        categories.add(catTitle);
                    }
                }
        }
        cursor.close();
        adapter = new LazyAdapter(this, paths, names, categories, count);
        listView.setAdapter(adapter);
这就是我用
TextWatcher
创建新光标和适配器的方法:

searchString = "";
    sqlQuery = getSqlQuery(sort, collId, ascDesc,searchString);

    searchBar.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        public void afterTextChanged(Editable s) {
            check = 1;
            listView.setAdapter(null);
            searchString = s.toString();
            Log.e("","searchString : "+searchString);
            sqlQuery = getSqlQuery(sort, collId, ascDesc,searchString);
            Log.e(""," sqlquery : "+sqlQuery);
            Cursor cursor = userDbHelper.executeSQLQuery(sqlQuery);
            Log.e("","cursor count : "+cursor.getCount());
            if (cursor.getCount() == 0) {
                noCards.setVisibility(View.VISIBLE);
                noCards.setText(getString(R.string.no_cards));
            } else if (cursor.getCount() > 0) {
                noCards.setVisibility(View.GONE);
                    for (cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
                        objectId = Integer.parseInt(cursor.getString(cursor.getColumnIndex("objectId")));
                        cardId.add(objectId);

                        title = cursor.getString(cursor.getColumnIndex("title"));
                        catTitle = cursor.getString(cursor.getColumnIndex("catTitle"));
                        int repeats = Integer.parseInt(cursor.getString(cursor.getColumnIndex("repeatsCount")));
                        count.add(repeats);

                        if(extra != 0){
                                    String cardsm = "SELECT objectId FROM cardmedias "
                                                    + "WHERE cardId="
                                                    + objectId
                                                    + " AND mediaType=" 
                                                    + 5012;

                                    Cursor cardsM = userDbHelper.executeSQLQuery(cardsm);
                                    if (cardsM.getCount() == 0) {

                                    } else if (cardsM.getCount() > 0) {
                                        for (cardsM.move(0); cardsM.moveToNext(); cardsM.isAfterLast()) {
                                            objectID = Integer.parseInt(cardsM.getString(cardsM.getColumnIndex("objectId")));

                                            String filename = "mediacard-"+objectID;
                                            if(storageID==1){
                                                path = RPCCommunicator.getImagePathFromInternalStorage(servername, userId, filename, OwnedStampii.this);
                                            } else if(storageID==2){
                                                path = RPCCommunicator.getImagePathFromExternalStorage(servername, userId, filename);
                                            }
                                            hm.put(objectID, path);

                                            path = hm.get(objectID);
                                            Log.i("","path : "+path);
                                            paths.add(path);
                                            names.add(title);
                                            categories.add(catTitle);
                                        }
                                    }
                        } else if (extra==0){
                            names.add(title);
                            categories.add(catTitle);
                        }
                    }
            }
            cursor.close();

            LazyAdapter adapter = new LazyAdapter(OwnedStampii.this, paths, names, categories, count);
            listView.setAdapter(adapter);

        }
    });
那么,我该如何使用新适配器刷新列表视图呢。我已经尝试使用
适配器.notifySetDataChanged()并且它不工作


提前谢谢

我修复了这个问题,在我的
PostTextChanged()
中,我只是清除用于存储数据并将新数据放在其上的ArrayList。

清除数据源在这种情况下是路径、名称、类别和计数。 将新结果加载到列表的数据源中(路径、名称、类别、计数),并将列表适配器设置为null,然后调用notifyDataSetChanged