James Patrick
James Patrick

Reputation: 263

unable to display image in listview

I am unable to display images but the text is displayed.

I have modified the code from http://mobile.dzone.com/news/android-tutorial-how-parse to the code below.

I would prefer using SimpleAdapter only .Is it possible? As I have implemented the same code as below everywhere.

Or little modifications to the below code would be fine.

How do I display images?

public class MainActivity extends ListActivity {

    ArrayList<HashMap<String, String>> mylist;
    SimpleAdapter adapter;

    ProgressDialog pd ;
     ListView lv;

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


        mylist = new ArrayList<HashMap<String, String>>();

        new loadingDisplayClass().execute();
    }

    public class loadingDisplayClass extends AsyncTask<String, Integer, String> {

        protected void onPreExecute() {
             pd = new ProgressDialog(MainActivity.this);
            pd.setTitle("Loading......");
            pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pd.show();

        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            String url = "https://itunes.apple.com/us/rss/topalbums/limit=50/json";

            JSONObject json = JSONfunctions.getJSONfromURL(url);


            try {
                JSONObject arr2 = json.getJSONObject("feed");
                JSONArray arr = arr2.getJSONArray("entry");

                for (int i = 0; i < arr.length(); i++) {
                    JSONObject e1 = arr.getJSONObject(i);

                    JSONArray jsar = e1.getJSONArray("im:image");

                    JSONObject jsname = e1.getJSONObject("im:name");
                    String name = jsname.getString("label");


                    JSONObject jso = e1.getJSONObject("im:artist");
                    String lbl = jso.getString("label");

                        JSONObject e12 = jsar.getJSONObject(0);

                    String icon = e12.getString("label");


                     HashMap<String, String> hashmapnew = new HashMap<String, String>();


                    hashmapnew.put("icons", icon);

                    hashmapnew.put("name", name);
                    hashmapnew.put("artist", lbl);

                    mylist.add(hashmapnew);

                    publishProgress(((int) ((i + 1 / (float) arr.length()) * 100)));


                }

            } catch (JSONException e) {

                Toast.makeText(getBaseContext(),
                        "Network communication error!", 5).show();
            }

             adapter = new SimpleAdapter(getBaseContext(), mylist,
                    R.layout.list, new String[] {
                            "icons","name","artist" },
                    new int[] { R.id.image,R.id.name,R.id.artist });

            return null;
        }

        protected void onProgressUpdate(Integer... integers) {

            pd.incrementProgressBy(integers[0]);

        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);


            setListAdapter(adapter);


            pd.dismiss();

               lv = getListView();

            lv.setTextFilterEnabled(true);
            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    @SuppressWarnings("unchecked")
                    HashMap<String, String> o = (HashMap<String, String>) lv
                            .getItemAtPosition(position);


                }
            });
        }

    }




}   

Upvotes: 1

Views: 239

Answers (1)

Pragnani
Pragnani

Reputation: 20155

I am unable to display images but the text is displayed

Images won't display because you are just passing url to the SimpleAdapter.In order to show images you need to first download the images in the url returned above service .

You need to create a custom Adapter and display images by downloading them

Downloading each image and displaying image in imageView takes lot time and as the listview recycle views your images will always download when the list is scrolled. So what you need to do is to use -Universal-Image-Loader or Lazy List to provide automatic downloading and caching images.

Upvotes: 1

Related Questions