I am working on a custom ListView with nested data for each row. I want to show a person's name with a checkbox for each line, and then dynamically add sub-rows to the ListView for 1-to-many phone numbers associated with that person.
That is currently working. However, with larger datasets I am seeing slowdowns and crashes due to intense memory usage. Is there a better way to accomplish what I'm trying to do?
Here is an example of what I want to see:
[] 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
Here is the code I'm currently using in the getView() function for my ArrayAdapter. I tried to clean it up some, so if there are any compile errors, they are likely a typo from my cleanup.
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;
}