Skip a row in list view
Asked Answered
L

2

10

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.

Literature answered 17/6, 2012 at 11:8 Comment(5)
why dont You filter the person before sending it to listview?Intelsat
Yea i have done that..but I want to know is there a way we can do this inside getView()..?Literature
you can not. You have to filter values before you submit your adapter to the listviewLepley
try setting convertview as null when u return the viewIntelsat
@Raghav: U got the answer juss show wat will be the last line of code?Intelsat
D
25

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);
Dyann answered 17/6, 2012 at 11:12 Comment(14)
I dont think so.coz each row in list view has same id so it will set the visibility of all above rows to gone.Literature
That's not correct - you just have to check for each row if the content is null or not and set the visibility to visible or gone.Dyann
You're welcome :) We're using that in our own projects, too :)Dyann
Can You tell me ...what is notifyDataSetChanged() for..?Willl it be applicable here.?Literature
notifyDataSetChanged() is used for telling the adapter that the views should be renewed (per example if the data in the adapter dynamically changes).Dyann
@TimMesserschmidt :Please share what will be the last line of code?Intelsat
if((list.get(position).get("alerts")).isEmpty()){ holder.txtFirst.setVisibility(View.GONE); } else holder.txtFirst.setText(map.get("firstName").toString() +"_____"+ map.get("lastName").toString());Literature
I've added a possible last line before returning the View.Dyann
@TimMesserschmidt :The that row will become invisible or some other items will show?Intelsat
Your code would only set the TextView's visibility to gone - my code would set the parent's View's visibility to gone. Furthermore I'd avoid checking for isEmpty() and I'd do it myself via == null || .equals("");Dyann
Hey can u givee me an exmaple showing the working of notifyDataSetChanged()..?Literature
Check this topic at Stackoverflow: #3669825Dyann
i think this is not the best solution for this purpose. just try to set your specific condition and increase your position number in getView.Antacid
Tried this solution. It creates blank places in the listArchaean
H
1

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

Heptagonal answered 30/7, 2016 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.