【问题标题】:Adding images in ListView在 ListView 中添加图像
【发布时间】:2011-04-07 06:40:56
【问题描述】:

我是安卓开发新手。我想在ListView 中添加图像,例如this

有什么想法吗?

【问题讨论】:

标签: android listview


【解决方案1】:

this tutorial 你可以看到如何定义一个自定义的ListView。这应该有助于您实施自己的解决方案。

【讨论】:

    【解决方案2】:

    Here's 通过重构 List Adapter 代码高效实现复杂的列表视图。

    【讨论】:

      【解决方案3】:

      这是帮助您在列表视图中添加图像的代码

      public class ItemsList extends ListActivity {
      
      private ItemsAdapter adapter;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      
          setContentView(R.layout.items_list);
      
          this.adapter = new ItemsAdapter(this, R.layout.items_list_item, ItemManager.getLoadedItems());
          setListAdapter(this.adapter);
      }
      
      private class ItemsAdapter extends ArrayAdapter<Item> {
      
          private Item[] items;
      
          public ItemsAdapter(Context context, int textViewResourceId, Item[] items) {
                  super(context, textViewResourceId, items);
                  this.items = items;
          }
      
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
                  View v = convertView;
                  if (v == null) {
                          LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  v = vi.inflate(R.layout.items_list_item, null);
                  }
      
                  Item it = items[position];
                  if (it != null) {
                          ImageView iv = (ImageView) v.findViewById(R.id.list_item_image);
                          if (iv != null) {
                                  iv.setImageDrawable(it.getImage());
                          }
                  }
      
                  return v;
              }
          }
      
          @Override
          protected void onListItemClick(ListView l, View v, int position, long id) {
              this.adapter.getItem(position).click(this.getApplicationContext());
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-02
        • 1970-01-01
        • 2014-09-09
        相关资源
        最近更新 更多