Android将新的Cursor设置为listView

时间:2012-01-16 09:59:19

标签: android listview adapter

当用户在Adapter中输入字母时,设置新的listViewEditText时遇到一些问题。所以我正在做的事情是,当我的Activity第一次启动时,我正在使用Cursor和Custom Adapter从Database填充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

创建新的Cursor和适配器的方法
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();并且它无效。

提前致谢!

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

清除您的数据源,在这种情况下,它是路径,名称,类别,计数。 将新结果加载到列表(路径,名称,类别,计数)的数据源中,并将don的set list adapter设置为null,并调用notifyDataSetChanged。