Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 9th, 2012  |  syntax: None  |  size: 7.43 KB  |  hits: 44  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Android Listview issues in adding data from AsyncTask
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4.  
  5. import org.json.JSONArray;
  6. import org.json.JSONException;
  7. import org.json.JSONObject;
  8. import org.w3c.dom.NodeList;
  9.  
  10.  
  11.  
  12. import android.app.Activity;
  13. import android.app.ProgressDialog;
  14. import android.content.Intent;
  15. import android.os.AsyncTask;
  16. import android.os.Bundle;
  17. import android.sax.Element;
  18. import android.view.View;
  19. import android.widget.AbsListView;
  20. import android.widget.AbsListView.OnScrollListener;
  21. import android.widget.AdapterView;
  22. import android.widget.AdapterView.OnItemClickListener;
  23. import android.widget.Button;
  24. import android.widget.ListView;
  25. import android.widget.ProgressBar;
  26. import android.widget.TextView;
  27.  
  28. public class AndroidJSONParsingActivity extends Activity {
  29.  
  30.     // url to make request
  31.  
  32.  
  33.     // JSON Node names
  34.     static final String TAG_CONTACTS = "comics";
  35.     static final String TAG_ID = "id";
  36.     static final String TAG_NAME = "title";
  37.     static final String TAG_EMAIL = "id";
  38.     static final String TAG_PHONE_MOBILE = "image";
  39.  
  40.     // contacts JSONArray
  41.     JSONArray contacts = null;
  42.     ListView list;
  43.     LazyAdapter adapter;
  44.     static int counter = 0;
  45.     ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
  46. @Override
  47. public void onCreate(Bundle savedInstanceState) {
  48.     super.onCreate(savedInstanceState);
  49.     setContentView(R.layout.main);
  50.     list=(ListView)findViewById(R.id.list);
  51.                     // call the function
  52.     LoadData();
  53.  
  54. }
  55.  
  56. public void LoadData(){
  57.  
  58.  
  59.       String url = "http://myurl?start=" + counter;
  60.  
  61.  
  62.             // Creating JSON Parser instance
  63.             JSONParser jParser = new JSONParser();
  64.  
  65.             // getting JSON string from URL
  66.             JSONObject json = jParser.getJSONFromUrl(url);
  67.  
  68.             try {
  69.                 // Getting Array of Contacts
  70.                 contacts = json.getJSONArray(TAG_CONTACTS);
  71.  
  72.                 // looping through All Contacts
  73.                 for(int i = 0; i < contacts.length(); i++){
  74.                     JSONObject c = contacts.getJSONObject(i);
  75.  
  76.                     // Storing each json item in variable
  77.                     String id = c.getString(TAG_ID);
  78.                     String name = c.getString(TAG_NAME);
  79.                     String email = c.getString(TAG_EMAIL);
  80.                     String mobile = "http://www.funnydash.com/uploads/s/" + c.getString(TAG_PHONE_MOBILE);
  81.                     // Phone number is agin JSON Object
  82.  
  83.                     // creating new HashMap
  84.                     HashMap<String, String> map = new HashMap<String, String>();
  85.  
  86.                     // adding each child node to HashMap key => value
  87.                     map.put(TAG_ID, id);
  88.                     map.put(TAG_NAME, name);
  89.                     map.put(TAG_EMAIL, email);
  90.                     map.put(TAG_PHONE_MOBILE, mobile);
  91.  
  92.                     // adding HashList to ArrayList
  93.                     contactList.add(map);
  94.                 }
  95.             } catch (JSONException e) {
  96.                 e.printStackTrace();
  97.             }
  98.  
  99.  
  100.             /**
  101.              * Updating parsed JSON data into ListView
  102.              * */
  103.  
  104.             // Getting adapter by passing xml data ArrayList
  105.             adapter=new LazyAdapter(this, contactList);        
  106.             list.setAdapter(adapter);
  107.  
  108.  
  109.  
  110.             list.setOnScrollListener(new EndlessScrollListener());
  111.  
  112. }
  113.  
  114. public class BackgroundLoadMore extends AsyncTask<Void, Void, Void> {
  115.  
  116.       @Override
  117.         protected void onPreExecute() {
  118.             // Showing progress dialog before sending http request
  119.  
  120.  
  121.         }
  122.  
  123.     protected Void doInBackground(Void... unused) {
  124.         LoadData();
  125.  
  126.         return (null);
  127.  
  128.  
  129.     }
  130.  
  131.     protected void onPostExecute(Void unused) {
  132.         // On completing background task
  133.         // closing progress dialog etc,.
  134.         list.setOnScrollListener(new EndlessScrollListener());
  135.  
  136.         }
  137.  
  138. }
  139.  
  140.       public class EndlessScrollListener implements OnScrollListener {
  141.  
  142.             private int visibleThreshold = 0;
  143.             private int currentPage = 0;
  144.         private int previousTotal = 0;
  145.         private boolean loading = true;
  146.  
  147.         public EndlessScrollListener() {
  148.         }
  149.         public EndlessScrollListener(int visibleThreshold) {
  150.             this.visibleThreshold = visibleThreshold;
  151.         }
  152.  
  153.         @Override
  154.         public void onScroll(AbsListView view, int firstVisibleItem,
  155.                 int visibleItemCount, int totalItemCount) {
  156.             if (loading) {
  157.                 if (totalItemCount > previousTotal) {
  158.                     loading = false;
  159.                     previousTotal = totalItemCount;
  160.                     counter +=12;
  161.                 }
  162.             }
  163.             if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
  164.                 // I load the next page of gigs using a background task,
  165.                 // but you can call any function here.
  166.                 new BackgroundLoadMore().execute();
  167.                 loading = true;
  168.             }
  169.         }
  170.  
  171.         @Override
  172.         public void onScrollStateChanged(AbsListView view, int scrollState) {
  173.         }
  174.     }
  175.        
  176. import java.util.ArrayList;
  177. import java.util.HashMap;
  178.  
  179. import android.app.Activity;
  180. import android.content.Context;
  181. import android.view.LayoutInflater;
  182. import android.view.View;
  183. import android.view.ViewGroup;
  184. import android.widget.BaseAdapter;
  185. import android.widget.ImageView;
  186. import android.widget.TextView;
  187.  
  188. public class LazyAdapter extends BaseAdapter {
  189.  
  190.     private Activity activity;
  191.     private ArrayList<HashMap<String, String>> data;
  192.  
  193.     private static LayoutInflater inflater=null;
  194.     public ImageLoader imageLoader;
  195.  
  196.  
  197.  
  198.     public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
  199.         activity = a;
  200.         data=d;
  201.         inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  202.         imageLoader=new ImageLoader(activity.getApplicationContext());
  203.     }
  204.  
  205.     public int getCount() {
  206.         return data.size();
  207.     }
  208.  
  209.     public Object getItem(int position) {
  210.         return position;
  211.     }
  212.  
  213.     public long getItemId(int position) {
  214.         return position;
  215.     }
  216.  
  217.  
  218.     public View getView(int position, View convertView, ViewGroup parent) {
  219.         View vi=convertView;
  220.         if(convertView==null)
  221.             vi = inflater.inflate(R.layout.list_row, null);
  222.  
  223.         TextView title = (TextView)vi.findViewById(R.id.title); // title
  224.         TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
  225.  
  226.         ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
  227.  
  228.         HashMap<String, String> song = new HashMap<String, String>();
  229.         song = data.get(position);
  230.  
  231.  
  232.  
  233.         // Setting all values in listview
  234.  
  235.     title.setText(song.get(AndroidJSONParsingActivity.TAG_NAME));
  236.     artist.setText(song.get(AndroidJSONParsingActivity.TAG_ID));
  237.  
  238.     imageLoader.DisplayImage(song.get(AndroidJSONParsingActivity.TAG_PHONE_MOBILE), thumb_image);
  239.     return vi;
  240. }
  241.        
  242. adapter=new LazyAdapter(this, contactList);        
  243. list.setAdapter(adapter);
  244. list.setOnScrollListener(new EndlessScrollListener());
  245.        
  246. CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
  247.        
  248. adapter=new LazyAdapter(this, contactList);        
  249. list.setAdapter(adapter);
  250. list.setOnScrollListener(new EndlessScrollListener());
  251.        
  252. // Getting adapter by passing xml data ArrayList
  253.         adapter=new LazyAdapter(this, contactList);        
  254.         list.setAdapter(adapter);