samedi 25 avril 2015

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


I am trying to get the list of first names of all the users from ParseUser table but its crashing with the error: doing much work on main thread. It works when I try fetch from other ParseObjects but it doesn't work with ParseUser table. The following is my 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, "6qUFzAHfl9bXRzzDlBegiXZx0Tw5dc29m3jXmnHt","TS6OswWh6HwKQm2uCJxqfprlyrP2mfpkmwkx3Vg9");
       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://ift.tt/nIICcg"
     xmlns:tools="http://ift.tt/LrGmb4"
     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://ift.tt/nIICcg"
  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) and some more.. I just need to get all the firstNames from ParseUser Table. it would be really helpful if someone guide me on this.


Aucun commentaire:

Enregistrer un commentaire