From 49d509eb2f953d03497301eb7a3dbddddf5468ff Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 5 Dec 2011 02:23:48 +0100 Subject: [PATCH] Added loading dialog, using AsyncTask --- LeWebConnect2011/AndroidManifest.xml | 70 +++--- LeWebConnect2011/res/drawable/customshape.xml | 16 +- .../res/drawable/customshape2.xml | 16 +- LeWebConnect2011/res/layout/loading.xml | 16 ++ LeWebConnect2011/res/layout/main.xml | 1 + LeWebConnect2011/res/layout/thinkit.xml | 50 ++--- .../thinkit/lewebconnect/LeWebConnect.java | 1 + .../thinkit/lewebconnect/Perticipents.java | 114 ++++++---- .../src/com/thinkit/lewebconnect/ThinkIT.java | 110 +++++----- .../src/com/thinkit/lewebconnect/Tweet.java | 204 +++++++++--------- .../thinkit/lewebconnect/TweetsAdapter.java | 102 ++++----- 11 files changed, 373 insertions(+), 327 deletions(-) create mode 100644 LeWebConnect2011/res/layout/loading.xml diff --git a/LeWebConnect2011/AndroidManifest.xml b/LeWebConnect2011/AndroidManifest.xml index 4969fc1..3fd1e59 100644 --- a/LeWebConnect2011/AndroidManifest.xml +++ b/LeWebConnect2011/AndroidManifest.xml @@ -1,37 +1,37 @@ - - - - - - - - - - - - - - - - - + + + + + + + + + + + + - - - - + + + + + + + + + \ No newline at end of file diff --git a/LeWebConnect2011/res/drawable/customshape.xml b/LeWebConnect2011/res/drawable/customshape.xml index e8d6d2f..8dc8e9a 100644 --- a/LeWebConnect2011/res/drawable/customshape.xml +++ b/LeWebConnect2011/res/drawable/customshape.xml @@ -1,9 +1,9 @@ - - - - - + + + + + \ No newline at end of file diff --git a/LeWebConnect2011/res/drawable/customshape2.xml b/LeWebConnect2011/res/drawable/customshape2.xml index da34220..074306e 100644 --- a/LeWebConnect2011/res/drawable/customshape2.xml +++ b/LeWebConnect2011/res/drawable/customshape2.xml @@ -1,9 +1,9 @@ - - - - - + + + + + \ No newline at end of file diff --git a/LeWebConnect2011/res/layout/loading.xml b/LeWebConnect2011/res/layout/loading.xml new file mode 100644 index 0000000..7e5b2bd --- /dev/null +++ b/LeWebConnect2011/res/layout/loading.xml @@ -0,0 +1,16 @@ + + + + + + + + + \ No newline at end of file diff --git a/LeWebConnect2011/res/layout/main.xml b/LeWebConnect2011/res/layout/main.xml index 1933b26..642d25d 100644 --- a/LeWebConnect2011/res/layout/main.xml +++ b/LeWebConnect2011/res/layout/main.xml @@ -17,6 +17,7 @@ android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp"> + \ No newline at end of file diff --git a/LeWebConnect2011/res/layout/thinkit.xml b/LeWebConnect2011/res/layout/thinkit.xml index 75b909a..1a5545d 100644 --- a/LeWebConnect2011/res/layout/thinkit.xml +++ b/LeWebConnect2011/res/layout/thinkit.xml @@ -1,28 +1,28 @@ - - + - - - - + + - - - + + + \ No newline at end of file diff --git a/LeWebConnect2011/src/com/thinkit/lewebconnect/LeWebConnect.java b/LeWebConnect2011/src/com/thinkit/lewebconnect/LeWebConnect.java index ed66433..05f6243 100644 --- a/LeWebConnect2011/src/com/thinkit/lewebconnect/LeWebConnect.java +++ b/LeWebConnect2011/src/com/thinkit/lewebconnect/LeWebConnect.java @@ -5,6 +5,7 @@ import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.util.Log; +import android.view.Window; import android.widget.TabHost; import android.widget.TextView; diff --git a/LeWebConnect2011/src/com/thinkit/lewebconnect/Perticipents.java b/LeWebConnect2011/src/com/thinkit/lewebconnect/Perticipents.java index 61b8026..102a96a 100644 --- a/LeWebConnect2011/src/com/thinkit/lewebconnect/Perticipents.java +++ b/LeWebConnect2011/src/com/thinkit/lewebconnect/Perticipents.java @@ -14,9 +14,11 @@ import com.thinkit.lewebconnect.Attendee.LeWebByCountryComparator; import com.thinkit.lewebconnect.Attendee.LeWebByLnameComparator; import android.app.ListActivity; +import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.net.Uri; +import android.os.AsyncTask; import android.os.Bundle; import android.sax.Element; import android.text.Editable; @@ -35,6 +37,7 @@ import android.widget.Button; import android.widget.EditText; import android.widget.Filterable; import android.widget.ListView; +import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.AdapterContextMenuInfo; @@ -52,62 +55,34 @@ public class Perticipents extends ListActivity { private ArrayList users; private EditText filterText = null; ArrayAdapter adapter = null; + private ListView lv; + private Context mContext; + private ProgressDialog mDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + - - String xml; - xml = XMLfunctions.getXML(); - Document doc = XMLfunctions.XMLfromString(xml); - int numResults = XMLfunctions.numResults(doc); - //Log.d(TAG, "numResults length: " + String.valueOf(numResults)); - - if((numResults <= 0)) { - Toast.makeText(this, "Please make sure connection is available !", Toast.LENGTH_LONG).show(); - finish(); - } - - - NodeList nodes = doc.getElementsByTagName("user"); - try { - users = new ArrayList(); - //fill in the list items from the XML document - for (int i = 0; i < nodes.getLength(); i++) { - Node e = (Node)nodes.item(i); - users.add(NodeToAttendee(e)); - } - Collections.sort(users, new LeWebByLnameComparator()); - - - setContentView(R.layout.perticipents); + mContext = this; + mDialog = new ProgressDialog(mContext); + setContentView(R.layout.perticipents); + + new getXmlTask().execute(); filterText= (EditText) findViewById(R.building_list.search_box); filterText.addTextChangedListener(filterTextWatcher); - - ListView lv = getListView(); + lv = getListView(); lv.setFastScrollEnabled(true); lv.setTextFilterEnabled(false); registerForContextMenu(lv); - - adapter = new LeWebAdapter(this, users, true, false, false); - - - setListAdapter(adapter); - registerForContextMenu(getListView()); - - - - - - } catch (Exception e) { - // TODO: handle exception - e.printStackTrace(); - } + + } + + private TextWatcher filterTextWatcher = new TextWatcher() { public void afterTextChanged(Editable s) { } @@ -326,4 +301,57 @@ public class Perticipents extends ListActivity { return user; } - } \ No newline at end of file + + private class getXmlTask extends AsyncTask> { + + @Override + protected void onPreExecute(){ + mDialog.setMessage("Loading data"); + mDialog.show(); + + } + + @Override + protected ArrayList doInBackground(Void... params) { + // TODO Auto-generated method stub + String xml; + xml = XMLfunctions.getXML(); + Document doc = XMLfunctions.XMLfromString(xml); + int numResults = XMLfunctions.numResults(doc); + //Log.d(TAG, "numResults length: " + String.valueOf(numResults)); + + if((numResults <= 0)) { +// Toast.makeText(this, "Please make sure connection is available !", Toast.LENGTH_LONG).show(); +// finish(); + return null; + } + + + NodeList nodes = doc.getElementsByTagName("user"); + try { + users = new ArrayList(); + //fill in the list items from the XML document + for (int i = 0; i < nodes.getLength(); i++) { + Node e = (Node)nodes.item(i); + users.add(NodeToAttendee(e)); + } + Collections.sort(users, new LeWebByLnameComparator()); + + } catch (Exception e) { + // TODO: handle exception + e.printStackTrace(); + } + return users; + } + + @Override + protected void onPostExecute(final ArrayList users){ + adapter = new LeWebAdapter(mContext, users, true, false, false); + + mDialog.hide(); + setListAdapter(adapter); + registerForContextMenu(getListView()); + + } + } +} \ No newline at end of file diff --git a/LeWebConnect2011/src/com/thinkit/lewebconnect/ThinkIT.java b/LeWebConnect2011/src/com/thinkit/lewebconnect/ThinkIT.java index acdf231..3a24c56 100644 --- a/LeWebConnect2011/src/com/thinkit/lewebconnect/ThinkIT.java +++ b/LeWebConnect2011/src/com/thinkit/lewebconnect/ThinkIT.java @@ -1,55 +1,55 @@ -package com.thinkit.lewebconnect; - -import android.app.Activity; -import android.content.Intent; -import android.net.Uri; -import android.os.Bundle; -import android.view.View; -import android.widget.Button; - -public class ThinkIT extends Activity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - // TODO Auto-generated method stub - super.onCreate(savedInstanceState); - - setContentView(R.layout.thinkit); - - try { - View button = findViewById(R.id.thinkit_button); - button.setOnClickListener(new View.OnClickListener() { - public void onClick(View v) { - String url = "http://i-think-it.com/"; - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse(url)); - startActivity(i); - } - }); - - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - View button = findViewById(R.id.email); - button.setOnClickListener(new View.OnClickListener() { - - public void onClick(View v) { - // TODO Auto-generated method stub - final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); - emailIntent.setType("plain/text"); - emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"contact@i-think-it.com"}); - emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Feedback"); - emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hello guys, your app is great ... "); - startActivity(Intent.createChooser(emailIntent, "Send mail...")); - } - }); - - - - } - - - -} +package com.thinkit.lewebconnect; + +import android.app.Activity; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; + +public class ThinkIT extends Activity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + // TODO Auto-generated method stub + super.onCreate(savedInstanceState); + + setContentView(R.layout.thinkit); + + try { + View button = findViewById(R.id.thinkit_button); + button.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + String url = "http://i-think-it.com/"; + Intent i = new Intent(Intent.ACTION_VIEW); + i.setData(Uri.parse(url)); + startActivity(i); + } + }); + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + View button = findViewById(R.id.email); + button.setOnClickListener(new View.OnClickListener() { + + public void onClick(View v) { + // TODO Auto-generated method stub + final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); + emailIntent.setType("plain/text"); + emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"contact@i-think-it.com"}); + emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Feedback"); + emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hello guys, your app is great ... "); + startActivity(Intent.createChooser(emailIntent, "Send mail...")); + } + }); + + + + } + + + +} diff --git a/LeWebConnect2011/src/com/thinkit/lewebconnect/Tweet.java b/LeWebConnect2011/src/com/thinkit/lewebconnect/Tweet.java index dfabe63..861097c 100644 --- a/LeWebConnect2011/src/com/thinkit/lewebconnect/Tweet.java +++ b/LeWebConnect2011/src/com/thinkit/lewebconnect/Tweet.java @@ -1,102 +1,102 @@ -package com.thinkit.lewebconnect; - -import java.io.IOException; -import java.net.URL; -import java.util.ArrayList; - -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.HttpClient; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.BasicResponseHandler; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.util.EntityUtils; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.json.JSONTokener; - -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.widget.Toast; - -public class Tweet { - public static final String TWEETS_URL = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name="; - public String message; - public String date_created; - public String tweet_id; - public String profile_image_url; - - public Tweet(String tweet_id, String message, String date_created, String profile_image_url) { - this.message = message; - this.date_created = date_created; - this.tweet_id = tweet_id; - this.profile_image_url = profile_image_url; - } - - public static ArrayList getTweets(String username) { - - ArrayList tweets = new ArrayList(); - - HttpClient client = new DefaultHttpClient(); - HttpGet get = new HttpGet(TWEETS_URL + username + "&count=10"); - - - HttpResponse httpResponse = null; - -// ResponseHandler responseHandler = new BasicResponseHandler(); - String responseBody = null; - - try { -// responseBody = client.execute(get, responseHandler); - httpResponse = client.execute(get); - HttpEntity httpEntity = httpResponse.getEntity(); - responseBody = EntityUtils.toString(httpEntity, "UTF-8"); - } catch (ClientProtocolException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - - try { -// JSONObject stats = (JSONObject) new JSONTokener(responseBody).nextValue(); - JSONTokener tokener = new JSONTokener(responseBody); - JSONArray stats = new JSONArray(tokener); - - for (int i =0; i < stats.length(); i++) { - JSONObject stat = stats.getJSONObject(i); - String tweet_id = stat.getString("id_str"); - String tweet_msg = stat.getString("text"); - String tweet_date = stat.getString("created_at"); - tweet_date = tweet_date.substring(0, 16); - JSONObject user = stat.getJSONObject("user"); - String profile_image = user.getString("profile_image_url"); - Tweet tweet = new Tweet(tweet_id, tweet_msg, tweet_date, profile_image); - tweets.add(tweet); - } - - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - return tweets; - - } - - public Bitmap getBitmap(String bitmapUrl) { - try { - URL url = new URL(bitmapUrl); - return BitmapFactory.decodeStream(url.openConnection().getInputStream()); - } catch (Exception e) { - // TODO: handle exception - return null; - } - } - -} +package com.thinkit.lewebconnect; + +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.HttpClient; +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.BasicResponseHandler; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.util.EntityUtils; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONTokener; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.widget.Toast; + +public class Tweet { + public static final String TWEETS_URL = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name="; + public String message; + public String date_created; + public String tweet_id; + public String profile_image_url; + + public Tweet(String tweet_id, String message, String date_created, String profile_image_url) { + this.message = message; + this.date_created = date_created; + this.tweet_id = tweet_id; + this.profile_image_url = profile_image_url; + } + + public static ArrayList getTweets(String username) { + + ArrayList tweets = new ArrayList(); + + HttpClient client = new DefaultHttpClient(); + HttpGet get = new HttpGet(TWEETS_URL + username + "&count=10"); + + + HttpResponse httpResponse = null; + +// ResponseHandler responseHandler = new BasicResponseHandler(); + String responseBody = null; + + try { +// responseBody = client.execute(get, responseHandler); + httpResponse = client.execute(get); + HttpEntity httpEntity = httpResponse.getEntity(); + responseBody = EntityUtils.toString(httpEntity, "UTF-8"); + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + try { +// JSONObject stats = (JSONObject) new JSONTokener(responseBody).nextValue(); + JSONTokener tokener = new JSONTokener(responseBody); + JSONArray stats = new JSONArray(tokener); + + for (int i =0; i < stats.length(); i++) { + JSONObject stat = stats.getJSONObject(i); + String tweet_id = stat.getString("id_str"); + String tweet_msg = stat.getString("text"); + String tweet_date = stat.getString("created_at"); + tweet_date = tweet_date.substring(0, 16); + JSONObject user = stat.getJSONObject("user"); + String profile_image = user.getString("profile_image_url"); + Tweet tweet = new Tweet(tweet_id, tweet_msg, tweet_date, profile_image); + tweets.add(tweet); + } + + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return tweets; + + } + + public Bitmap getBitmap(String bitmapUrl) { + try { + URL url = new URL(bitmapUrl); + return BitmapFactory.decodeStream(url.openConnection().getInputStream()); + } catch (Exception e) { + // TODO: handle exception + return null; + } + } + +} diff --git a/LeWebConnect2011/src/com/thinkit/lewebconnect/TweetsAdapter.java b/LeWebConnect2011/src/com/thinkit/lewebconnect/TweetsAdapter.java index 271bf29..c2c8a4b 100644 --- a/LeWebConnect2011/src/com/thinkit/lewebconnect/TweetsAdapter.java +++ b/LeWebConnect2011/src/com/thinkit/lewebconnect/TweetsAdapter.java @@ -1,51 +1,51 @@ -package com.thinkit.lewebconnect; - -import java.util.ArrayList; - -import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.TextView; - -public class TweetsAdapter extends ArrayAdapter { - private ArrayList tweets; - private Context context; - - public TweetsAdapter(Context context, int textViewResouceID, ArrayList tweets) { - super(context, textViewResouceID, tweets); - this.tweets = tweets; - this.context = context; - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - View v = convertView; - if (v == null) { - LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - v = vi.inflate(R.layout.tweet_row, null); - } - - Tweet tweet = tweets.get(position); - if (tweet != null) { - TextView tweet_msg = (TextView) v.findViewById(R.id.tweet_text); - TextView tweet_date = (TextView) v.findViewById(R.id.tweet_date); -// ImageView image = (ImageView) v.findViewById(R.id.avatar); -// - if (tweet_date != null) { - tweet_date.setText(tweet.date_created); - } - - if(tweet_msg != null) { - tweet_msg.setText(tweet.message); - } - -// if(image != null) { -// image.setImageBitmap(getBitmap(tweet.image_url)); -// } - } - return v; - - } -} +package com.thinkit.lewebconnect; + +import java.util.ArrayList; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +public class TweetsAdapter extends ArrayAdapter { + private ArrayList tweets; + private Context context; + + public TweetsAdapter(Context context, int textViewResouceID, ArrayList tweets) { + super(context, textViewResouceID, tweets); + this.tweets = tweets; + this.context = context; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + View v = convertView; + if (v == null) { + LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + v = vi.inflate(R.layout.tweet_row, null); + } + + Tweet tweet = tweets.get(position); + if (tweet != null) { + TextView tweet_msg = (TextView) v.findViewById(R.id.tweet_text); + TextView tweet_date = (TextView) v.findViewById(R.id.tweet_date); +// ImageView image = (ImageView) v.findViewById(R.id.avatar); +// + if (tweet_date != null) { + tweet_date.setText(tweet.date_created); + } + + if(tweet_msg != null) { + tweet_msg.setText(tweet.message); + } + +// if(image != null) { +// image.setImageBitmap(getBitmap(tweet.image_url)); +// } + } + return v; + + } +}