I have a task to make an Android application with a `ListView`, which must
have an add button and search system. I have watched a few tutorials (
https://www.youtube.com/watch?v=c9yC8XGaSv4 and
https://www.youtube.com/watch?v=7LgEk3Cg9Yk) and using both tutorials I
have developed a program. The program can do both things, but there is a
bug. When I first run the app, I add a few items and then I use the search
functionality. The search works just fine, but after the search has been
used once, it is no longer working.
Below is my code:
private ArrayList<String> arrayList;
private ArrayAdapter<String> adapter;
private EditText txtInput;
SearchView searchView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView)findViewById(R.id.listv);
String Items[]={};
arrayList = new ArrayList<>(Arrays.asList(Items));
adapter = new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(adapter);
txtInput = (EditText)findViewById(R.id.txtinput);
Button btAdd = (Button)findViewById(R.id.btadd);
searchView = (SearchView)findViewById(R.id.searchView1);
searchView.setOnQueryTextListener(new
SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
return false;
}
@Override
public boolean onQueryTextChange(String text) {
adapter.getFilter().filter(text);
return false;
}
});
btAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newItem = txtInput.getText().toString();
arrayList.add(newItem);
adapter.notifyDataSetChanged();
}
});
}
}
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/febb9b5f-a2ff-4876-9479-0fe74b0bd1b4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.