2017-02-16 15 views
1

現在、文字列のオブジェクトにイメージを統合しようとしています。ただし、画像は、残りの文字列と同時にapiで返されるURLです。そのため、イメージを取得し、アダプターがリストビューにイメージ・ビューを取り込むために、そのイメージをオブジェクトに渡す必要があります。それが混乱している場合は申し訳ありません!ListViewのビットマップ画像ビュー

ListViewLoaderは以下の通りです:

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); 
    } 
} 

とプロパティパーサは以下の通りである:私は下にちょうどリンクにチュートリアルを統合しようとしている

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; 
 
    } 
 
}

アダプターでの使用方法を理解できない: https://codingsec.net/2016/04/load-image-internet-android/

申し訳ありませんが、さらに詳しい情報が必要な場合はお知らせください。

答えて

0

あなたはこのように、アダプタ内の文字列を渡すとpicassoライブラリを使用することができます。

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

は、画像ビューを移入します。

関連する問題