Android Open Source - PatternAndroid Main Activity






From Project

Back to project page PatternAndroid.

License

The source code is released under:

MIT License

If you think the Android project PatternAndroid 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.twitterclone.ui;
/* w w w.  j a  v  a2s . c o m*/
import com.foxykeep.datadroid.requestmanager.Request;
import com.foxykeep.datadroid.requestmanager.RequestManager.RequestListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.twitterclone.R;
import com.twitterclone.model.RequestFactory;
import com.twitterclone.model.RestRequestManager;
import com.twitterclone.model.operations.RestTwitter;
import com.twitterclone.model.provider.Contract.Tweets;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.content.CursorLoader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.app.AlertDialog;
import android.database.Cursor;

public class MainActivity extends FragmentActivity {
  
  final String TAG = getClass().getSimpleName();

  private PullToRefreshListView listView;
  private SimpleCursorAdapter adapter;
  private String requestString="lehinevych";
  private RestRequestManager requestManager;
  
  private static final int LOADER_ID = 1;
  private static final String[] PROJECTION = { 
    Tweets._ID,
    Tweets.USER_NAME,
    Tweets.BODY
  };
  
  private LoaderCallbacks<Cursor> loaderCallbacks = new LoaderCallbacks<Cursor>() {

    @Override
    public Loader<Cursor> onCreateLoader(int loaderId, Bundle arg1) {
      return new CursorLoader(
        MainActivity.this,
        Tweets.CONTENT_URI,
        PROJECTION,
        null,
        null,
        null
      );
    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
      adapter.swapCursor(cursor);
      if (cursor.getCount() == 0) {
        update();
      }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
      adapter.swapCursor(null);
    }
  };
  
  RequestListener requestListener = new RequestListener() {
    
    @Override
    public void onRequestFinished(Request request, Bundle resultData) {
      listView.onRefreshComplete();
    }
    
    void showError() {
      listView.onRefreshComplete();
      AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
      builder.
        setTitle(android.R.string.dialog_alert_title).
        setMessage(getString(R.string.faled_to_load_data)).
        create().
        show();
    }
    
    @Override
    public void onRequestDataError(Request request) {
      showError();
    }
    
    @Override
    public void onRequestCustomError(Request request, Bundle resultData) {
      showError();
    }
    
    @Override
    public void onRequestConnectionError(Request request, int statusCode) {
      showError();
    }
  };
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    listView = (PullToRefreshListView)findViewById(R.id.listView);
    adapter = new SimpleCursorAdapter(this,
      R.layout.tweet_view, 
      null, 
      new String[]{ Tweets.USER_NAME, Tweets.BODY },
      new int[]{ R.id.user_name_text_view, R.id.body_text_view }, 
      0);
    listView.setAdapter(adapter);
    listView.setOnRefreshListener(new OnRefreshListener<ListView>() {

      @Override
      public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        update();
      }
    });
    
    getSupportLoaderManager().initLoader(LOADER_ID, null, loaderCallbacks);
    
    requestManager = RestRequestManager.from(this);
    
    final Button button = (Button) findViewById(R.id.buttonRequest);
      final EditText edit = (EditText)findViewById(R.id.editRequest);
    button.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        requestString = edit.getText().toString();
        update();
      }
    });
  }
   
  void update() {
    listView.setRefreshing();
    Request updateRequest = new Request(RequestFactory.REQUEST_TWEETS);
    updateRequest.put("screen_name", requestString);
    Log.e("Request",requestString);
    RestTwitter.getTweets(updateRequest);
    requestManager.execute(updateRequest, requestListener);
  }

}




Java Source Code List

android.support.util.Base64.java
android.support.util.LruCache.java
com.foxykeep.datadroid.exception.ConnectionException.java
com.foxykeep.datadroid.exception.CustomRequestException.java
com.foxykeep.datadroid.exception.DataException.java
com.foxykeep.datadroid.internal.network.NetworkConnectionImpl.java
com.foxykeep.datadroid.network.NetworkConnection.java
com.foxykeep.datadroid.network.UserAgentUtils.java
com.foxykeep.datadroid.requestmanager.RequestManager.java
com.foxykeep.datadroid.requestmanager.Request.java
com.foxykeep.datadroid.service.MultiThreadedIntentService.java
com.foxykeep.datadroid.service.RequestService.java
com.foxykeep.datadroid.util.DataDroidLog.java
com.foxykeep.datadroid.util.ObjectUtils.java
com.handmark.pulltorefresh.library.ILoadingLayout.java
com.handmark.pulltorefresh.library.IPullToRefresh.java
com.handmark.pulltorefresh.library.LoadingLayoutProxy.java
com.handmark.pulltorefresh.library.OverscrollHelper.java
com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.java
com.handmark.pulltorefresh.library.PullToRefreshBase.java
com.handmark.pulltorefresh.library.PullToRefreshExpandableListView.java
com.handmark.pulltorefresh.library.PullToRefreshGridView.java
com.handmark.pulltorefresh.library.PullToRefreshHorizontalScrollView.java
com.handmark.pulltorefresh.library.PullToRefreshListView.java
com.handmark.pulltorefresh.library.PullToRefreshScrollView.java
com.handmark.pulltorefresh.library.PullToRefreshWebView.java
com.handmark.pulltorefresh.library.extras.PullToRefreshWebView2.java
com.handmark.pulltorefresh.library.extras.SoundPullEventListener.java
com.handmark.pulltorefresh.library.internal.EmptyViewMethodAccessor.java
com.handmark.pulltorefresh.library.internal.FlipLoadingLayout.java
com.handmark.pulltorefresh.library.internal.IndicatorLayout.java
com.handmark.pulltorefresh.library.internal.LoadingLayout.java
com.handmark.pulltorefresh.library.internal.RotateLoadingLayout.java
com.handmark.pulltorefresh.library.internal.Utils.java
com.handmark.pulltorefresh.library.internal.ViewCompat.java
com.twitterclone.model.RequestFactory.java
com.twitterclone.model.RestRequestManager.java
com.twitterclone.model.operations.RestTwitter.java
com.twitterclone.model.operations.TweetsOperation.java
com.twitterclone.model.provider.Contract.java
com.twitterclone.model.provider.RestProvider.java
com.twitterclone.model.service.RestService.java
com.twitterclone.ui.MainActivity.java