我正在尝试从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表中添加了其他字段(firstName,lastName)等等。我只需要从ParseUser表中获取所有firstNames。如果有人指导我,这将是非常有帮助的。
答案 0 :(得分:0)
我认为你的方法有点缺陷,但可能是因为你链接的教程有点旧。我认为有更好的方法。
首先,您的ProgressDialog
可能应该替换为ProgressBar
(请参阅Progress & Activity上的Android教程)。
其次,我认为你应该使用ParseQueryAdapter
并让Parse为你处理一切。这里有一个official tutorial,但我会简要解释一下:
public class MainActivity extends ActionBarActivity {
// Declare Variables
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Parse.initialize(this, APP, SECRET);
listView = (ListView)findVIewById(R.id.listview);
UsersAdapter adapter = new UsersAdapter(this, new ParseQueryAdapter.QueryFactory<ParseUser>() {
@Override
public ParseQuery<ParseUser> create() {
ParseQuery<ParseUser> query = ParseUser.getQuery();
return query;
}
});
adapter.addOnQueryLoadListener(new ParseQueryAdapter.OnQueryLoadListener<Message>() {
@Override
public void onLoading() {
// show progress bar
}
@Override
public void onLoaded(List<ParseUser> list, Exception e) {
// close progress bar
}
});
listview.setAdapter(adapter);
}
其中UsersAdapter
定义为
public class UsersAdapter extends ParseQueryAdapter<ParseUser> {
public UsersAdapter(Context context, QueryFactory<ParseUser> queryFactory) {
super(context, queryFactory);
}
@Override
public View getItemView(final ParseUser user, View v, ViewGroup parent) {
// build your views
}
}
所以你像往常一样创建ListView
,但是你为它添加了一个特殊的适配器:(1)使用你提供的查询查询Parse你想要的东西,(2)将这些结果添加到{{ 1}} - 您需要向ListView
提供实施。
您可能希望对getItemView
中的ProgressBar
执行某些操作。
答案 1 :(得分:-1)
尝试打电话 new Runnable(){ void run(){ new RemoteDataTask()。execute(); } } .RUN();
而不是 new RemoteDataTask()。execute();