Skip to content Skip to sidebar Skip to footer

How To Fetch The Image Using Json In Listfragment?

I am new to android development,I am parsing my data using JSON Parsing method,I extend my class with List Fragment and I want my data in list view but the problem is i am getting

Solution 1:

Try to AndroidQuery with custom adapter :

publicclassCustomAdapterextendsBaseAdapter {

    private Context context;
    private ArrayList<HashMap<String,String>> listData;
    private AQuery aQuery;

    privatestaticfinal String TAG_NAME="name";
    privatestaticfinal String TAG_PROFILE="profile_id";
    privatestaticfinal String TAG_IMAGE="image";
    privatestaticfinal String TAG_CAST="cast";
    privatestaticfinal String TAG_AGE="age";
    privatestaticfinal String TAG_LOCATION="location";


    publicCustomAdapter(Context context,ArrayList<HashMap<String,String>> listData) {
        this.context = context;
        this.listData=listData;
        aQuery = newAQuery(this.context);
    }

    @OverridepublicintgetCount() {
        return listData.size();
    }

    @Overridepublic Object getItem(int position) {
        return listData.get(position);
    }

    @OverridepubliclonggetItemId(int position) {
        return position;
    }

    @Overridepublic View getView(finalint position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = newViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
            holder.propic = (ImageView) convertView.findViewById(R.id.propic);
            holder.txtproname = (TextView) convertView.findViewById(R.id.txtproname);
            holder.txtproid = (TextView) convertView.findViewById(R.id.txtproid);
            holder.txtprofilecast = (TextView) convertView.findViewById(R.id.txtprofilecast);
            holder.txtprofileage = (TextView) convertView.findViewById(R.id.txtprofileage);
            holder.txtprofileplace = (TextView) convertView.findViewById(R.id.txtprofileplace);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtproname.setText(listData.get(position).get(TAG_NAME));
        holder.txtproid.setText(listData.get(position).get(TAG_PROFILE));
        holder.txtprofilecast.setText(listData.get(position).get(TAG_CAST));
        holder.txtprofileage.setText(listData.get(position).get(TAG_AGE));
        holder.txtprofileplace.setText(listData.get(position).get(TAG_LOCATION));

        aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);

        // image parameter : 1 : memory cache,2:file cache,3:target width,4:fallback imagereturn convertView;
    }
    classViewHolder{
        ImageView propic;
        TextView txtproname;
        TextView txtproid;
        TextView txtprofilecast;
        TextView txtprofileage;
        TextView txtprofileplace;
    }
}

How to set adapter to ListView :

CustomAdapteradapter=newCustomAdapter(getActivity(),aList);
setListAdapter(adapter);

Solution 2:

You can use universal image loader for viewing images from your server.Z Just pass the image url and your view and you are good to go.

For your reference here is the link to Universal Image loader with all its documentation. https://github.com/nostra13/Android-Universal-Image-Loader

Hop it helps you.

Solution 3:

I am hardly suggest you to use Android Query for this. Its mind blowing api given by Android itself. You can download image, download bitmap or whatever you wanna do you can. You can download the jar file from here :here Download the jar file and set jar to your Build Path.

AQuery androidAQuery=newAQuery(this);

As an example to load image directly from url:

androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANYDEFAULT IMAGE YOU WANT TOSHOW);

As an example to get Bitmap from url:

androidAQuery.ajax(YOURIMAGEURL,Bitmap.class,0,newAjaxCallback<Bitmap>(){
                        @Overridepublicvoidcallback(String url, Bitmap object, AjaxStatus status) {
                            super.callback(url, object, status);

                            //You will get Bitmap from object.
                        }

                    });

It's very fast and accurate, and using this you can find many more features like Animation when loading; getting a bitmap, if needed; etc.

