[英]Populating a String[] Array with data from Parse.com (Android)
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: 对于以下String []数组:
private static final String[] USERS = new String[] {
"user1", "user2", "user3", "user4", "user5"
};
The following is the XML for my searchUserTextField
. 以下是我的searchUserTextField
的XML。 Just a typical AutoCompleteTextView
: 只是一个典型的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? 我想知道,如何使用从Parse中的User类检索到的数据填充USERS
数组? Basically, the use case is as follows: 基本上,用例如下:
1) User beings typing in AutoCompleteTextView
, 1)用户键入AutoCompleteTextView
,
2) suggestions pop up as a function of the available usernames in my Parse.com User class, 2)根据我的Parse.com用户类中可用的用户名弹出建议,
3) user selects the name and searches for the user. 3)用户选择名称并搜索用户。
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. 我意识到这可能不是我要完成的正确或高级实现,但是由于我是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
? 我的问题基本上可以归结为:如何根据对AutoCompleteTextView
输入,如何使用Parse中的用户列表动态填充USERS
数组?
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. 我必须this
设置为getApplicationContext()
以便ArrayAdapter正确构造我的自定义strings
数组。 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. 值得称赞的是下面的用户hack_on ,但我必须更改ArrayAdapter的上下文才能使其正常工作。
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());
}
}
});
One of the (not accepted) answers to this question: Dynamically updating an AutoCompleteTextView adapter shows how to use a filter in the adapter to update the suggestions dynamically. 此问题的一种(未接受的)答案: 动态更新AutoCompleteTextView适配器显示了如何在适配器中使用过滤器来动态更新建议。 You would have to implement the "autocomplete()" method and in your case return results from Parse. 您将必须实现“ autocomplete()”方法,并且在这种情况下需要从Parse返回结果。
EDIT : By dynamically populate, Op was asking to run a query before display of the list, not in response to user typing in the list. 编辑 :通过动态填充,Op要求在显示列表之前运行查询,而不是响应用户在列表中键入。
ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
userQuery.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> parseUsers, ParseException e) {
if (e == null) {
Log.d("score", "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_USER_REAL_NAME);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 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("score", "Error: " + e.getMessage());
}
}
});
Can't I do something like this? 我不能做这样的事情吗?
ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
userQuery.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> parseUsers, ParseException e) {
if (e == null) {
Log.d("score", "Retrieved " + parseUsers.size());
ParseUser[] data = parseUsers.toArray(new ParseUser[parseUsers.size()]);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, data);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.searchUserTextField);
if(parseUsers.size() < 40) textView.setThreshold(1);
else textView.setThreshold(2);
textView.setAdapter(adapter);
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
I'm just getting an error on the following line: 我在以下一行上遇到错误:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, data);
It says "Cannot resolve constructor 'ArrayAdapter(com.Parse.FindCallback, int, com.parse.ParseUser[])' 它说:“无法解析构造函数'ArrayAdapter(com.Parse.FindCallback,int,com.parse.ParseUser [])”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.