【问题标题】:How to display image in listview from json dynamically如何从json动态显示列表视图中的图像
【发布时间】:2013-05-08 22:54:55
【问题描述】:

我想列出放置在特定 url 中的 json 中的文本和图像

我已经连续显示了textview,但是我的代码是如何显示图像的

public class AndroidJSONParsingActivity extends ListActivity {


    // url to make request
    private static String url = "***MY URL***";

    // JSON Node names

    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_PRICE = "price";
    private static final String TAG_URL = "hosting_thumbnail_url";


    // contacts JSONArray


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

        // Hashmap for ListView
        ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONArray json = jParser.getJSONFromUrl(url);

        try {
            // Getting Array of Contacts


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

                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String email = c.getString(TAG_PRICE);
                //String  image1 = c.getString( TAG_URL);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_NAME, name);
                map.put(TAG_PRICE, email);
                //map.put(TAG_URL, image1);


                // adding HashList to ArrayList
                contactList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(this, contactList,
                R.layout.list_item,
                new String[] { TAG_NAME, TAG_PRICE }, new int[] {
                        R.id.name, R.id.email });

        setListAdapter(adapter);


    }

}

我想要这样的列表视图“http://pic002.cnblogs.com/images/2012/372551/2012021011374176.jpg

【问题讨论】:

    标签: android json android-listview android-arrayadapter


    【解决方案1】:

    您需要编写一个自定义列表适配器。谷歌“android 自定义列表适配器图像”。第一个命中是this 一个,这看起来是个不错的例子。

    【讨论】:

      【解决方案2】:

      正如 SimonSays 所说,您必须实现自己的 ListView 适配器类及其 getView() 方法。

      如果您打算在 ListView 中动态获取这些 JSON 对象,我强烈建议使用一些 Thread 机制,这会在获取该 JSON 对象时立即将数据设置为 ListView - 即 AsyncTask

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-18
        • 1970-01-01
        相关资源
        最近更新 更多