0

I'm population a list view with an array and loading a new layout for each item in the array however sometimes an item may be empty/null and I don't want to inflate a layout for that object. Is there any way to do this?

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi = convertView;

        if (vi == null)
            vi = inflater.inflate(R.layout.profile_edit_layout, null);
        TextView tvName = (TextView) vi.findViewById(R.id.textViewSiteName);
        tvName.setText(siteNames.get(position));

        TextView tvInfo = (TextView) vi.findViewById(R.id.textViewSiteInfo);
        tvInfo.setText(sites.get(position));

        ImageView iv = (ImageView) vi.findViewById(R.id.ivProfilePic);
        iv.setImageResource(data.get(position));

    return vi;
}

Say for example sites(3) is null or empty then don't inflate a layout for this item.

Charuක
  • 12,953
  • 5
  • 50
  • 88
lubsey111
  • 29
  • 1

2 Answers2

0

You can iterate over the array with a loop, checking for null or zero values.

If sites(3) != null add to new array and so on, then pass in new array to be populated in ListView

NSTuttle
  • 3,237
  • 2
  • 17
  • 26
  • Edit: Reworded to make more sense. Basically I want to have a button within each layout that performs a different task depending on the sites value. How would I do this is the sites array is different every time? – lubsey111 Dec 03 '16 at 06:30
  • You would test what ever value determines your button. Then implement that particular button. `if(isCool = true){ //add this button} else {//add other button}` – NSTuttle Dec 03 '16 at 06:45
  • Thanks, for the pointers. I think using setTag, getTag for each layout and checking for the tag in the OnClick is best here. Thanks. – lubsey111 Dec 03 '16 at 06:50
  • OR... if a profile picture, name, etc. does not exist, don't add the object to view...You would test each item for !null and either place or dont place object. – NSTuttle Dec 03 '16 at 06:51
0

what you can do is maintain another array which will have only valid data everytime and send that array to adapter rather than main adapter!

Satish Silveri
  • 393
  • 4
  • 9