SimpleCursorAdaptor未在Android中的ListView中显示结果

时间:2014-10-30 05:16:23

标签: android android-listview simplecursoradapter

我遇到的问题是“SimpleCursorAdapter”,它应该为数据库中的所有结果显示一行。我还有一个“editText”字段,应该用于过滤结果集。

问题是listView的默认视图是不显示值和空表。一旦我开始输入EditText,它就会过滤得很好。

是否可以使用“默认”视图(在EditText中未输入任何内容)来显示所有值?我该怎么办?

提前感谢您,以下是我的代码:

private void setupListView(ListView bandListView) {

    ListView listView = (ListView) bandListView.findViewById(R.id.BandSelectorListView);

    //Get my SQLite Helper Class
    final LiveMusicSqlLiteHelper lmslh = new LiveMusicSqlLiteHelper(context);   
    Cursor cursor = lmslh.getBandInfo();
      // The desired columns to be bound
      String[] columns = new String[] {
              LiveMusicArchiveConstants.bandAttributeName
      };

          // the XML defined views which the data will be bound to
          int[] to = new int[] { 
            R.id.bandNameTextBoxId,
          };

      @SuppressWarnings("deprecation")
      final SimpleCursorAdapter bandListAdapter = new SimpleCursorAdapter(context, R.layout.band_item_layout, cursor, columns,to);

      // Assign adapter to ListView
      listView.setAdapter(bandListAdapter);  

      //Set the click item handler
      listView.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
             Toast.makeText(context,
                        "Item in position " + position + " clicked",
                        Toast.LENGTH_LONG).show();  
        }});

      bandListAdapter.setFilterQueryProvider(new FilterQueryProvider() {

            @Override
            public Cursor runQuery(CharSequence constraint) {
                String partialValue = constraint.toString();
                return lmslh.getAllSuggestedValues(partialValue);

            }
        });

      //Create the TextWatcher.  This is used to monitor the edit text so that it can filter as typing
      TextWatcher filterTextWatcher = new TextWatcher() {

          public void beforeTextChanged(CharSequence s, int start, int count,int after) {  
          }  
          public void onTextChanged(CharSequence s, int start, int before,int count) {  
              bandListAdapter.getFilter().filter(s);
          }

          @Override
          public void afterTextChanged(Editable s) {
              // TODO Auto-generated method stub              
          }  
      };
      //Set the filter
      EditText filterEditText = (EditText)fragmentView.findViewById(R.id.bandFilterEditText);
      filterEditText.setVisibility(View.VISIBLE);
      filterEditText.addTextChangedListener(filterTextWatcher);
      //Close the cursor
      cursor.close();

}

2 个答案:

答案 0 :(得分:0)

当partialValue为null或为空时,lmslh.getAllSuggestedValues(partialValue)是否返回所有值?

请注意FilterQueryProvider.runQuery文档中的合同:

  

合约:当约束为空或为空时,原始结果,   在进行任何过滤之前,必须退回。

答案 1 :(得分:0)

好的,所以是的stooopid ....

我在方法结束时的cursor.close()上有点太热心了。这意味着第一次加载实际上是空的。当我再次运行查询时,创建了另一个游标。

我在onDestroy()方法中移动了cursor.onClose(),让应用程序从那里控制它。

谢谢, 克雷格