Dynamic AutocompleteTextView with ArrayAdapter and TextWatcher - android

I'm trying to update the list of an AutocompleteTextView dynamically using and ArrayAdapter. In order to update this View I use a TextWatcher to monitor any changes that may occur in the AutocompleteTextView.
The problem is that the list isn't updating at all and I can't understand why. I've been looking for something like that on internet and I've found couple of different approaches but still I can't understand why this one, that should be the simplest one, isn't working. Any explaination would be much appreciated.
Target AVD: Google APIs level 10, Android 2.3.3
Here is the simplified code:
public class AutocompleteActivity extends Activity implements TextWatcher {
ArrayAdapter<String> adapter = null;
AutoCompleteTextView acTextView = null;
ArrayList<String> addresses = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocompleteview);
acTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete_address);
adapter = new ArrayAdapter<String>(this, R.layout.listitem, addresses);
adapter.setNotifyOnChange(true);
acTextView.addTextChangedListener(this);
acTextView.setAdapter(adapter);
}
#Override
public void onTextChanged(CharSequence text, int start, int before, int after) {
try {
adapter.add("test");
}
catch (Exception e) {
e.printStackTrace();
}
}

In your source code, TextWatcher is supplied to the AutoCompleteTextView, this is the real problem.
If you look at AutoCompleteTextView source code, you will find AutoCompleteTextView has its
own TextWatcher, named "MyWatcher". Becauseof this, AutoCompleteTextView can not repond to your typing action.

Related

Hiding Listview until search is initiated

I have a question:
I have a listview (from custom adapter) with a searchbar on top of it. Everything works ok, the only thing I need is when opening the activity to keep the listview hidden, or invisible, until the search has started in the searchbar (in other words, to keep the listview hidden until I start typing something in the searchbar).
You can find the code here:
How to start new intent after search-filtering listview?
Plus a suggested solution, which unfortunately did not work, you can see here:
Android search list invisible by default? Can it be done?
I am looking forward to your replies guys and I thank you in advance.
Try adding TextWatcher to your search EditText. This will detect when you type something in your search:
EditText search= (EditText) findViewById(R.id.search);
search.addTextChangedListener(filterTextWatcher);
TextWatcher method:
private TextWatcher filterTextWatcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
//Change the visibility here
list.setVisibility(View.VISIBLE);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
};
I think you may be thinking about an AutoCompleteTextView? It requires that you load the list data into its adapter then set the adapter.
// Get a reference to the AutoCompleteTextView in the layout
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
// Get the string array
String[] countries = getResources().getStringArray(R.array.countries_array);
// Create the adapter and set it to the AutoCompleteTextView
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
textView.setAdapter(adapter);
Google's Guide: here.

Populating List View of Objects dynamically in Android

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)
CODE
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.
PROBLEM
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

Android AutoCompleteTextView with List<String>

I have an AutoCompleteTextView where the user types an address. I want to be able to show suggestions below as he types. To do that I get a list of possible addresses via the Reverse Geocoding API. i want then to show this list of strings (possible addresses) to the user. Just like the google maps app does.
I have added a TextChangedListener to the AutoCompleteTextView. On the onTextChanged() event an AsyncTask is executed where the list of possible address gets updated on the onPostExecute().
autoText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
new GeoTask().execute();
}
});
Attempt 1
This is the list:
static List<String> suggestionList = new ArrayList<String>();
And this is the code for the adapter for the AutoCompleteTextView :
autoText.setThreshold(3);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, suggestionList);
autoText.setAdapter(adapter);
adapter.setNotifyOnChange(true);
With the above code nothing shows up.
Attempt 2
I also tried using an array as an argument for the adapter and every time the list of address gets updated I convert it to the array.
static String[] suggestions;
static List<String> suggestionList = new ArrayList<String>();
Adapter:
autoText.setThreshold(3);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, suggestionList);
autoText.setAdapter(adapter);
adapter.setNotifyOnChange(true);
AsyncTask:
protected void onPostExecute(Void aVoid) {
...
suggestions = suggestionList.toArray(new String[suggestionList.size()]);
super.onPostExecute(aVoid);
}
How can i make it work? Is there a way to use the adapter with the list?
The reason why nothing shows up is because the list is empty and in onPostExecute of your AsyncTask your only assigning new array to your suggestions reference, what you should really do is use adapters methods to add and remove elements. You can try this code:
adapter.clear();
adapter.addAll(/*new collection of suggestions*/);
in your onPostExecute method.
NOTE: adapter.addAll() method appeared only from 11th API, so if you use lower, then you would have to add each item manually.

