Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

android - 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 all the data perfectly except the images,i don't know how to solve it,my response looks like this

 {"matching":[{"name":"Monic Dano","profile_id":"GM335695","image":"http://mywebsitename.com/images/Girlnoimage.jpg","cast":"","age":"24","location":"Ivory Coast"}]}

here is my logcat output see my output is look like this and i am not getting images

public class HomeFragment extends ListFragment {

    //CustomAdapter adapter;
    //private List<RowItem> rowItems;

    private ProgressDialog pDialog;
    //JSON parser class
    JSONParser jsonParser = new JSONParser();

    JSONArray matching=null;

    ArrayList<HashMap<String,String>> aList;
    private static String MATCH_URL = null;
    private static final String TAG_MATCH="matching";
    private static final String TAG_NAME="name";
    private static final String TAG_PROFILE="profile_id";
    private static final String TAG_IMAGE="image";
    private static final String TAG_CAST="cast";
    private static final String TAG_AGE="age";
    private static final String TAG_LOCATION="location";

    private ListView listview;

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        String strtext = getArguments().getString("user_login_id");
         MATCH_URL = "http://mywebsitename.com/webservice/matching?version=apps&user_login_id="+strtext;
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        aList = new ArrayList<HashMap<String,String>>();

       // rowItems = new ArrayList<RowItem>();

        listview=(ListView)rootView.findViewById(android.R.id.list);

        new LoadAlbums().execute();



        return rootView;
    }

    class LoadAlbums extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(HomeFragment.this.getActivity());
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        protected String doInBackground(String... args) {
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(MATCH_URL, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    matching = jsonObj.getJSONArray(TAG_MATCH);

                    // looping through All Contacts
                    for (int i = 0; i < matching.length(); i++) {
                        JSONObject c = matching.getJSONObject(i);

                        // Storing each json item values in variable
                        String user_name = c.getString(TAG_NAME);
                        String user_profile=c.getString(TAG_PROFILE);
                        String user_image=c.getString(TAG_IMAGE);
                        String user_cast=c.getString(TAG_CAST);
                        String user_age=c.getString(TAG_AGE);
                        String user_location=c.getString(TAG_LOCATION);



                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_NAME,user_name);
                        map.put(TAG_PROFILE, user_profile);
                        map.put(TAG_IMAGE, user_image);
                        map.put(TAG_CAST, user_cast);
                        map.put(TAG_AGE, user_age+" years");
                        map.put(TAG_LOCATION, user_location);



                        // adding HashList to ArrayList
                        aList.add(map);


                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }


        protected void onPostExecute(String file_url) {

            super.onPostExecute(file_url);
            // dismiss the dialog after getting all albums
            if (pDialog.isShowing())
            pDialog.dismiss();
            // updating UI from Background Thread

                    /**
                     * Updating parsed JSON data into ListView
                     * */

                    // updating listview

                   CustomAdapter adapter = new CustomAdapter(getActivity(),aList);
                    setListAdapter(adapter);
                }

        }




}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try to AndroidQuery with custom adapter :

public class CustomAdapter extends BaseAdapter {

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

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


    public CustomAdapter(Context context,ArrayList<HashMap<String,String>> listData) {
        this.context = context;
        this.listData=listData;
        aQuery = new AQuery(this.context);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

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

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            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 image
        return convertView;
    }
    class ViewHolder{
        ImageView propic;
        TextView txtproname;
        TextView txtproid;
        TextView txtprofilecast;
        TextView txtprofileage;
        TextView txtprofileplace;
    }
}

How to set adapter to ListView :

CustomAdapter adapter = new CustomAdapter(getActivity(),aList);
setListAdapter(adapter);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...