Android 将新 Cursor 设置为 listView

发布于 2024-12-27 14:06:56 字数 7429 浏览 1 评论 0原文

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

问题是,当我开始输入编辑文本时,我可以在 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);

        }
    });

所以任何想法如何我使用新适配器刷新列表视图。我已经尝试过 adapter.notifySetDataChanged(); 但它不起作用。

提前致谢!

I have a little issue with settings a new Adapter to a listView when user enter letters in EditText. So the thing which I'm doing is, when my Activity starts for the first time, I'm populating the listView from Database with Cursor and Custom Adapter. When user enter some text in EditText I'm creating new sql statement and getting the data with new Cursor. After that I'm creating new Custom Adapter and trying to set it to my list view.

The problem is that when I start typing in edit text I can saw in Logs from LogCat that the sql statement is the right one, and cursor size, but my ListView isn't populated with the new data. It's staying the same. Here is how I'm doing it :

This is how I'm populating the listView for first time :

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);

and this is how I'm creating the new Cursor and adapter with 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);

        }
    });

So any ideas how can I refresh my list View with the new adapter. I've already tried with adapter.notifySetDataChanged(); and it's not working.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

当爱已成负担 2025-01-03 14:06:56

清除您的数据源,在本例中是路径、名称、类别、计数。
将新结果加载到列表(路径、名称、类别、计数)的数据源中,并将列表适配器设置为空,并调用notifyDataSetChanged。

clear your data sources in this case it is paths, names, categories, count.
Load new results into data sources of list(paths, names, categories, count), and don's set list adapter to null, and call notifyDataSetChanged.

长梦不多时 2025-01-03 14:06:56

我解决了这个问题,在我的 afterTextChanged() 中,我只是清除用于存储数据的数组列表并将新数据放入其中。

I fix that, in my afterTextChanged() I'm just clearing the arraylists using for storing the data and putting the new data on them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文