//Declare adapter globally.private EfficientAdapter adapter;
//Initialize it in onCreate() method
adapter = new EfficientAdapter(this);
//Set your adapter like
listview.setAdapter(adapter);
//Adapter class codeprivateclassEfficientAdapterextendsBaseAdapter {

            private LayoutInflater mInflater;
            private Context context;

            publicEfficientAdapter(Context context) {
                mInflater = LayoutInflater.from(context);
                this.context = context;
            }

            @Override
            publicintgetCount() {
                return aList.size();
            }

            @Override
            public Object getItem(int arg0) {
                returnnull;
            }

            @Override
            publiclonggetItemId(int position) {
                return0;
            }

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {

                final ViewHolder holder;

                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.YOUR ITEM LAYOUT, null);
                    holder = new ViewHolder();
                    holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
                    holder.txtProfile = (TextView) convertView.findViewById(R.id.txtProfile);
                    holder.txtCast = (TextView) convertView.findViewById(R.id.txtCast);
                    holder.txtAge = (ImageView) convertView.findViewById(R.id.txtAge);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

                holder.txtName.setText(aList.get(position).get(TAG_NAME));
                holder.txtProfile.setText(aList.get(position).get(TAG_PROFILE));
                holder.txtCast.setText(aList.get(position).get(TAG_CAST));
                holder.txtAge.setText(aList.get(position).get(TAG_AGE));
                aQuery.id(holder.imgUser).image(data.get(position).get(TAG_IMAGE), true, true);
                return convertView;
            }

            classViewHolder {
                TextView txtName;
                TextView txtProfile;
                TextView txtCast;
                TextView txtAge;
                ImageView imgUser;
            }

        }

Solution 4:

In source code of SimpleAdapter:

privatevoid bindView(int position, View view) {
        final Map dataSet = mData.get(position);
        if (dataSet == null) {
            return;
        }

        final ViewBinder binder = mViewBinder;
        finalString[] from = mFrom;
        finalint[] to = mTo;
        finalint count = to.length;

        for (int i = 0; i < count; i++) {
            final View v = view.findViewById(to[i]);
            if (v != null) {
                finalObject data = dataSet.get(from[i]);
                String text = data == null ? "" : data.toString();
                if (text == null) {
                    text = "";
                }

                boolean bound = false;
                if (binder != null) {
                    bound = binder.setViewValue(v, data, text);
                }

                if (!bound) {
                    if (v instanceof Checkable) {
                        if (data instanceofBoolean) {
                            ((Checkable) v).setChecked((Boolean) data);
                        } elseif (v instanceof TextView) {
                            // Note: keep the instanceof TextView check at the bottom of these// ifs since a lot of views are TextViews (e.g. CheckBoxes).
                            setViewText((TextView) v, text);
                        } else {
                            thrownew IllegalStateException(v.getClass().getName() +
                                    " should be bound to a Boolean, not a " +
                                    (data == null ? "<unknown type>" : data.getClass()));
                        }
                    } elseif (v instanceof TextView) {
                        // Note: keep the instanceof TextView check at the bottom of these// ifs since a lot of views are TextViews (e.g. CheckBoxes).
                        setViewText((TextView) v, text);
                    } elseif (v instanceof ImageView) {
                        if (data instanceofInteger) {
                            setViewImage((ImageView) v, (Integer) data);                            
                        } else {
                            setViewImage((ImageView) v, text);
                        }
                    } else {
                        thrownew IllegalStateException(v.getClass().getName() + " is not a " +
                                " view that can be bounds by this SimpleAdapter");
                    }
                }
            }
        }
    }

You can see if your view is ImageView , the code will use the url String be the resId in

/**
 * Called by bindView() to set the image for an ImageView but only if
 * there is no existing ViewBinder or if the existing ViewBinder cannot
 * handle binding to an ImageView.
 *
 * By default, the value will be treated as an image resource. If the
 * value cannot be used as an image resource, the value is used as an
 * image Uri.
 *
 * This method is called instead of {@link #setViewImage(ImageView, int)}
 * if the supplied data is not an int or Integer.
 *
 * @param v ImageView to receive an image
 * @param value the value retrieved from the data set
 *
 * @see #setViewImage(ImageView, int) 
 */publicvoid setViewImage(ImageView v, String value) {
    try {
        v.setImageResource(Integer.parseInt(value));
    } catch (NumberFormatException nfe) {
        v.setImageURI(Uri.parse(value));
    }
}

And your error is here , so you need Override the getView function of SimpleAdapter.Here is code:

Uriuri= Uri.parse("http://gujjumatch.com/images/Girlnoimage.jpg");
image.setImageURI(uri);

Post a Comment for "How To Fetch The Image Using Json In Listfragment?"