【发布时间】:2015-07-04 09:52:04
【问题描述】:
我试图从 ParseUser 表中获取所有用户的名字列表,但它因错误而崩溃:在主线程上做很多工作。当我尝试从其他 ParseObjects 获取时它有效,但它不适用于 ParseUser 表。以下是我的代码
MainActivity.java
public class MainActivity extends ActionBarActivity {
// Declare Variables
ListView listview;
List<ParseUser> ob;
ProgressDialog mProgressDialog;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Parse.initialize(this, APP, SECRET);
new RemoteDataTask().execute();
}
// RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("loading all donors");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
// RemoteDataTask AsyncTask
@Override
protected Void doInBackground(Void... params) {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseUser> query = ParseUser.getQuery();
try {
ob = query.find();
} catch (com.parse.ParseException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
listview = (ListView) findViewById(R.id.listview);
// Pass the results into an ArrayAdapter
adapter = new ArrayAdapter<String>(MainActivity.this,
R.layout.listview_item);
// Retrieve object "name" from Parse.com database
for (ParseObject User : ob) {
adapter.add((String) User.get("firstName"));
}
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
// Capture button clicks on ListView items
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#de99ac"
tools:context="com.nyu.blife_app.MainActivity">
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5sp"
android:textSize="25sp" >
</TextView>
我在 ParseUser 表中添加了其他字段(名字、姓氏)等等。我只需要从 ParseUser 表中获取所有名字。如果有人指导我这将非常有帮助。
【问题讨论】:
-
你为什么使用
AsyncTask?解析对象和查询具有许多操作的异步方法。 -
例如
query.findInBackground(FindCallback)。 -
我已经按照这个教程androidbegin.com/tutorial/…
-
我觉得有点老了,不太好。你最好使用 Parse 中的内置异步方法和专门为
ListViews 编写的适配器。
标签: java android xml android-studio