First Parse Application

Now you know what is Parse, how to get it and you have also initialized data in the Parse cloud that is going to be used by your Android application

You have also initialized Parse in your Android application using the initialize Parse code snippet

Now building the application is simple

  • Parse requires a call to the data on the cloud and therefore as we learnt earlier, we have to make this call asynchronously – you need a AsyncTask. Look at the private class RemoteDataTask  that extends AsyncTask
  • Learn to use the query language – it is simple : https://parse.com/docs/android_guide#queries
  • the rest of the steps are the same as you would do in a typical application – you have access to the data dynamically, build the adapter and associate the adapter to the view, do all this in in onPostExecute method
  • Enable internet access on your AndroidManifest file – <uses-permission android:name=”android.permission.INTERNET” />

public class MainActivity extends Activity {

private ListView categoryListView;
private ArrayAdapter<String> categoryListAdapter;
private List<ParseObject> categories;
private ArrayList<String> categoryList = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeParse();
new RemoteDataTask().execute();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

private void initializeParse() {
Parse.initialize(this, “NDUawcm7DwXAkkSnuLMMiG6waq7l2RgOOu6YOpZB”,
“z3OdV85A5Dvr3tj49M06nZ1GdvQFa4nJLzxxW6Ct”);
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
ParseACL.setDefaultACL(defaultACL, true);
}

private class RemoteDataTask extends
android.os.AsyncTask<Void, Void, Void> {
// Override this method to do custom remote calls
protected Void doInBackground(Void… params) {
// Gets the current list of Categories in sorted order
ParseQuery query = new ParseQuery(“Categories”);
query.orderByDescending(“_created_at”);

try {
categories = query.find();
Log.i(“MainActivity:RemoteDataTask:doInBackground:Categories:”,categories.toString());
} catch (ParseException e) {
Log.e(“MainActivity:RemoteDataTask:doInBackground:Exception Retrieving Categories:”,
e.getMessage());

}
return null;
}

protected void onPostExecute(Void result) {
// Put the list of categories into the list view
for (ParseObject category : categories) {
categoryList.add((String) category.get(“Name”));
}
categoryListView = (ListView) findViewById(R.id.categoryListView);
categoryListAdapter = new ArrayAdapter<String>(MainActivity.this,
R.layout.activity_main, R.id.categoryTextView, categoryList);
categoryListView.setAdapter(categoryListAdapter);
}

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: