I have written a ContentProvider
which returns data from multiple tables (parent and child) using join. The result is a Cursor
that contains one row for each child table row that was retrieved. For eg. If there are 2 rows in my child table for one row in parent table, the cursor has 2 rows with duplicated values for parent columns and unique values for child table.
While using this in ListView
, I want to show one ListView
item per parent i.e. per two records.
I have written a custom adaptor extending from CursorAdaptor
to do so, where I have overridden getView
, newView
and bindView
methods as follows -
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (!mDataValid) {
throw new IllegalStateException("this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
View v;
if (convertView == null) {
//If the lookup value _id is same for this record don't create new view
//instead only bind view and set the data from child table into existing view
String lookupValue = mCursor.getString(mCursor.getColumnIndex(mLookupColumn));
v = parent.findViewWithTag(lookupValue);
if(v == null){
v = newView(mContext, mCursor, parent);
v.setTag(lookupValue);
}
} else {
v = convertView;
}
this.bindView(v, mContext, mCursor);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
mAdapterView.setData(context, convertToDomainObject(cursor), view);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(mLayout, parent, false);
}
However, the result of this is weird, I get blank views at alternate positions which is understandable (because my getView returns null) but this also is not consistent.
I have been struggling with this for quite some time. Would really appreciate any help.
Note - using distinct, group by doesn't help because I want to fetch both records and use them only map them to one row causing *:1 mapping between records and list items.