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 MyRepos extends Activity { private static class MyReposTask extends AsyncTask<Void, Void, Void> { public MyRepos activity; @Override protected Void doInBackground(final Void... params) { if (activity.mJson == null) { try { final Response resp = activity.mGapi.repo.list(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) { final Intent i = new Intent(getApplicationContext(), Repository.class); i.putExtra("repo_owner", mTarget); try { i.putExtra("repo_name", mJson.getJSONObject(position).getString("name")); } catch (final JSONException e) { e.printStackTrace(); } startActivity(i); return; } }; private String mPassword; private String mTarget; private MyReposTask 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(MyRepos.this, mListView); final Bundle extras = getIntent().getExtras(); if (extras != null) { mTarget = extras.getString("target"); } if ((mTarget == null) || mTarget.equals("")) { mTarget = mUsername; } mTask = (MyReposTask) getLastNonConfigurationInstance(); if ((mTask == null) || (mTask.getStatus() == AsyncTask.Status.FINISHED)) { mTask = new MyReposTask(); } 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); } }