【问题标题】:How to show a text and image in a listview?如何在列表视图中显示文本和图像?
【发布时间】:2012-08-29 17:12:48
【问题描述】:

我有一个列表视图,我希望它用于显示文本和 corrs 图像。我已经为此使用了arrayadapter。我能够得到一个包含文本值和图像 url 的哈希图数组列表。

<Arraylist<Hashmap<String,string>> testdata  :  "name" and "image_url"

现在我正在尝试绑定它。但是没有显示图像,并且 logcat 显示 resolveuri 在错误的位图上失败。 (我的网址是“/com.example.vocab.MainActivity/res/drawable-hdpi/right_icon.png”)。我究竟做错了什么?提前感谢您的帮助。

  // Binding resources Array to ListAdapter
        this.setListAdapter(new SimpleAdapter(Grammar_tab_all.this, testdata ,
                R.layout.list_item, new String[] { "name","img_url"},
                new int[] { R.id.module_name_item, R.id.img_recom}));
        final ListView lv = getListView();

【问题讨论】:

  • 字符串不是图像,所以你不能指望通过它来解决这样的事情。这也是不够的代码。

标签: android listview bitmapimage


【解决方案1】:

要在列表视图中显示可绘制图像,最好的方法是仅存储可绘制图像的 int id。

试试这个。

listItems = new ArrayList<HashMap<String,Integer>>();
String fieldName = "image_id";

HashMap<String, Integer> listData1 = new HashMap<String, Integer>();
HashMap<String, Integer> listData2 = new HashMap<String, Integer>();

listData1.put(fieldName, R.drawable.camera_icon_focus_dim);
listData2.put(fieldName, R.drawable.camera_icon_scene_mode);

listItems.add(listData1);
listItems.add(listData2);

SimpleAdapter listItemAdapter = new SimpleAdapter(
    this,
    listItems,
    R.layout.image_list_item,
    new String[] { fieldName },
    new int[] { R.id.listitem_img });

【讨论】:

    【解决方案2】:

    如果您需要更多信息,请参考此链接http://developerboards.att.lithium.com/t5/AT-T-Developer-Program-Blogs/Developing-Apps-for-Android-Beyond-quot-Hello-World-quot-Part-I/ba-p/28983/page/2,而不是此&lt;Arraylist&lt;Hashmap&lt;String,string&gt;&gt; testdata 尝试使用此&lt;Arraylist&lt;Hashmap&lt;String,Object&gt;&gt; testdata

    【讨论】:

      【解决方案3】:

      您必须使用自定义列表视图: 看看这个网站 http://blog.sptechnolab.com/2011/02/01/android/android-custom-listview-items-and-adapters/

      【讨论】:

        【解决方案4】:

        如果你使用android资源文件夹中的图片,那么你可以使用

        // get Drawable from resources folder
        Resources res = context.getResources();
        Drawable drawable = res.getDrawable( R.drawable.myImage );
        
        ImageView mImageView.setImageDrawable( mDrawable );
        // or
        ImageView mImageView.setImageBitmap( mBitmap );
        

        ImageView 是您的 ListItems 布局中的一个。我为每个 ListView 编写了一个自己的 ListAdapter,我在其中扩展了特殊布局并将数据设置为布局。

        【讨论】:

          【解决方案5】:

          如果你想拥有不同的图像,你需要一个自定义的列表适配器,这是我在互联网上找到的关于这个主题的最好的 tutorial :)

          【讨论】:

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