android - bindView and newView for two view layout in CursorAdapter
Asked Answered
P

1

3

So here's the story:

I want to use two layouts in my adapter. So basically, I need to have an if in the newView() to determine which view to return and and an if in bindView() to know as well what to do in the view. Is this the right approach?

I'm thinking of something like this:

@Override
public View newView(Context context, Cursor c,
        ViewGroup parent) {     
    if (HEADER == getItemViewType(c.getPosition())){
        return (View) layoutInflater.inflate(R.layout.my_header, null);
    } else {
        return (View) layoutInflater.inflate(R.layout.my_row, null);
    }
}

Then on bindView:

@Override
public void bindView(final View view, final Context context,
        Cursor c) {     
    if (TYPE_HEADER == getItemViewType(c.getPosition())){
        // init and set values here e.g. view.findViewById().setText()
    } else {
        // init and set values here e.g. view.findViewById().setText()
    }
}

Am I on the right track here? Because according to my logs, the c.getPosition() in newView gives different result on c.getPosition() in bindView. I'm actually thinking of just overriding the getView() but they said good practice is overriding newView and bindView in CursorAdapter.

Panettone answered 28/1, 2013 at 7:19 Comment(4)
To be honest, I can't answer you question, and I would try this solution, too. However, I'm wondering whether your databse layout is "good". It seems that you store the table's header and the table's content in the same table. That's not how one should use a database.Jecoa
@Jecoa , my header is actually is for alphabet headers that I plan to insert on my list. My headers are not stored in db.Panettone
This is a great question. I am in a very similar situation. I want to make the first row a different view, but checking cursor.getPosition() == 0 causes multiple rows to contain the special view. I am guessing this is due to how Cursor adapter tries to be more efficient, just grabbing small groups of data at a time.Rule
It sounds to me like an ExpandableListView is closer to what you want. Putting "placeholder" rows in your db for alphabetical sorting sounds like very poor database design to me.Fascinate
F
0

Aside from my comment (on the question) about poor database design...

I would make one layout that wraps both of the header and content layouts, then mark the header type of the two layouts as visiblity="gone". For both bindView and newView rows where you need the header subview, make it View.VISIBLE and the content subview View.GONE. Of course when you want the content layout swap them.

As per my comment, you probably want ExpandableListView. I don't have statistics or measurements on it, but I suspect that the performance will be better. It will also mean that you aren't relying on the hacky solution of jamming "headers" into the same table as your data.

Fascinate answered 17/11, 2013 at 21:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.