使用带有Android的JSON在Listview上获取图像

时间:2015-12-06 22:31:36

标签: android json rest listview

我已经根据Tavi Ramada(Android JSON解析教程)http://www.androidhive.info/2012/01/android-json-parsing-tutorial/的教程实现了一个listview,它完美地工作但是我想在listview的左侧添加一个图像来显示产品图像。

这是我创建的网络服务的链接: http://sheloappwebservices.axikmobile.com/getjson.php

这是我的主要活动代码:

package com.example.myapp;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class JSONSheloActivity extends ListActivity {

    private ProgressDialog pDialog;

    // URL to get contacts JSON
    private static String url = "http://sheloappwebservices.axikmobile.com/getjson.php/";

    // JSON Node names
    private static final String TAG_PRODUCTOS = "productos";
    private static final String TAG_ID = "id";
    private static final String TAG_CLAVE = "clave";
    private static final String TAG_NOMBRE = "nom_prod";
    private static final String TAG_TITULO = "titulo_1";
    private static final String TAG_SUBTITULO1 = "subtitulo_1";
    private static final String TAG_SUBTITULO2 = "subtitulo_2";
    private static final String TAG_PROP1 = "prop_1";
    private static final String TAG_PROP2 = "prop_2";
    private static final String TAG_PROP3 = "prop_3";
    private static final String TAG_PROP4 = "prop_4";
    private static final String TAG_PROP5 = "prop_5";
    private static final String TAG_PROP6 = "prop_6";
    private static final String TAG_PROP7 = "prop_7";
    private static final String TAG_PROP8 = "prop_8";
    private static final String TAG_PROP9 = "prop_9";
    private static final String TAG_PROP10 = "prop_10";
    private static final String TAG_PROP11 = "prop_11";
    private static final String TAG_PRECIO = "precio_prod";
    private static final String TAG_CONTENIDO = "contenido_prod";
    private static final String TAG_NOTA = "notamed_prod";
    private static final String TAG_CATEGORIA = "categ_prod";
    private static final String TAG_IMAGEN = "img_prod";

    // contacts JSONArray
    JSONArray productos = null;

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> productList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activityjson_main);

        productList = new ArrayList<HashMap<String, String>>();

        ListView lv = getListView();

        // Listview on item click listener
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // getting values from selected ListItem
                String nom_prod = ((TextView) view.findViewById(R.id.nom_prod))
                        .getText().toString();

                String titulo_1 = ((TextView) view.findViewById(R.id.titulo_1))
                        .getText().toString();

                String precio_prod = ((TextView) view.findViewById(R.id.precio_prod))
                        .getText().toString();

                // Starting single contact activity
                Intent in = new Intent(getApplicationContext(),
                        SingleContactActivity.class);
                in.putExtra(TAG_NOMBRE, nom_prod);
                in.putExtra(TAG_TITULO, titulo_1);
                in.putExtra(TAG_PRECIO, precio_prod);

                startActivity(in);

            }
        });

        // Calling async task to get json
        new GetContacts().execute();
    }

    /**
     * Async task class to get json by making HTTP call
     * */
    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(JSONSheloActivity.this);
            pDialog.setMessage("Cargando productos...");
           // pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

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

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

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

                    // Getting JSON Array node
                    productos = jsonObj.getJSONArray(TAG_PRODUCTOS);

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

                        String nombre = c.getString(TAG_NOMBRE);
                        String titulo = c.getString(TAG_TITULO);
                        String precio = c.getString(TAG_PRECIO);

                        // Phone node is JSON Object
                        /* JSONObject phone = c.getJSONObject(TAG_PHONE);
                        String mobile = phone.getString(TAG_PHONE_MOBILE);
                        String home = phone.getString(TAG_PHONE_HOME);
                        String office = phone.getString(TAG_PHONE_OFFICE); */

                        // tmp hashmap for single contact
                        HashMap<String, String> product_info = new HashMap<String, String>();

                        // adding each child node to HashMap key => value

                        product_info.put(TAG_NOMBRE, nombre);
                        product_info.put(TAG_TITULO, titulo);
                        product_info.put(TAG_PRECIO, precio);

                        // adding contact to contact list
                        productList.add(product_info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            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(
                    JSONSheloActivity.this, productList,
                    R.layout.listajson_item, new String[] {TAG_NOMBRE, TAG_TITULO,
                    TAG_PRECIO }, new int[] {R.id.nom_prod,
                    R.id.titulo_1, R.id.precio_prod });

            setListAdapter(adapter);
        }

    }

}

如何为列表视图添加图像并显示它?任何有关这方面的反馈将不胜感激。谢谢

2 个答案:

答案 0 :(得分:0)

您需要为此实现自己的自定义适配器,并覆盖适配器中的$("body").append('<div id="jquery-cookie-law-script" class="' + this.options.style +' ' + this.options.location + ' ' + position + '">'+ cookieInfoMore + cookieInfoMain + '</div>'); 方法以根据需要修改列表项。这是实现此类行为http://www.vogella.com/tutorials/AndroidListView/article.html#ownadapter

的非常好的教程

答案 1 :(得分:0)