简体   繁体   中英

Bitmap imageview in ListView

I'm currently trying to integrate an image into an object currently of strings. However, the image is a url returned in the api at the same time as the rest of the strings. So need to retrieve the image, and pass it into the object for the adapter to populate the image view in the listview. Sorry if that's confusing!

The ListViewLoader is as below:

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

    JSONObject jObject;
    /** Doing the parsing of xml data in a non-ui thread */
    @Override
    protected SimpleAdapter doInBackground(String... strJson) {
        List<HashMap<String, String>> properties = null;

        try{
            PropertiesParser propertiesParser = new PropertiesParser();
            jObject = new JSONObject(strJson[0]);

            /** Getting the parsed data as a List construct */
            properties = propertiesParser.parse(jObject);
        }catch(Exception e) {
            Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
        }

        /** Keys used in Hashmap */
        String[] from = { "image", "numBedrooms","propertyType","marketingStatus","price","applicationStatus","displayableAddress","listingId"};

        /** Ids of views in listview_layout */
        int[] to = { R.id.image, R.id.numBedrooms, R.id.propertyType, R.id.marketingStatus, R.id.price, R.id.applicationStatus, R.id.displayableAddress, R.id.listingId};

        Log.d(TAG, properties.toString());
        /**
         *  Instantiating an adapter to store each item
         *  R.layout.listview_layout defines the layout of each item
         */
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), properties, R.layout.properties_layout, from, to);

        return adapter;
    }

    /** Invoked by the Android system on "doInBackground" is executed completely */
    /** This will be executed in ui thread */
    @Override
    protected void onPostExecute(SimpleAdapter adapter) {

        /** Getting a reference to listview of main.xml layout file */
        ListView listView = ( ListView ) findViewById(R.id.listView1);

        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new ListClickHandler());

        InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    }
}

And the properties parser is as below:

 import android.util.Log; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class PropertiesParser { private static final String TAG = PropertiesParser.class.getSimpleName(); /** Receives a JSONObject and returns a list */ public List<HashMap<String,String>> parse(JSONObject jObject){ JSONObject jembedded = null; JSONArray jProperties = null; try { // Retrieves all the elements in the '_embedded' array jembedded = jObject.getJSONObject("_embedded"); jProperties = jembedded.getJSONArray("items"); } catch (JSONException e) { e.printStackTrace(); } return getProperties(jProperties); } private List<HashMap<String, String>> getProperties(JSONArray jProperties){ int propertyCount = jProperties.length(); List<HashMap<String, String>> propertyList = new ArrayList<HashMap<String,String>>(); HashMap<String, String> property = null; /** Taking each property, parses and adds to list object */ for(int i=0; i<propertyCount;i++){ try { /** Call getProperty with property JSON object to parse the property */ property = getProperty((JSONObject)jProperties.get(i)); propertyList.add(property); } catch (JSONException e) { e.printStackTrace(); } } return propertyList; } /** Parsing the Property JSON object */ private HashMap<String, String> getProperty(JSONObject jProperty) { HashMap<String, String> property = new HashMap<String, String>(); String applicationStatus = ""; String marketingStatus = ""; String numBedrooms = ""; String propertyType = ""; String price = ""; String displayableAddress = ""; String listingId = ""; JSONObject revisions = null; JSONObject currentRevision = null; JSONObject extra = null; JSONObject address = null; try { // Populate details into array.. listingId = jProperty.getString("id"); applicationStatus = jProperty.getString("application_status"); marketingStatus = jProperty.getString("marketing_status"); revisions = jProperty.getJSONObject("_embedded"); currentRevision = revisions.getJSONObject("current_revision"); extra = currentRevision.getJSONObject("_embedded"); address = extra.getJSONObject("address"); numBedrooms = currentRevision.getString("number_of_beds"); propertyType = currentRevision.getString("property_type"); price = currentRevision.getString("price"); displayableAddress = address.getString("line_one") + ", " + address.getString("post_code"); property.put("listingId", listingId); property.put("applicationStatus", applicationStatus); property.put("marketingStatus", marketingStatus); property.put("numBedrooms", numBedrooms + " bedroom(s)"); property.put("propertyType", propertyType); property.put("price", price); property.put("displayableAddress", displayableAddress); Log.d(TAG, property.toString()); } catch (JSONException e) { e.printStackTrace(); } return property; } } 

I have tried integrating the tutorial in the link below but just can't figure out how to use it with the adapter: https://codingsec.net/2016/04/load-image-internet-android/

Sorry, If any further info is needed please let me know.

You can pass the strings in the adapter and use picasso library, like this:

Picasso.with(context).load(yourStringUrl).into(imageView);

to populate the image view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM