我正在制作一个应用程序,它将允许用户添加其图片和名称,然后将其显示在列表视图中。此外,我还为他们添加了搜索功能。
创建搜索过滤器时,代码运行良好,但是,当我向其中添加一个sqlite数据库时,它搞砸了。当我尝试搜索时,除了列表中所有添加的数据外,它什么都不会显示。
这是我的代码的片段:
DatabaseHelper db;
Uri imageUri;
EditText textsearch;
ListView lv;
AlertDialog.Builder builder;
ArrayList<Names> list = new ArrayList<Names>(); //this is my reference list
ArrayList<Names> displaylist = new ArrayList<Names>(); //arraylist to display all data
ArrayList<Names> anotherlist = new ArrayList<>();
ArrayList<Names> findlist = new ArrayList<>();
Adapter adapter;
private Names names = new Names();
private static final int PICK_IMAGE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textsearch = (EditText) findViewById(R.id.textsearch);
lv = (ListView) findViewById(R.id.listview);
db = new DatabaseHelper(this);
displaylist = db.getAll();
adapter = new Adapter(this, displaylist);
madapter = new Adapter(this,findlist);
final Adapter myadapter = new Adapter(this, list);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
//
builder = new AlertDialog.Builder(this);
lv.setOnItemClickListener(this);
this.textsearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
list.clear();
String s1 = s.toString();
Pattern pattern = Pattern.compile(s1);
for (int i=0; i<displaylist.size(); i++){
String find = displaylist.get(i).getName().toLowerCase();
Matcher matcher = pattern.matcher(find);
if(matcher.find()){
list.add(displaylist.get(i));
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}//end if
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}