Advertisement
Guest User

MainActivity.java

a guest
Jan 13th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. import org.json.JSONArray;
  2. import org.json.JSONException;
  3. import org.json.JSONObject;
  4.  
  5. import com.example.helloworld.TwitterHelper.ApiException;
  6.  
  7. import android.os.AsyncTask;
  8. import android.os.Bundle;
  9. import android.app.Activity;
  10. import android.content.Context;
  11. import android.util.Log;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.view.View.OnClickListener;
  16. import android.view.ViewGroup;
  17. import android.view.inputmethod.InputMethodManager;
  18. import android.widget.BaseAdapter;
  19. import android.widget.Button;
  20. import android.widget.EditText;
  21. import android.widget.ImageButton;
  22. import android.widget.ListView;
  23. import android.widget.TextView;
  24. import android.widget.Toast;
  25.  
  26. public class MainActivity extends Activity {
  27. static final private int EXIT_ID = Menu.FIRST;
  28.  
  29. private EditText m_search_key;
  30. private ImageButton m_btn_ok;
  31. private TextView m_header;
  32. private ListView m_tweet_list;
  33. private InputMethodManager im_ctrl;
  34. private JSONArray jresult;
  35.  
  36. /** Called when the activity is first created. */
  37. @Override
  38. public void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40.  
  41. /* Use res/layout/main.xml as the view of
  42. * this Activity */
  43. setContentView(R.layout.activity_main);
  44. m_search_key = (EditText) findViewById(R.id.search_key);
  45. m_btn_ok = (ImageButton) findViewById(R.id.btn_ok);
  46. m_tweet_list = (ListView) findViewById(R.id.tweet_list);
  47. m_header = (TextView) findViewById(R.id.tweet_header);
  48. m_btn_ok.setOnClickListener(ok_handler);
  49.  
  50. im_ctrl = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  51. }
  52.  
  53. @Override
  54. public boolean onCreateOptionsMenu(Menu menu) {
  55. super.onCreateOptionsMenu(menu);
  56.  
  57. /* the context menu currently has only one option */
  58. menu.add(0, EXIT_ID, 0, R.string.exit);
  59. return true;
  60. }
  61.  
  62. @Override
  63. public boolean onOptionsItemSelected(MenuItem item) {
  64. switch (item.getItemId()) {
  65. case EXIT_ID:
  66. finish(); /* terminate the application */
  67. return true;
  68. }
  69. return super.onOptionsItemSelected(item);
  70. }
  71.  
  72. OnClickListener ok_handler = new OnClickListener() {
  73. @Override
  74. public void onClick(View v) {
  75. /* hide the soft keyboard */
  76. im_ctrl.hideSoftInputFromWindow(m_search_key.getWindowToken(), 0);
  77.  
  78. new TwitterTask().execute(m_search_key.getText().toString());
  79. /* show a brief message */
  80. Toast.makeText(getApplicationContext(),
  81. getString(R.string.hello) + " " + m_search_key.getText(),
  82. Toast.LENGTH_LONG).show();
  83. }
  84. };
  85.  
  86. /* Params (String), Progress (Integer), Result (String) */
  87. private class TwitterTask extends AsyncTask<String, Integer, String>
  88. {
  89.  
  90. @Override
  91. protected String doInBackground(String... params) {
  92. String query = params[0];
  93. if (query == null) return "";
  94. try {
  95. String result = TwitterHelper.downloadFromServer(query);
  96. return result;
  97. } catch (ApiException e) {
  98. e.printStackTrace();
  99. Log.e("HSD", "Problem making twitter search request");
  100. System.out.println("error");
  101. }
  102. return "";
  103. }
  104.  
  105. @Override
  106. protected void onPostExecute(String result) {
  107. m_tweet_list.setAdapter(new JSONAdapter(getApplicationContext()));
  108. try {
  109. JSONObject obj = new JSONObject(result);
  110. jresult = obj.getJSONArray("results");
  111. String info = obj.getString("next_page");
  112. m_header.setText(info);
  113. } catch (JSONException e) {
  114. System.out.println("error2");
  115. e.printStackTrace();
  116. }
  117. }
  118. }
  119.  
  120. /*--- ListAdapter for rendering JSON data ---*/
  121. private class JSONAdapter extends BaseAdapter
  122. {
  123. private Context mCtx;
  124.  
  125. public JSONAdapter (Context c)
  126. {
  127. mCtx = c;
  128. }
  129.  
  130. @Override
  131. public int getCount() {
  132. return jresult == null ? 0 : jresult.length();
  133. }
  134.  
  135. @Override
  136. public Object getItem(int arg0) {
  137. return null;
  138. }
  139.  
  140. @Override
  141. public long getItemId(int pos) {
  142. return pos;
  143. }
  144.  
  145. @Override
  146. public View getView(int pos, View convertView, ViewGroup parent) {
  147. TextView tv;
  148.  
  149. if (convertView == null)
  150. tv = new TextView(mCtx);
  151. else
  152. tv = (TextView) convertView;
  153. try {
  154. JSONObject obj = jresult.getJSONObject(pos);
  155. tv.setText(obj.getString("from_user") + ": " +
  156. obj.getString("text"));
  157. } catch (JSONException e) {
  158. tv.setText (e.getMessage());
  159. }
  160. return tv;
  161. }
  162.  
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement