从 Arraylist> 填充 ListView

发布于 2024-12-16 11:47:33 字数 315 浏览 2 评论 0 原文

我有一个 ArrayList>,我想用它填充 ListView。我可以使用什么类型的适配器以及在适配器的 from 字段中放置什么内容?示例如下:

String[] from = ?
int[] to = new int[] { R.id.contact, R.id.name});
adapter = new KindOfAdapter(this, R.layout.row, from, to)

希望很清楚。感谢任何帮助。

I have an ArrayList<HashMap<Contact, Name>> and I want to populate a ListView with it. What type of adapter can I use and what do I put in the from field of my adapter? Example below:

String[] from = ?
int[] to = new int[] { R.id.contact, R.id.name});
adapter = new KindOfAdapter(this, R.layout.row, from, to)

Hope it is clear. Appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青春有你 2024-12-23 11:47:33

1.实现 BaseAdapter

我认为最好的选择是扩展 BaseAdapter 并实现以下方法:

getCount()
getItem(int)
getItemId(int)
getView(int, View, ViewGroup)

它看起来像这样:

public class MyAdapter extends BaseAdapter() {
   private List<Map<Contact, Name>> map;
   private Context context;

   public MyAdapter(List<Map<Contact, Name>> map>, Context context) {
      this.map = map;
      this.context = context;
   }

   public int getCount() {
      return map.size(); // or do you want one list item per name?
      // if so, just count out all the map entries in each item of the list
   }

   public int getItemId(int position) {
      return position; // doesn't matter too much...
   }

   public View getView(int position, View convertView, ViewGroup parent) {
      // populate the view here...
      // use LayoutInflater.from(context).inflate(resource, parent, false) to inflate new views
   }
}

2。谨慎使用 ViewHolder 模式

在实现 getView() 时,利用此设计模式将节省大量内存:

http://www.screaming-penguin.com/node/7767

1. Implement BaseAdapter

I think your best bet is to extend BaseAdapter and implement the following methods:

getCount()
getItem(int)
getItemId(int)
getView(int, View, ViewGroup)

It would look something like this:

public class MyAdapter extends BaseAdapter() {
   private List<Map<Contact, Name>> map;
   private Context context;

   public MyAdapter(List<Map<Contact, Name>> map>, Context context) {
      this.map = map;
      this.context = context;
   }

   public int getCount() {
      return map.size(); // or do you want one list item per name?
      // if so, just count out all the map entries in each item of the list
   }

   public int getItemId(int position) {
      return position; // doesn't matter too much...
   }

   public View getView(int position, View convertView, ViewGroup parent) {
      // populate the view here...
      // use LayoutInflater.from(context).inflate(resource, parent, false) to inflate new views
   }
}

2. Be scrupulous about using the ViewHolder pattern

When implementing getView(), utilizing this design pattern will save a LOT of memory:

http://www.screaming-penguin.com/node/7767

七分※倦醒 2024-12-23 11:47:33

ArrayAdapter> - ArrayAdapter 设计为与 ArrayLists(和 arrays[])一起使用,尽管它不支持 View 绑定,您只需重写 getView()

An ArrayAdapter> - ArrayAdapter is designed to be used with ArrayLists (and arrays[]), although it doesn't support View binding you only have to override getView()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文