我有2个光标,光标1和光标2.光标适配器已用于显示光标1的结果。但是,我想为每5个光标2个项目插入光标2的项目。我怎样才能做到这一点?
目前我已经为光标1的列表视图扩展了CursorAdapter。现在我想将光标2项插入光标1中,每5个光标1项。
我看过MatrixCursor和MergeCursor,他们会合并并添加行,但我想插入其间。任何想法我们怎么能这样做?
答案 0 :(得分:0)
从您的解释中了解您想要达到的目标并不容易。 您应该扩展 BaseAdapter 类,因为它更容易适应您的需求。 这应该适合你。
public class CursorAdapterTwo extends BaseAdapter{
private final Context context;
LayoutInflater layoutInflater;
String[] dataTypes;
Cursor cursor1;
Cursor cursor2;
public CursorAdapterTwo(Context context, Cursor cursor1, Cursor cursor2) {
this.context = context;
this.cursor1 = cursor1;
this.cursor2 = cursor2;
layoutInflater = LayoutInflater.from(context);
}
static class ViewHolder {
TextView textView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
cursor1.moveToPosition(position);
if((position % 5) == 0)
{
cursor2.moveToPosition(position / 5);
}
if (convertView == null) {
convertView = layoutInflater.inflate(layout.list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) convertView.findViewById(id.text_view);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String columnType = dataTypes[position];
viewHolder.textView.setText("New text");
return convertView;
}
@Override
public int getCount() {
if (cursor1 != null) {
return cursor1.getCount() + cursor2.getCount();
} else {
return 0;
}
}
@Override
public Object getItem(int position) {
if (cursor1 != null) {
cursor1.moveToPosition(position);
return cursor1;
} else {
return null;
}
}
@Override
public long getItemId(int position) {
return position;
}
}