I set up the following array adapter:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, USERS);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.searchUserTextField);
textView.setAdapter(adapter);
For the following String[] array:
private static final String[] USERS = new String[] {
"user1", "user2", "user3", "user4", "user5"
};
The following is the XML for my searchUserTextField
. Just a typical AutoCompleteTextView
:
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="65dp"
android:background="@drawable/edittext_border"
android:layout_gravity="top"
android:textColor="#000000"
android:hint="@string/search_user"
android:id="@+id/searchUserTextField"
android:ems="150"
android:paddingLeft="25dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:maxLines ="4"
android:maxLength ="150"
android:scrollHorizontally="false"
android:typeface="sans"
android:textSize="14sp"
android:fontFamily="sans-serif-light"
android:capitalize="none"
android:inputType="textEmailAddress"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/submitSearch"
android:layout_toStartOf="@+id/submitSearch" />
So far, a very simple implementation.
I was wondering, how can I populate my USERS
array with data retrieved from my User class in Parse? Basically, the use case is as follows:
1) User beings typing in AutoCompleteTextView
,
2) suggestions pop up as a function of the available usernames in my Parse.com User class,
3) user selects the name and searches for the user.
I realize this probably isn't the correct or advanced implementation for what I'm trying to accomplish, but I just want to work on this in baby steps as I'm new to Android.
My question basically boils down to, how can I dynamically populate the USERS
array with a list of Users from Parse based on input to the AutoCompleteTextView
?
Update for Answer: I used the following in my application. I had to set this
to getApplicationContext()
in order for the ArrayAdapter to properly construct my custom strings
array. Credit for answer goes to user hack_on below, but I had to change the context of my ArrayAdapter to get it working for my purposes.
ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
userQuery.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> parseUsers, ParseException e) {
if (e == null) {
Log.d("users", "Retrieved " + parseUsers.size());
ParseUser[] data = parseUsers.toArray(new ParseUser[parseUsers.size()]);
String[] strings = new String[data.length];
for (int i = 0; i < data.length; i++) {
strings[i] = data[i].getString(ParseConstants.KEY_USERNAME);
}
// Test to see if it was correctly printing out the array I wanted.
// System.out.println(Arrays.toString(strings));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, strings);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.searchUserTextField);
if(parseUsers.size() < 40) textView.setThreshold(1);
else textView.setThreshold(2);
textView.setAdapter(adapter);
} else {
Log.d("users", "Error: " + e.getMessage());
}
}
});