【表结构】【1】【1】:https://i.stack.imgur.com/b0cj3.jpg 我有这两个表,希望使用contentprovider检索事务的表数据(这个db需要共享给其他应用程序)。我也为这些实现了代码。给你
case TRANSACTION:
SQLiteQueryBuilder sqLiteQueryBuilder = new SQLiteQueryBuilder();
sqLiteQueryBuilder.setTables("TransactionTable INNER JOIN Products ON TransactionTable.product_id_in_product_table = Products._ID");
HashMap<String,String> columnprojection = new HashMap<String,String>();
columnprojection.put(myTransactionEntry._ID , myTransactionEntry.TABLE_NAME + "." + myTransactionEntry._ID + "AS" + "TID" );
columnprojection.put("PRODUCT NAME ", myDbEntry.TABLE_NAME + "." +myDbEntry.COLUMN_PRODUCT_NAME + " AS " + "PNAME");
columnprojection.put(myTransactionEntry.COLUMN_PRODUCT_QUANTITY_CHANGED,myTransactionEntry.TABLE_NAME + "." +myTransactionEntry.COLUMN_PRODUCT_QUANTITY_CHANGED + " AS " + "QTY");
columnprojection.put(myTransactionEntry.COLUMN_TRANSACTION_TYPE,myTransactionEntry.TABLE_NAME + "." +myTransactionEntry.COLUMN_TRANSACTION_TYPE + " AS " + "TTYPE");
columnprojection.put(myTransactionEntry.COLUMN_TRANSACTION_DATE,myTransactionEntry.TABLE_NAME + "." +myTransactionEntry.COLUMN_TRANSACTION_DATE + " AS " + "TDATE");
columnprojection.put(myTransactionEntry.COLUMN_ANYREMARK,myTransactionEntry.TABLE_NAME + "." +myTransactionEntry.COLUMN_ANYREMARK+ " AS " + "TREMARK");
sqLiteQueryBuilder.setProjectionMap(columnprojection);
c = sqLiteQueryBuilder.query(db,projection,selection,selectionArgs,null,null,null);
break;
但是我在logcat中出错了
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.balaji/com.example.balaji.DashBoard}:
java.lang.IllegalArgumentException: Invalid column product_id_in_product_table
我到底怎么了?
my CustomCursorAdatper's coding here
mytransid.setText( String.valueOf(cursor.getInt(cursor.getColumnIndex("TID"))));
myproductid.setText(cursor.getString(cursor.getColumnIndex("PNAME")));
myproductquantity_changed.setText(String.valueOf(cursor.getInt(cursor.getColumnIndex("QTY"))));
myproduct_type_of_trns.setText(String.valueOf(cursor.getString(cursor.getColumnIndex(("TTYPE")))));
myproduct_timestamp.setText(String.valueOf(cursor.getInt(cursor.getColumnIndex(("TDATE")))));
alos,列“pname”在cursor.getcolumnindex(“pname”)行中返回-1;我不知道为什么?为什么不包括在投影名称中?
1条答案
按热度按时间zed5wv101#
我知道答案了。我必须在投影图的产品表中包含产品id,因为每个列都需要从transactiontable中显示出来。在适配器中,getcolumnindex(“pname”)返回-1,因为在sqlitebuilder的查询方法中,我使用的是投影数组,而不是将其设置为null。谢谢这个家伙。我了解了setprojectionmap()的确切含义[https://blog.mousetech.com/android-projection-maps-explained/]