gpt4 book ai didi

android - 过滤 SimpleCursorAdapter

转载 作者:行者123 更新时间:2023-11-29 16:03:27 25 4
gpt4 key购买 nike

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

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


}});
}

}

最佳答案

它不起作用,因为 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

关于android - 过滤 SimpleCursorAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21780499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com