java - How to fetch the list of users from ParseUser table and show the it in list view in andorid -


i trying list of first names of users parseuser table crashing error: doing work on main thread. works when try fetch other parseobjects doesn't work parseuser table. following code

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 progressdialog         mprogressdialog = new progressdialog(mainactivity.this);         // set progressdialog title         mprogressdialog.settitle("loading donors");         // set progressdialog message         mprogressdialog.setmessage("loading...");         mprogressdialog.setindeterminate(false);         // show progressdialog         mprogressdialog.show();     }      // remotedatatask asynctask     @override     protected void doinbackground(void... params) {          // locate 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 results arrayadapter         adapter = new arrayadapter<string>(mainactivity.this,                 r.layout.listview_item);         // retrieve object "name" parse.com database         (parseobject user : ob) {             adapter.add((string) user.get("firstname"));         }         // binds adapter listview         listview.setadapter(adapter);          // close 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> 

i have added additional fields in parseuser table (firstname, lastname) , more.. need firstnames parseuser table. helpful if guide me on this.

i think approach bit flawed, because tutorial linked bit old. think there's better approach.

first of all, progressdialog should replaced progressbar (see android tutorial on progress & activity).

secondly, think should using parsequeryadapter , let parse handle you. there's official tutorial on this, explain briefly:

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);     } 

where usersadapter defined as

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 views     } } 

so create listview usual, add special adapter (1) queries parse stuff want using query provide , (2) adds these results listview - need provide implementation getitemview.

you may want progressbar in onqueryloadlistener.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -