Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8691499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:07:59+00:00 2026-06-13T00:07:59+00:00

I follow this toturial Android Lazy Loading images and text in listview from http

  • 0

I follow this toturial Android Lazy Loading images and text in listview from http json data and now i change listview to gridview.

My question is

I want to click gridview item and it show image in imageview

MainActivity

public class MainActivity extends Activity {

GridView gridView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String strUrl = "http://ta.wptrafficanalyzer.in/demo1/first.php/countries/";
    DownloadTask downloadTask = new DownloadTask();
    downloadTask.execute(strUrl);

    gridView = (GridView) findViewById(R.id.lv_countries);
    gridView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v, int positon,
                long id) {
            Toast.makeText(getApplicationContext(), "welcome to hello gridview", Toast.LENGTH_SHORT).show();
        }
    });

}

private String downloadUrl(String strUrl) throws IOException{
    String data = "";
    InputStream iStream = null;
    try{
        URL url = new URL(strUrl);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.connect();
        iStream = urlConnection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
        StringBuffer sb  = new StringBuffer();
        String line = "";
        while( ( line = br.readLine())  != null){
            sb.append(line);
        }

        data = sb.toString();
        br.close();

    }catch(Exception e){
        Log.d("Exception while downloading url", e.toString());
    }finally{
        iStream.close();
    }

    return data;
}

private class DownloadTask extends AsyncTask<String, Integer, String>{
    String data = null;
    @Override
    protected String doInBackground(String... url) {
        try{
            data = downloadUrl(url[0]);
        }catch(Exception e){
            Log.d("Background Task",e.toString());
        }
        return data;
    }

    @Override
    protected void onPostExecute(String result) {
        ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();
        listViewLoaderTask.execute(result);
    }
}

private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{

    JSONObject jObject;
    @Override
    protected SimpleAdapter doInBackground(String... strJson) {
        try{
            jObject = new JSONObject(strJson[0]);
            CountryJSONParser countryJsonParser = new CountryJSONParser();
            countryJsonParser.parse(jObject);
        }catch(Exception e){
            Log.d("JSON Exception1",e.toString());
        }

        CountryJSONParser countryJsonParser = new CountryJSONParser();
        List<HashMap<String, Object>> countries = null;

        try{
            countries = countryJsonParser.parse(jObject);
        }catch(Exception e){
            Log.d("Exception",e.toString());
        }

        String[] from = { "country","flag","details"};
        int[] to = { R.id.tv_country,R.id.iv_flag,R.id.tv_country_details};
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.lv_layout, from, to);

        return adapter;
    }

    @Override
    protected void onPostExecute(SimpleAdapter adapter) {

        gridView.setAdapter(adapter);

        for(int i=0;i<adapter.getCount();i++){
            HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
            String imgUrl = (String) hm.get("flag_path");
            ImageLoaderTask imageLoaderTask = new ImageLoaderTask();

            HashMap<String, Object> hmDownload = new HashMap<String, Object>();
            hm.put("flag_path",imgUrl);
            hm.put("position", i);

            imageLoaderTask.execute(hm);
        }
    }
}

