我正在使用JSON API來收集列表視圖的數據。 lisview有幾個textviews和一個imageviews。我已經通過很多示例來加載帶有URL的imageview,但沒有一個適用於listview圖像。這是我的主文件。將ListView中的imageview加載到JSON數據收集的URL
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private String userphoto,radiusdb,carWash,carpetWash,commerical,domestic, garden,industrial,laundry,oven,pest,tile,vet,windowWash,verification, serviceRadius, listing_price;
private ListView lv;
// URL to get contacts JSON
private static String url = api url;
ArrayList<HashMap<String, String>> resultList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetResults().execute();
}
/**
* Async task class to get json by making HTTP call
*/
private class GetResults extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray results = jsonObj.getJSONArray("Results");
// looping through All Contacts
for (int i = 0; i < results.length(); i++) {
JSONObject c = results.getJSONObject(i);
//verification="";
String id = c.getString("id");
String title = c.getString("title");
String url = c.getString("url");
// Phone node is JSON Object
JSONObject user_data = c.getJSONObject("user_data");
String wlt_membership = user_data.getString("wlt_membership");
try {
userphoto = user_data.getString("userphoto");
}
catch(final JSONException e){
userphoto="https://www.kleanify.com/wp-content/themes/BT/templates/template_business_theme/img/defaultverify.jpg";
}
JSONObject post_meta = c.getJSONObject("post_meta");
String addressCount1 = post_meta.getString("addressCount1");
try{
listing_price = post_meta.getString("listing_price");
}
catch(final JSONException e){
listing_price="";
}
try{
radiusdb = post_meta.getString("radiusdb");
}
catch(final JSONException e){
radiusdb="";
}
JSONObject categories = c.getJSONObject("categories");
try {
windowWash = categories.getString("window-wash");
}
catch(final JSONException e){
windowWash="";
}
try {
carWash = categories.getString("car-wash");
}
catch(final JSONException e){
carWash="";
}
try {
carpetWash = categories.getString("carpet-wash");
}
catch(final JSONException e){
carpetWash="";
}
try {
commerical = categories.getString("commercial-cleaning");
}
catch(final JSONException e){
commerical="";
}
try {
domestic = categories.getString("domestic-cleaning");
}
catch(final JSONException e){
domestic="";
}
try {
garden = categories.getString("garden-cleaning");
}
catch(final JSONException e){
garden="";
}
try {
industrial = categories.getString("industrial-cleaning");
}
catch(final JSONException e){
industrial="";
}
try {
laundry = categories.getString("laundry");
}
catch(final JSONException e){
laundry="";
}
try {
oven = categories.getString("oven-cleaning");
}
catch(final JSONException e){
oven="";
}
try {
pest = categories.getString("pest-control");
}
catch(final JSONException e){
pest="";
}
try {
tile = categories.getString("tile-cleaning");
}
catch(final JSONException e){
tile="";
}
try {
vet = categories.getString("vet");
}
catch(final JSONException e){
vet="";
}
if(wlt_membership.equals("0")){
verification="";
}
else if(wlt_membership.equals("1")){
verification="Silver Member";
}
else if(wlt_membership.equals("3")){
verification="Verified";
}
serviceRadius="Service Radius "+radiusdb+" miles";
// tmp hash map for single contact
HashMap<String, String> result = new HashMap<>();
// adding each child node to HashMap key => value
result.put("id", id);
result.put("title", title);
result.put("url", url);
result.put("verification",verification);
result.put("userphoto",userphoto);
result.put("serviceRadius",serviceRadius);
result.put("addressCount1",addressCount1);
result.put("listing_price",listing_price);
// adding contact to contact list
resultList.add(result);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, resultList,
R.layout.list_item, new String[]{"title","verification","serviceRadius","addressCount1","listing_price"}, new int[]{
R.id.title, R.id.verification_type,R.id.radius,R.id.address,R.id.service});
/*MySimpleAdapter adapter = new MySimpleAdapter(MainActivity.this, resultList,
R.layout.row, new String[] {}, new int[] {});*/
lv.setAdapter(adapter);
}
}
}
請加適配器類此 –
你爲什麼不使用畢加索圖像庫顯示在列表視圖中 – param