【问题标题】:Filtering SimpleCursorAdapter过滤 SimpleCursorAdapter
【发布时间】:2014-02-14 13:18:16
【问题描述】:

此代码通过显示数据库中的所有数据正常工作,但我在过滤时遇到问题。我尝试了很多代码但没有任何效果,有人可以帮助我吗?谢谢

 package com.example.dictionary;


    import java.util.List;
    import com.example.dictionary.R;
    import android.os.Bundle;
    import android.app.Activity;
    import android.database.Cursor;
    import android.support.v4.widget.SimpleCursorAdapter;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;


    public class MainActivity extends Activity{
        Cursor cursor;
        ListView listView;
        SimpleCursorAdapter adapter;
        Button back, clear;
        List<String> items;
        String get;
        EditText et;
        int textlength = 0;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText) findViewById(R.id.editText1);

打开我的数据库并获取所有数据

        Historydb db = new Historydb(this);
        db.open();        
        cursor = db.getword();
        startManagingCursor(cursor);
        cursor.moveToFirst();
        adapter = new SimpleCursorAdapter(this,
                    android.R.layout.simple_list_item_1, cursor,
                    new String[] { "word" }, new int[] { android.R.id.text1 });

       listView = (ListView) findViewById(R.id.listView1);
       listView.setAdapter(adapter);

我在编辑文本中设置了 addtextchangelistener。

        et.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {

        // Abstract Method of TextWatcher Interface.
        }

         public void beforeTextChanged(CharSequence s, int start, int count,int after) {
        // Abstract Method of TextWatcher Interface.

         }
          public void onTextChanged(CharSequence s, int start, int before,int count) {


          adapter.getFilter().filter(s);


           }});
   }

}

【问题讨论】:

    标签: android


    【解决方案1】:

    它不起作用,因为adapter.getfilter().filter(cs); 不能直接用于 SimpleCursorAdapter。 对于SimpleCursorAdapter,您必须先使用adapter.setFilterQueryProvider

    这里是完整的描述:ListView, SimpleCursorAdapter, an an EditText filter -- why won't it do anything?

    还有这个:Using an EditText to filter a SimpleCursorAdapter-backed ListView

    【讨论】:

    【解决方案2】:

    试试这个....

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
    // When user changed the Text
    ((SimpleAdapter) YourActivity.this.adapter).getFilter().filter(cs);  
    }
    

    【讨论】:

    • public void onTextChanged(CharSequence s, int start, int before,int count) { ((SimpleCursorAdapter)MainActivity.this.adapter).getFilter().filter(s); } 这里是阿尼尔先生
    • 你从数据库获取数据??
    • 我已经获得了 db 中的所有数据并在 listview 中显示所有数据,我的问题是过滤数据先生。
    • 不工作先生 ListAdapter 适配器;适配器 = 新 SimpleCursorAdapter(this,android.R.layout.simple_list_item_1, cursor,new String[] { "word" }, new int[] { android.R.id.text1 }); public void onTextChanged(CharSequence s, int start, int before,int count) { ((SimpleCursorAdapter)MainActivity.this.adapter).getFilter().filter(s); } });
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    相关资源
    最近更新 更多