private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{

    @Override
    protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {

        InputStream iStream=null;
        String imgUrl;
        imgUrl = (String) hm[0].get("flag_path");
        int position = (Integer) hm[0].get("position");

        URL url;
        try {
            url = new URL(imgUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.connect();
            iStream = urlConnection.getInputStream();

            File cacheDirectory = getBaseContext().getCacheDir();
            File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+position+".png");
            FileOutputStream fOutStream = new FileOutputStream(tmpFile);

            Bitmap b = BitmapFactory.decodeStream(iStream);
            b.compress(Bitmap.CompressFormat.PNG,100, fOutStream);

            fOutStream.flush();
            fOutStream.close();
            HashMap<String, Object> hmBitmap = new HashMap<String, Object>();

            hmBitmap.put("flag",tmpFile.getPath());
            hmBitmap.put("position",position);

            return hmBitmap;

        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(HashMap<String, Object> result) {
        String path = (String) result.get("flag");
        int position = (Integer) result.get("position");

        SimpleAdapter adapter = (SimpleAdapter ) gridView.getAdapter();
        HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(position);
        hm.put("flag",path);

        adapter.notifyDataSetChanged();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}
CountryJSONParser.java

public class CountryJSONParser {

public List<HashMap<String,Object>> parse(JSONObject jObject){

    JSONArray jCountries = null;
    try {
        jCountries = jObject.getJSONArray("countries");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return getCountries(jCountries);
}

private List<HashMap<String, Object>> getCountries(JSONArray jCountries){
    int countryCount = jCountries.length();
    List<HashMap<String, Object>> countryList = new ArrayList<HashMap<String,Object>>();
    HashMap<String, Object> country = null;

    for(int i=0; i<countryCount;i++){
        try {
            country = getCountry((JSONObject)jCountries.get(i));
            countryList.add(country);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    return countryList;
}

private HashMap<String, Object> getCountry(JSONObject jCountry){

    HashMap<String, Object> country = new HashMap<String, Object>();
    String countryName = "";
    String flag="";
    String language = "";
    String capital = "";
    String currencyCode = "";
    String currencyName = "";

    try {
        countryName = jCountry.getString("countryname");
        flag = jCountry.getString("flag");
        language = jCountry.getString("language");
        capital = jCountry.getString("capital");
        currencyCode = jCountry.getJSONObject("currency").getString("code");
        currencyName = jCountry.getJSONObject("currency").getString("currencyname");

        String details =        "Language : " + language + "\n" +
                            "Capital : " + capital + "\n" +
                            "Currency : " + currencyName + "(" + currencyCode + ")";

        country.put("country", countryName);
        country.put("flag", R.drawable.blank);
        country.put("flag_path", flag);
        country.put("details", details);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return country;
}

}

When Done but gridview show image not properly

<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" >

activity_man.xml

<GridView
    android:id="@+id/lv_countries"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    tools:context=".MainActivity" />

ly_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/iv_flag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/str_iv_flag" />

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:08:00+00:00Added an answer on June 13, 2026 at 12:08 am

    In gridview onClickListener get data of clicked item

    gridView.setOnItemClickListener(new OnItemClickListener() {
    
            public void onItemClick(AdapterView<?> parent, View v, int positon,
                    long id) {
    HashMap<String, Object> hm = gridView.getAdapter().getPosition(positon);
    
       String imgPath = (String) hm.get("flag"); //get downloaded image path
    Intent i = new Intent(yourActivityContext, NewActivityToDisplayImage.class); //start new Intent to another Activity.
    i.putExtra("ClickedImagePath", imgPath ); //put image link in intent.
    startActivity(i);
    
            }
        });
    

    In NewActivityToDisplayImage get image path and set to imageview.

    public class NewActivityToDisplayImage extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout);
    
            String imagePath = getIntent().getStringExtra("ClickedImagePath"); //get image path from post activity
            if (imagePath != null && !imagePath.isEmpty) {
                File imgFile = new File(imagePath);
                if (imgFile.exists()) {
    
                    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    
                    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
                    myImage.setImageBitmap(myBitmap);
    
                }
    
            }
    
        }}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I follow this tutorial http://wptrafficanalyzer.in/blog/android-lazy-loading-images-and-text-in-listview-from-http-json-data/ . and I wonder, how can i click the
I just follow this DIHQuickStart , try to import data to solr from mysql.
I follow this article to update data to gae localhost server from MySQL. This
I'm trying to follow this tutorial from Cython: http://docs.cython.org/docs/tutorial.html#the-basics-of-cython and I'm having a problem.
i'm trying to follow this tutorial here http://ios-blog.co.uk/iphone-development-tutorials/parsing-json-on-ios-with-asihttprequest-and-sbjson/ to do both the ASIHttp and
I am trying to follow this example from MSDN: http://msdn.microsoft.com/en-us/library/a1hetckb.aspx I think i'm doing
I tried to follow this instruction: http://wiki.phonegap.com/w/page/16494774/Getting-started-with-Android-PhoneGap-in-Eclipse I can run the example but when
Sorry to follow this topic http://stackoverflow.com/questions/11044825/javascript-run-on-ie-7 I want to use document.GetElementByClassName ON INTERNET EXPLORE
I'm trying to follow this tutorial using StructureMap : http://iridescence.no/post/Constructor-Injection-for-ASPNET-MVC-Action-Filters.aspx What I'm trying to
I'm trying to follow this documentation on Symfony : http://symfony.com/doc/current/book/forms.html ok so here is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.