具有动态布局数据的 ListView 中的内存使用情况



我正在处理一个自定义列表视图,其中包含每行的嵌套数据。 我想显示一个人的姓名,每行都有一个复选框,然后动态地将子行添加到与该人关联的一对多电话号码的 ListView。

这目前正在起作用。 但是,对于较大的数据集,由于内存使用量大,我看到速度变慢和崩溃。 有没有更好的方法来完成我想要做的事情?

以下是我想看到的示例:

[] Joe Smith
  [] 1-555-444-3333
  [] 1-555-777-6655
  [] 1-555-234-3232
[] John Doe
  [] 1-555-882-3434
[] Someone Else
  [] 1-555-949-0304
  [] 1-555-392-4433

这是我目前在 ArrayAdapter 的 getView() 函数中使用的代码。 我试图清理一些,所以如果有任何编译错误,它们可能是我清理的错字。

public View getView(int position, View convertView, ViewGroup parent)
{
View vi=convertView;
ViewHolder holder;
CustomContactInfo custom_contact = getItem(position);
if(convertView==null){
    inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    vi = inflater.inflate(R.layout.name_line, null);
    holder=new ViewHolder();
    holder.contact_name=(TextView)vi.findViewById(R.id.contact_name);
    holder.cb_selected=(CheckBox)vi.findViewById(R.id.contact_check);
    holder.phone_numbers=(LinearLayout)vi.findViewById(R.id.phone_number_container);                
    vi.setTag(holder);
} else {            
    holder=(ViewHolder)vi.getTag();
}
// Set the name of the contact on the main line
holder.contact_name.setText(custom_contact.name);
// Add the custom views for each phone number
holder.phone_numbers.removeAllViews();
for (Phone p : custom_contact.phones) 
{
    View v = this.inflater.inflate(R.layout.phone_number, null);
    CheckBox cb = (CheckBox)v.findViewById(R.id.phone_check);
    TextView number = (TextView)v.findViewById(R.id.phone_number);
    number.setText(p.phone_num);
    holder.phone_numbers.addView(v);
}
return vi;

}

我想你想研究一个ExpandableListView.

最新更新