Java tutorial
/** * Hubroid - A GitHub app for Android * * Copyright (c) 2011 Eddie Ringle. * * Licensed under the New BSD License. */ package net.idlesoft.android.apps.github.activities.tabs; import net.idlesoft.android.apps.github.R; import net.idlesoft.android.apps.github.activities.Hubroid; import net.idlesoft.android.apps.github.activities.Repository; import net.idlesoft.android.apps.github.adapters.RepositoriesListAdapter; import org.idlesoft.libraries.ghapi.GitHubAPI; import org.idlesoft.libraries.ghapi.APIAbstract.Response; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.AdapterView.OnItemClickListener; public class WatchedRepos extends Activity { private static class WatchedReposTask extends AsyncTask<Void, Void, Void> { public WatchedRepos activity; @Override protected Void doInBackground(final Void... params) { if (activity.mJson == null) { try { final Response resp = activity.mGapi.user.watching(activity.mTarget); if (resp.statusCode != 200) { /* Oh noez, something went wrong */ return null; } activity.mJson = (new JSONObject(resp.resp)).getJSONArray("repositories"); } catch (final JSONException e) { e.printStackTrace(); } } activity.mAdapter.loadData(activity.mJson); return null; } @Override protected void onPostExecute(final Void result) { activity.mAdapter.pushData(); activity.mAdapter.setIsLoadingData(false); } @Override protected void onPreExecute() { activity.mAdapter.setIsLoadingData(true); } } private RepositoriesListAdapter mAdapter; private final GitHubAPI mGapi = new GitHubAPI(); private JSONArray mJson; private ListView mListView; private final OnItemClickListener mOnListItemClick = new OnItemClickListener() { public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) { try { final Intent i = new Intent(getApplicationContext(), Repository.class); i.putExtra("repo_owner", mJson.getJSONObject(position).getString("owner")); i.putExtra("repo_name", mJson.getJSONObject(position).getString("name")); startActivity(i); } catch (final JSONException e) { e.printStackTrace(); // TODO: If there's an error, notify the user rather than doing // nothing. } return; } }; private String mPassword; private String mTarget; private WatchedReposTask mTask; private String mUsername; @Override public void onCreate(final Bundle icicle) { super.onCreate(icicle); final SharedPreferences prefs = getSharedPreferences(Hubroid.PREFS_NAME, 0); mUsername = prefs.getString("username", ""); mPassword = prefs.getString("password", ""); mGapi.authenticate(mUsername, mPassword); mListView = (ListView) getLayoutInflater().inflate(R.layout.tab_listview, null); mListView.setOnItemClickListener(mOnListItemClick); setContentView(mListView); mAdapter = new RepositoriesListAdapter(WatchedRepos.this, mListView); final Bundle extras = getIntent().getExtras(); if (extras != null) { mTarget = extras.getString("target"); } if ((mTarget == null) || mTarget.equals("")) { mTarget = mUsername; } mTask = (WatchedReposTask) getLastNonConfigurationInstance(); if ((mTask == null) || (mTask.getStatus() == AsyncTask.Status.FINISHED)) { mTask = new WatchedReposTask(); } mTask.activity = this; if (mTask.getStatus() == AsyncTask.Status.PENDING) { mTask.execute(); } } @Override public void onRestoreInstanceState(final Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); try { if (savedInstanceState.containsKey("json")) { mJson = new JSONArray(savedInstanceState.getString("json")); } else { return; } } catch (final Exception e) { e.printStackTrace(); return; } if (mJson != null) { mAdapter.loadData(mJson); mAdapter.pushData(); } } @Override public void onResume() { super.onResume(); mListView.setAdapter(mAdapter); } @Override public Object onRetainNonConfigurationInstance() { return mTask; } @Override public void onSaveInstanceState(final Bundle savedInstanceState) { if (mJson != null) { savedInstanceState.putString("json", mJson.toString()); } super.onSaveInstanceState(savedInstanceState); } }