Question

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
}
Was it helpful?

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.

OTHER TIPS

You are getting empty spaces when condition is true? And you want to remove that space? If so, then use View.GONE instead of View.INVISIBLE.

View.INVISIBLE make the present view invisible to the user but don't remove the space allotted by it. Thus showing empty spaces between two ListView items.

View.GONE not only makes view invisible but also remove that space so that it can be used by others. Thus there will be no gaps between two ListView items.

Change chAppStat.setVisibility(View.INVISIBLE); to chAppStat.setVisibility(View.GONE);.

Two notes here.

First, check the Android documentation and see what View.INVISIBLE actually means. As already mentioned by others, View.GONE would be more appropriate in your case.

Second, I would not try to hide the cells like that at all. Filtering the list should be the preferred way. Either filter the list before you use it in the adapter, or use filtering on your custom adapter. An example can be found here, but this technique is more for runtime filtering of the list like if the user can filter the list with a query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top