Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip a row in list view

I guess getView() is called for each item of the list, but my question is, Can we not show the row if data associated with that row is null. I want to do this inside getView () only.

For Example:

If the alerts for a person whose name is to be shown in list view are null.I don't want to display that person's name in list view.

like image 637
Rookie Avatar asked Jun 17 '12 11:06

Rookie


2 Answers

You can set the visibility of that row to "gone" in the last line before returning the View - that should work for you.

Edit: Be sure to set visibility to visible if the content is not null - otherwise all the views will become "gone" as the ListView reuses views.

myView.setVisibility((myData == null) ? View.GONE : View.VISIBLE);
like image 121
Tim Avatar answered Nov 16 '22 02:11

Tim


Hi Pals I tried setting the visibility to invisible but still i can see an empty row in a listview so i change a little bit and it worked according to my need so it worth sharing if someone needs similar result like mine.

I am using List as source my requirement was to skip few applications so what i did was i removed that application from the List and notified Data set change

public AppListAdapter(Context context, List<ApplicationInfo> appList) {
        this.context = context;
        this.appList = appList;
        inflater = LayoutInflater.from(context);
        localStorage = new LocalStorage(context);
        pm = context.getPackageManager();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
              convertView = inflater.inflate(R.layout.applist_item, parent, false);
            // Other lines of code
        if(appName.length()==0 || getItem(position).packageName.equals("package.name.to.skip")) {
              appList.remove(position);
              notifyDataSetChanged();
        }
    return convertView;
}

Lines of Concern

appList.remove(position);
notifyDataSetChanged();

Your suggestions and corrections are always welcome.
AzmatHunzai

like image 30
Azmat Karim Khan Avatar answered Nov 16 '22 01:11

Azmat Karim Khan