Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have used Universal ImageL0ader for Lazy Loading. You download the same and use it or use a different technique. Customize the below according to your needs.</p> <ol> <li><p>Use a <code>thread</code> or <code>Asynctask</code> to make a http request to the json response.</p></li> <li><p>Parse the response and store it in a list.</p></li> <li><p>Use a Custom Adapter to display the data. </p></li> </ol> <p>You can also use Volley library. </p> <pre><code>public class MainActivity extends Activity { Button b; ListView lv; ArrayList&lt;String&gt; msg = new ArrayList&lt;String&gt;(); ArrayList&lt;String&gt; title = new ArrayList&lt;String&gt;(); ArrayList&lt;String&gt; thumb = new ArrayList&lt;String&gt;(); ProgressDialog pd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv= (ListView) findViewById(R.id.lv); pd = new ProgressDialog(MainActivity.this); pd.setTitle("Loading.."); b= (Button) findViewById(R.id.button1); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new TheTask().execute(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void getData() { HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpGet request = new HttpGet("https://gdata.youtube.com/feeds/api/users/twistedequations/uploads?v=2&amp;alt=jsonc&amp;start-index=1&amp;max-results=5"); try { HttpResponse response = httpclient.execute(request); HttpEntity resEntity = response.getEntity(); String _response=EntityUtils.toString(resEntity); // content will be consume only once JSONObject json = new JSONObject(_response); JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items"); for (int i = 0; i &lt; jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String title1 = jsonObject.getString("title"); title.add(title1); String thumbUrl = jsonObject.getJSONObject("thumbnail").getString("sqDefault"); URL url1 = new URL(thumbUrl); //Bitmap bmp = BitmapFactory.decodeStream(url1.openConnection().getInputStream()); thumb.add(thumbUrl); String url; try { url = jsonObject.getJSONObject("player").getString("default"); msg.add(url); } catch (JSONException ignore) { } } } catch(Exception e1) { e1.printStackTrace(); } httpclient.getConnectionManager().shutdown(); } class TheTask extends AsyncTask&lt;Void,Void,Void&gt; { @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub super.onPostExecute(result); pd.dismiss(); YouTubeAdapter you = new YouTubeAdapter(MainActivity.this,msg,title,thumb); lv.setAdapter(you); } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); pd.show(); } @Override protected Void doInBackground(Void... params) { // TODO Auto-generated method stub getData(); return null; } } } public class YouTubeAdapter extends BaseAdapter{ Context mContext; Intent intent; LayoutInflater mInflater; ArrayList&lt;String&gt; mVideo; ArrayList&lt;String&gt; mTitle; ArrayList&lt;String&gt; mThumb; static DisplayImageOptions options; static ImageLoader imageLoader; public YouTubeAdapter(Context context,ArrayList&lt;String&gt; a,ArrayList&lt;String&gt; title,ArrayList&lt;String&gt; thumb) { mContext=context; mVideo=a; mTitle = title; mThumb= thumb; options = new DisplayImageOptions.Builder() .showImageForEmptyUri(R.drawable.ic_launcher) .resetViewBeforeLoading() .cacheOnDisc() .imageScaleType(ImageScaleType.IN_SAMPLE_INT) .bitmapConfig(Bitmap.Config.RGB_565) .displayer(new FadeInBitmapDisplayer(300)) .build(); imageLoader = ImageLoader.getInstance(); imageLoader.init(ImageLoaderConfiguration.createDefault(context)); mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { // TODO Auto-generated method stub return mVideo.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(final int position, View arg1, ViewGroup arg2) { ViewHolder vh; if(arg1==null) { arg1=mInflater.inflate(R.layout.video, null); vh= new ViewHolder(); vh.tv1=(TextView)arg1.findViewById(R.id.textView1); vh.tv2=(TextView)arg1.findViewById(R.id.textView2); vh.iv=(ImageView)arg1.findViewById(R.id.imageView1); vh.pb = (ProgressBar) arg1.findViewById(R.id.pb); arg1.setTag(vh); } else { vh= (ViewHolder)arg1.getTag(); } vh.tv1.setText(mTitle.get(position)); vh.tv2.setText(mVideo.get(position)); display(vh.iv, mThumb.get(position), vh.pb); return arg1; } static public void display(ImageView img, String url, final ProgressBar spinner) { imageLoader.displayImage(url, img, options, new ImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { spinner.setVisibility(View.VISIBLE); } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { spinner.setVisibility(View.GONE); } @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { spinner.setVisibility(View.GONE); } @Override public void onLoadingCancelled(String imageUri, View view) { } }); } static class ViewHolder { TextView tv1; TextView tv2; ImageView iv; ProgressBar pb; } } &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" &gt; &lt;ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/lv" android:layout_above="@+id/button1" /&gt; &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="Button" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Snap Shot</p> <p><img src="https://i.stack.imgur.com/yjkGB.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload