my DB like that. 我搜索了许多页面,视频,但我无法找到一个例子,我无法做到这个例子。 在我的项目中,没有任何添加,删除和更新按钮。我的sqlite数据库中包含所有数据。
在这个程序中,我的主表将列在listview的第一个主要布局上。当我单击一行时,我必须在listview上的相关表上看到其他数据。 (当我点击listview上的客户时,我会在另一个列表视图上看到所有订单。我桌子上的所有数据。)
我希望我解释了我的问题。是否有人会为我提供一个例子。
答案 0 :(得分:0)
正如您已经了解客户,您可以执行一个非常简单的查询来获取可以构建/重新构建ListView的游标。
然后以下将返回这样一个光标: -
public Cursor getCustomerOrders(long customerref) {
return this.getWritableDatabase().query("Orders",null,
"CustomerID=?",
new String[]{Long.toString(customerref)},
null,null,null);
}
customerref 是客户表的 ID 列中的值。
您将使用以下方法调用它(假设SQLiteOpenHelper
子类名为DBHelper
): -
Cursor orderlist = getCustomerOrders(clicked_customer);
public class MainActivity extends AppCompatActivity {
ListView customerlist;
ListView orderlist;
Cursor customers;
Cursor orders;
SimpleCursorAdapter sca_customers;
SimpleCursorAdapter sca_orders;
DBHelper dbh = new DBHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customerlist = (ListView) findViewById(R.id.customerlist);
orderlist = (ListView) findViewById(R.id.orderlist);
//Create some randomish data for 3 Customers
Random rnd = new Random(System.currentTimeMillis());
Cursor chkforempty = dbh.getWritableDatabase().query(DBHelper.CUSTOMERSTABLE,null,null,null,null,null,null);
boolean chk = (chkforempty.getCount() > 0);
chkforempty.close();
if (!chk) {
for (int i=0; i < 3; i++) {
String suffix = Integer.toString(i);
dbh.insertCustomer("Compnay" + suffix,"Name " + suffix, "Last Name " + suffix);
int randomorders = rnd.nextInt(10) + 1;
for (int i2=0; (i2 < randomorders) || (i2 < 10); i2++ ) {
dbh.insertOrder((long) i,"Destination " + Integer.toString(i2));
}
}
}
//Back to the real code
customers = dbh.getCustomers();
orders = dbh.getCustomerOrders(-1); // get none at first
sca_customers = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
customers,
new String[]{DBHelper.CUSTOMER_COMPANY_COL},
new int[]{android.R.id.text1},0);
sca_orders = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
orders,
new String[]{DBHelper.ORDER_DESTINATION_COL},
new int[]{android.R.id.text1},0);
customerlist.setAdapter(sca_customers);
orderlist.setAdapter(sca_orders);
customerlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
orders = dbh.getCustomerOrders(l);
sca_orders.swapCursor(orders);
}
});
}
}
public class DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydatabase";
private static final int DBVERSION = 1;
public static final String CUSTOMERSTABLE = "customers";
public static final String ORDERSTABLE = "orders";
public static final String CUSTOMER_ID_COL = "_id";
public static final String CUSTOMER_COMPANY_COL = "company";
public static final String CUSTOMER_NAME_COL = "name";
public static final String CUSTOMER_LASTNAME_COL = "lastname";
private static final String CUSTOMERSTABLE_CREATESQL =
"CREATE TABLE " + CUSTOMERSTABLE +
"(" +
CUSTOMER_ID_COL + " INTEGER PRIMARY KEY, " +
CUSTOMER_COMPANY_COL + " TEXT, " +
CUSTOMER_NAME_COL + " TEXT, " +
CUSTOMER_LASTNAME_COL + " TEXT " +
")";
public static final String ORDER_ID_COL = "_id";
public static final String ORDER_CUSTOMERREF_COL = "customerid";
public static final String ORDER_DATE_COL = "date";
public static final String ORDER_DESTINATION_COL = "destination";
private static final String ORDERSTABLE_SCREATSQL =
"CREATE TABLE " + ORDERSTABLE +
"(" +
ORDER_ID_COL + " INTEGER PRIMARY KEY, " +
ORDER_CUSTOMERREF_COL + " INTEGER, " +
ORDER_DATE_COL + " LONG DEFAULT CURRENT_TIMESTAMP, " +
ORDER_DESTINATION_COL + " TEXT " +
")";
DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CUSTOMERSTABLE_CREATESQL);
db.execSQL(ORDERSTABLE_SCREATSQL);
}
@Override
public void onUpgrade(SQLiteDatabase db,
int oldversion,
int newversion) {
}
public long insertCustomer(String company, String name, String lastname) {
ContentValues cv = new ContentValues();
cv.put(CUSTOMER_COMPANY_COL,company);
cv.put(CUSTOMER_NAME_COL,name);
cv.put(CUSTOMER_LASTNAME_COL,lastname);
return this.getWritableDatabase().insert(CUSTOMERSTABLE,null,cv);
}
public long insertOrder(long customerid, String destination) {
ContentValues cv = new ContentValues();
cv.put(ORDER_CUSTOMERREF_COL,customerid);
cv.put(ORDER_DESTINATION_COL,destination);
return this.getWritableDatabase().insert(ORDERSTABLE,null,cv);
}
public Cursor getCustomers() {
return this.getWritableDatabase().query(CUSTOMERSTABLE,
null,null,null,null,null,null);
}
public Cursor getCustomerOrders(long customerref) {
return this.getWritableDatabase().query(ORDERSTABLE,null,
ORDER_CUSTOMERREF_COL+"=?",
new String[]{Long.toString(customerref)},
null,null,null);
}
}
请注意,已使用适合自己的名称。这只是针对非常基本的功能进行了测试。
最初运行时显示: -
点击Compnay1会导致: -