Java tutorial
/* * Copyright 2011 Azwan Adli Abdullah * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.gh4a.fragment; import java.util.ArrayList; import java.util.List; import org.eclipse.egit.github.core.Repository; import org.eclipse.egit.github.core.RepositoryId; import org.eclipse.egit.github.core.client.GitHubClient; import org.eclipse.egit.github.core.client.PageIterator; import org.eclipse.egit.github.core.service.RepositoryService; import android.os.Bundle; import android.support.v4.app.LoaderManager; import android.support.v4.content.Loader; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import com.actionbarsherlock.app.SherlockFragment; import com.gh4a.BaseSherlockFragmentActivity; import com.gh4a.Constants; import com.gh4a.Gh4Application; import com.gh4a.R; import com.gh4a.adapter.RepositoryAdapter; import com.gh4a.loader.PageIteratorLoader; public class ForkListFragment extends BaseFragment implements LoaderManager.LoaderCallbacks<List<Repository>>, OnItemClickListener { private String mRepoOwner; private String mRepoName; private ListView mListView; private RepositoryAdapter mAdapter; private PageIterator<Repository> mDataIterator; public static ForkListFragment newInstance(String repoOwner, String repoName) { ForkListFragment f = new ForkListFragment(); Bundle args = new Bundle(); args.putString(Constants.Repository.REPO_OWNER, repoOwner); args.putString(Constants.Repository.REPO_NAME, repoName); f.setArguments(args); return f; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mRepoOwner = getArguments().getString(Constants.Repository.REPO_OWNER); mRepoName = getArguments().getString(Constants.Repository.REPO_NAME); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.generic_list, container, false); mListView = (ListView) v.findViewById(R.id.list_view); return v; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mAdapter = new RepositoryAdapter(getSherlockActivity(), new ArrayList<Repository>()); mListView.setAdapter(mAdapter); mListView.setOnItemClickListener(this); loadData(); getLoaderManager().initLoader(0, null, this); getLoaderManager().getLoader(0).forceLoad(); } private void loadData() { Gh4Application app = (Gh4Application) getSherlockActivity().getApplication(); GitHubClient client = new GitHubClient(); client.setOAuth2Token(app.getAuthToken()); RepositoryService repoService = new RepositoryService(client); mDataIterator = repoService.pageForks(new RepositoryId(mRepoOwner, mRepoName)); } private void fillData(List<Repository> repos) { mAdapter.addAll(repos); mAdapter.notifyDataSetChanged(); } @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { Gh4Application context = ((BaseSherlockFragmentActivity) getActivity()).getApplicationContext(); Repository repo = (Repository) adapterView.getAdapter().getItem(position); context.openRepositoryInfoActivity(getActivity(), repo.getOwner().getLogin(), repo.getName(), 0); } @Override public Loader<List<Repository>> onCreateLoader(int id, Bundle args) { return new PageIteratorLoader<Repository>(getSherlockActivity(), mDataIterator); } @Override public void onLoadFinished(Loader<List<Repository>> loader, List<Repository> repos) { hideLoading(); if (repos != null) { fillData(repos); } } @Override public void onLoaderReset(Loader<List<Repository>> repos) { // TODO Auto-generated method stub } }