2

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;

}

Blather
  • 1,118
  • 5
  • 15
  • 25
  • I recommend trying not to display so much data within a ListView. I've noticed they are incredibly easy to slow down, even with the EfficientAdapter concept you are using. You can try ExpandableListView, although I don't think it will do much considering it's essentially what you've already created. – mrres1 Sep 25 '12 at 03:50
  • Another suggestion is to minimize the amount of data actually being drawn in the View. That includes different text colors and so forth. The more detailed the View is, the longer it's going to take to draw on the screen, and the more apt it will be to bog down the scrolling of the list. – mrres1 Sep 25 '12 at 03:53

1 Answers1

2

I think you want to look into an ExpandableListView.

zienkikk
  • 2,404
  • 1
  • 21
  • 28
  • I did look at it before, but I wasn't sure if I could duplicate this functionality. Can you default everything to "expanded" and hide the expand/collapse option in an ExpandableListView? Thank you for the tip, I will read up on it some more. – Blather Sep 25 '12 at 03:33
  • Hmm, I'm not sure. You could experiment with ExpandableListView's expandGroup(int) method during activity creation and then maybe making each group disabled? – zienkikk Sep 25 '12 at 03:42
  • This seems like the best option, even though it's a little disappointing that the core ListView can't handle it. Thanks for pointing me towards the ExpandableListView. – Blather Sep 25 '12 at 23:27