Back to project page PatternAndroid.
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.
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); } }