Android Open Source - android-loader Main Activity






From Project

Back to project page android-loader.

License

The source code is released under:

MIT License

If you think the Android project android-loader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.myloader;
/* w  w  w  .  j a  va  2 s.  c o m*/
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


import android.R.integer;
import android.app.Activity;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.AsyncTaskLoader;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Debug;
import android.provider.ContactsContract.Contacts;
import android.text.StaticLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;


// ?????????? (?????????????????????????????)
class BigData{
  static int msDataIndex = 0;
  
  int mDataIndex = 0;
  String mDataName = "";
  ArrayList<String> mList = new ArrayList<String>();
  
  // ?????????????????????????????????
  public BigData(){
    String table = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    mDataIndex = msDataIndex++;
    mDataName = String.valueOf(table.charAt(mDataIndex % table.length()));
    Log.d("test", "...BigData" + mDataName);
  }
  
  // ???????????????????????????????????????
  public void load(){
    Log.d("test", "...load" + mDataName);
    try{
      Random r = new Random();
      for(int i = 0; i < 3000; i++){
        if(i % 100 == 0){
          Log.d("test", "...loading" + mDataName + ":" + i);
        }
        int n = r.nextInt(10000);
        mList.add("item" + mDataName + "_" + n);
        Thread.sleep(1);
      }
    }
    catch(InterruptedException ex){
      Log.d("test", "======InterruptedException" + mDataName);
    }
  }
  
  // ????
  String at(int index){
    return mList.get(index)  ;
  }
}

// ????
class BigDataLoader extends AsyncTaskLoader<BigData>{
  public BigDataLoader(Context context) {
    super(context);
    Log.d("test", "...BigDataLoader.");
  }

  @Override
  public BigData loadInBackground() {
    // BigData??
    Log.d("test", "...loadInBackground.");
    BigData data = new BigData();
    data.load();
    return data;
  }
}

// Loader ????????? Activity ????????? Fragment ??????
// LoaderCallbacks ? implements ???????????????
public class MainActivity extends Activity implements LoaderCallbacks<BigData> {
  // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- //
  // LoaderCallbacks???????3???
  // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- //
  @Override
  public Loader<BigData> onCreateLoader(int id, Bundle args) {
    // Loader???
    // ?Loader???Activity????????????????????????????????????????????
    //   Activity???????getApplication()?context??????????????
    //   ????????????
    Log.d("test", "------------onCreateLoader");
    BigDataLoader loader = new BigDataLoader(this.getApplication());
    loader.forceLoad();
    return loader;
  }
  @Override
  public void onLoadFinished(Loader<BigData> loader, BigData data) {
    // ????????????
    Log.d("test", "------------onLoadFinished");
    TextView textView = (TextView)findViewById(R.id.textView1);
    textView.setText(data.at(0));
    // ??????????
    ProgressBar progress = (ProgressBar)findViewById(R.id.progressBar1);
    progress.setVisibility(View.GONE);
    
  }
  @Override
  public void onLoaderReset(Loader<BigData> loader) {
    // Loader??????????????????????????????????????????????????????
    Log.d("test", "------------onLoaderReset");
  }
    
  // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- //
  // ????????
  // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- //
    public void buttonMethodRestart(View button){
      // ???????
    ProgressBar progress = (ProgressBar)findViewById(R.id.progressBar1);
    progress.setVisibility(View.VISIBLE);
      // ???????
      getLoaderManager().restartLoader(0, null, this);
    }

  // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- //
  // Activity
  // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- //
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    // ?????????LoaderManager?????
    getLoaderManager().initLoader(0, null, this);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }

}




Java Source Code List

com.example.myloader.MainActivity.java