Search code examples
androidlistviewconditional-statementsinvisible

ListView with conditions shows empty rows


I'm having an issue displaying ListView items. When I remove my conditions everything displays correctly, but when I put the conditions back in to make things invisible to the user my listview ends up having empty place/line and I don´t know how to fix it. What is wrong? Are the conditions correct?

@Override                                                                                   
public View getView(int position, View convertView, ViewGroup parent) {
    ApplicationInfo entry = aListAppInfo.get(position);
    View v = convertView;                                                                   
    // convertView
    // recyklovace
    if(v == null ){ 
        LayoutInflater inflater = LayoutInflater.from(aContext);
        v = inflater.inflate(R.layout.layout_appinfo, null);
    }else{} 
    //nahrat layout
    ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
    TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
    TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);
    chAppStat = (ToggleButton)v.findViewById(R.id.switch1);
    chAppStat.setTag(entry.packageName);
    chAppStat.setOnCheckedChangeListener(aListener);                                                                        
    if (aListAppInfo.get(position).packageName == aListAppInfo.get(position).loadLabel (aPackManager )
                    ||((entry.packageName.equals("android")) 
                    || (entry.packageName.equals("com.android.systemui"))
                    || (entry.packageName.equals("com.android.dreams.basic"))
                    || (entry.packageName.equals("com.android.certinstaller"))
                    || (entry.packageName.equals("com.android.defcontainer"))
                    || (entry.packageName.equals("com.android.htmlviewer"))
                    || (entry.packageName.equals("com.android.keychain"))
                    || (entry.packageName.equals("com.android.location.fused"))
                    || (entry.packageName.equals("com.android.providers.applications"))
                    || (entry.packageName.equals("com.android.providers.userdictionary")))){
        chAppStat.setVisibility(View.INVISIBLE);        
    }else{  
        chAppStat.setVisibility(View.VISIBLE);
    }
    ivAppIcon.setImageDrawable(entry.loadIcon(aPackManager));
    tvPkgName.setText(entry.packageName);
    tvAppName.setText(entry.loadLabel(aPackManager));
    return v;
// navrat view
}

Solution

  • A few things:

    • You assign convertView to v. Don't do that, there's no need, simply work with convertView directly.

    • Use native ArrayAdapter's getItem(position) instead. This will manage the List you've passed your ArrayAdapter and get items from it, so there's no need to use your own structured.

    • Use View.GONE instead of View.INVISIBLE. This will make the layout disappear completely, instead of leaving the space it fits when it's visible.