我目前正在尝试将图像集成到当前字符串的对象中。但是,图像是在api中与其余字符串同时返回的url。因此需要检索图像,并将其传递到对象中,以便适配器填充列表视图中的图像视图。对不起,如果那令人困惑!
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 :(得分:0)