我有一个CursorAdapter
从数据库构建的Cursor
,如果View
第一个项目中的 a 未填写,它会使用设置了的第一个后续列表项中View
出现的任何数据填充第一个项目的 s 。此外,我发现每个列表项都会触发 3 次,这似乎很奇怪。它只对第一个列表项执行此操作,而不是所有带有 null的项目。这是它的样子:View
View
onBindView
View
请注意,“Julian”的地址已复制到“Alice”。每当我在 onBindView 方法中设置断点时,我可以看到当 customerName 为“Alice Martin”时,if(billingAddressCursor!=null && billingAddressCursor.getCount()>0)
对于所有三个调用,总是评估为 false onBindView
,所以我知道它永远不会进入计费条件。
为什么它在第一个位置绘制错误的值,我该如何阻止它?代码:
private class CustomerAdapter extends CursorAdapter {
/*** DEBUG CODE, TO BE REMOVED ***/
int aliceBindCount = 0;
int blakeBindCount = 0;
int cscBindCount = 0;
int julianBindCount = 0;
/*** ^^^^^^^^^^^^^^^^^^^^^^^^^ ***/
public CustomerAdapter(Context context, Cursor cursor) {
super(context, cursor);
}
public void bindView(View convertView, Context context, Cursor cursor) {
if(convertView!=null) {
String customerName = cursor.getString(cursor.getColumnIndex(CustomerSchema.NAME));
((TextView)convertView.findViewById(R.id.cli_customer_name)).setText(customerName);
/*** DEBUG CODE, TO BE REMOVED ***/
if(customerName.equals("Alice Martin")) {
aliceBindCount += 1;
} else if(customerName.equals("Blake Slappey")) {
blakeBindCount += 1;
} else if(customerName.equals("Conceptual Systems")) {
cscBindCount += 1;
} else if(customerName.equals("Julian")) {
julianBindCount += 1;
}
/*** ^^^^^^^^^^^^^^^^^^^^^^^^^ ***/
}
final Long billingAddressID = cursor.getLong(cursor.getColumnIndex(CustomerSchema.BILLING_ADDRESS_ID));
Cursor billingAddressCursor = DbDesc.getInstance().getDatabase(getActivity()).query(
LocationSchema.TABLE_NAME,
null,
LocationSchema._ID+"=?",
new String[]{ String.valueOf(billingAddressID) },
null,
null,
null
);
if(billingAddressCursor!=null && billingAddressCursor.getCount()>0) {
billingAddressCursor.moveToFirst();
String street = billingAddressCursor.getString(billingAddressCursor.getColumnIndex(LocationSchema.STREET));
String city = billingAddressCursor.getString(billingAddressCursor.getColumnIndex(LocationSchema.CITY));
String state = billingAddressCursor.getString(billingAddressCursor.getColumnIndex(LocationSchema.STATE));
String zip = billingAddressCursor.getString(billingAddressCursor.getColumnIndex(LocationSchema.ZIP));
if(zip==null || zip.equals("")) {
zip = "[NO ZIP]";
}
if(street==null || street.equals("")) {
street = "[NO STREET]";
}
if(city==null || city.equals("")) {
city = "[NO CITY]";
}
if(state==null || state.equals("")) {
state = "[NO STATE]";
}
((TextView)convertView.findViewById(R.id.cli_street)).setText(street);
((TextView)convertView.findViewById(R.id.cli_city_state_zip)).setText(city+", "+state+" "+zip);
}
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.customer_list_item, parent, false);
return v;
}
}
而且我确信 Alice 不应该有与该记录相关联的帐单地址。这是来自 sqlite3 的表查询。唯一带有帐单地址 ID 的记录是 Julian 的记录,如您所见:
sqlite> .tables
.tables
android_metadata contract location
contact customer phone
sqlite> select * from customer;
select * from customer;
2|Conceptual Systems|||||||
3|Blake Slappey|||||||
4|Julian|1||||||
5|Alice Martin|||||||