I've looked up a few tutorials online but decided the following was the best approach.
My Activity only consists of a text field (editText) and a list view (roomlv)
The objects contained in the list are RoomCell objects
My Class has the following variables
public class RoomsActivity extends ActionBarActivity {
ListView listview2;
RoomCell[] roomcells = {rc1, rc2, rc3, rc4, rc5, rc6, rc7, rc8, rc9};
//where rc1, rc2, ... are predefined objects
RoomCell[] finalcells = roomcells;
RoomListAdapter rla;
My onCreate method looks like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rooms);
listview2 = (ListView) findViewById(R.id.roomlv);
listview2.setClickable(true);
listview2.setOnItemClickListener(onListClick2);
EditText filterText = (EditText) findViewById(R.id.editText);
filterText.addTextChangedListener(filterTextWatcher);
rla = new RoomListAdapter(this, finalcells);
listview2.setAdapter(rla);
}
where my TextWatcher works like this:
private TextWatcher filterTextWatcher = new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(s.length()==0){
finalcells = roomcells;
rla.notifyDataSetChanged();
}else{
finalcells = filterList(roomcells, s);
rla.notifyDataSetChanged();
}
}
};
where the other 2 override methods are omitted for convenience here.
The filterList() method returns the filtered array (this, I believe is working fine but I will paste the code if asked for)
And finally the OnClick code is here:
private AdapterView.OnItemClickListener onListClick2 = new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
GlobVar.roomtitle = finalcells[position].name;
GlobVar.roomsize = finalcells[position].size;
Intent i = new Intent(RoomsActivity.this, TheRoomActivity.class);
startActivity(i);
}
};
where 2 global variables are assigned specific values to the clicked cells.
Editing the text field makes no difference whatsoever, and clicking on a cell crashes my program?
I've looked over my code countless times and cannot locate any mistakes.
Thanks for any help in advance.
You need to call:
rla = new RoomListAdapter(RoomsActivity.this, finalcells);
listview2.setAdapter(rla);
before calling:
rla.notifyDataSetChanged();
inside your onTextChanged method as it will have new filtered objects each time you type into your EditText
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With