I am having an issue with a "SimpleCursorAdapter" that is supposed to show a row for all results in the database. I also have an "editText" field that is supposed to be used for filtering the results set.
The issue is that the default view of the listView is to show no values and an empty table. Once I start typing in the EditText then it will filter just fine.
Is it possible to have the "default" view (when nothing is entered in the EditText) to show all values? How would I do that?
Thank you in advance and below is my code:
private void setupListView(ListView bandListView) {
ListView listView = (ListView) bandListView.findViewById(R.id.BandSelectorListView);
//Get my SQLite Helper Class
final LiveMusicSqlLiteHelper lmslh = new LiveMusicSqlLiteHelper(context);
Cursor cursor = lmslh.getBandInfo();
// The desired columns to be bound
String[] columns = new String[] {
LiveMusicArchiveConstants.bandAttributeName
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.bandNameTextBoxId,
};
@SuppressWarnings("deprecation")
final SimpleCursorAdapter bandListAdapter = new SimpleCursorAdapter(context, R.layout.band_item_layout, cursor, columns,to);
// Assign adapter to ListView
listView.setAdapter(bandListAdapter);
//Set the click item handler
listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(context,
"Item in position " + position + " clicked",
Toast.LENGTH_LONG).show();
}});
bandListAdapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
String partialValue = constraint.toString();
return lmslh.getAllSuggestedValues(partialValue);
}
});
//Create the TextWatcher. This is used to monitor the edit text so that it can filter as typing
TextWatcher filterTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
public void onTextChanged(CharSequence s, int start, int before,int count) {
bandListAdapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
//Set the filter
EditText filterEditText = (EditText)fragmentView.findViewById(R.id.bandFilterEditText);
filterEditText.setVisibility(View.VISIBLE);
filterEditText.addTextChangedListener(filterTextWatcher);
//Close the cursor
cursor.close();
}
Ok, so yeah kind of stooopid....
I got a bit too zealous on my cursor.close() at the end of the method. This meant that the first load was actually empty. When I ran the query again then another cursor was created.
I moved the cursor.onClose() in the onDestroy() method and let the app control it from there.
Thanks, Craig