Android list -- how to find the wanted item?

I do have a list of possibly several thousands items (just testing with shorter list with about 200 items). The information is stored in SQLite, the ContentProvider, loader and SimpleCursorAdapter is used. The list is sorted lexicographically, and the android:fastScrollEnabled is used. The list scrolls smoothly -- no problem when one knows the exact name of the item.
Occasionally, I would like to find the items that contain some substring somewhere in the middle of their name. The `... LIKE "%wanted%" is a solution for me. However, I would like to give the user an incremental filtering -- i.e. the list content be updated during typing the substring. The reasoning is that it may not be neccessary to type many characters, and one should find the item as soon as possible. The goal is not to find one or none item. The goal is to filter the list so that manual scrolling be acceptable to overview the candidate items and select one of them by touch.
I have met the SearchView widget that looks nicely at the action bar. Anyway, reading something more about it in the doc, I am not sure if it is the right tool for me. Or if the recommended implementation is the one for me. (I am an android beginner, I am even not sure if I understand it well.)
Is it possible to use the widget for incremental filtering of the list in the same activity that has the SearchView in the Action Bar? Can you point me to some code that possibly shows how to implement the wanted behaviour?
Sample code to try out :
public class AndroidListViewFilterActivity extends Activity {
ArrayAdapter<String> dataAdapter = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Generate list View from ArrayList
displayListView();
}
private void displayListView() {
//Array list of countries
List<String> countryList = new ArrayList<String>();
countryList.add("Aruba");
countryList.add("Anguilla");
countryList.add("Netherlands Antilles");
countryList.add("Antigua and Barbuda");
countryList.add("Bahamas");
countryList.add("Belize");
countryList.add("Bermuda");
countryList.add("Barbados");
countryList.add("Canada");
countryList.add("Costa Rica");
countryList.add("Cuba");
countryList.add("Cayman Islands");
countryList.add("Dominica");
countryList.add("Dominican Republic");
countryList.add("Guadeloupe");
countryList.add("Grenada");
//create an ArrayAdaptar from the String Array
dataAdapter = new ArrayAdapter<String>(this,R.layout.country_list, countryList);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
//enables filtering for the contents of the given ListView
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
EditText myFilter = (EditText) findViewById(R.id.myFilter);
myFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
dataAdapter.getFilter().filter(s.toString());
}
});
}
}

How to filter text in a ListView properly?

I've tried to filter my ListView from my EditText box, but currently, it's not working.
I have a ListView that get data properly and they are added to my own ListView using a ArrayAdapter class and my own ArrayList class. When I for example, typing in "table" I want it to sort from the loaded titles in my ListView and than it should show me the items that are left and matching table.
My current code:
private ArrayList<Order> orders; /* ArrayList class with my own Order class to
define title, info etc. */
private OrderAdapter adapter; //Own class that extends ArrayAdapter
orders = new ArrayList<Order>();
adapter = new OrderAdapter(this, R.layout.listrow, orders); /* listrow defining a
single item in my ListView */
setListAdapter(adapter); //Set our adapter to a ListView
search.addTextChangedListener(filterTextWatcher); //search is my EditText
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s); //Filter from my adapter
adapter.notifyDataSetChanged(); //Update my view
}
};
Okay, so how can I achieve this? When I entering some value into the EditText box everything disappear, even if there is a match.
Thanks in advance and tell me if you need more code snippets or if this question i unclear!
The built-in filter implemented by ArrayAdapter converts the contained object to string by calling toString() method. It is this string that will be used to perform the matching. If you are only trying to match one string field in your Order object, you can override the toString() method for your Order class to return that field. If you want to perform more flexible matching, such as matching multiple fields, check out this post which shows how to create a custom filter:
Custom filtering in Android using ArrayAdapter

Categories

Resources