我有一个价格检查器应用程序,它使用其条形码请求产品的详细信息。很简单. .我只在第一页。
我创建了UI和SQL查询,我可以从服务器获取数据,但是,我不能更新UI。我肯定有什么地方做得不对。但是我还在学习Kotlin,所以请对我有耐心。
请求和显示数据的函数是这个
@Composable
fun PriceChecker(viewModel: MainViewModel,context: Context) {
var barcode by remember {
mutableStateOf("")
}
val focusManager = LocalFocusManager.current
Box(modifier = Modifier.size(250.dp)) {
Column(
modifier = Modifier
.size(500.dp)
.border(width = 2.dp, color = Color.White, shape = RoundedCornerShape(12.dp))
) {
TextField(
onValueChange = {
barcode = it
},
value = barcode,
singleLine = true,
keyboardActions = KeyboardActions(onDone = ({
//hidekeyboard
focusManager.clearFocus()
//get product details
viewModel.getDetails(barcode) <-- this
//erasing text after sending barcode
barcode = ""
})),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
)
Text(text = viewModel.name) <-- needed value from SQLthrough ViewModel
Text(text = viewModel.price)<-- needed value from SQL through ViewModel
}
}
}
在视图模型中,我有这个简单的
class MainViewModel:ViewModel() {
var price= RepositorySQL().price
var name = RepositorySQL().name
fun getDetails(barcode:String){
RepositorySQL().getProductDetails(barcode)
}
}
我试图使用mutablestateof()
的值,但它也没有更新UI
在Sql连接器中,我这样做:
var price by mutableStateOf("Product Name") //also var price = ""
private set
var name by mutableStateOf("Product Price") //also var name = ""
private set
val cls ="net.sourceforge.jtds.jdbc.Driver"
var connection: Connection? = null
fun getProductDetails(barcode: String){
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
Class.forName(cls)
connection = DriverManager.getConnection(newUrl, newUser, newPass)
if (connection!=null){
val statement = connection!!.createStatement()
val result = statement.executeQuery("Select [ItemName],[Price] FROM [twisted].[dbo].[Inventory] WHERE [ItemNum] = '$barcode';")
while (result.next()){
name = result.getString("ItemName")
price = result.getDouble("Price").toString()
Log.d(TAG, "Product name = ${name}") //values are printed correctly.
Log.d(TAG, "Product Price = ${price}")//values are printed correctly.
}
}else{
name = "Didn't get any data"
Log.d(TAG, name)
}
}
UI保持不变,即使我更新值…我相信解决办法应该很容易,但我还在学习中……
由于某些原因你在每一行都创建了RepositorySQL()
,这将是所有不同的对象
在您的RepositorySQL
中,您使用mutableStateOf
与委托。这使得MainViewModel
中的price
和name
变成了纯字符串,而不是状态,所以它们不会观察RepositorySQL
的值
要使MutableState
为private set
,必须声明私有MutableState
和公共State
,如下所示:
class RepositorySQL {
private val _price = mutableStateOf("Product Name")
val price: State<String> = _price
private val _name = mutableStateOf("Product Price")
val name: State<String> = _name
fun getProductDetails(t: String) {
_price.value = "1"
_name.value = "2"
}
}
在MainViewModel
你可以声明你的值作为委托给RepositorySQL
:
class MainViewModel:ViewModel() {
private val repositorySQL = RepositorySQL()
val price by repositorySQL.price
val name by repositorySQL.name
fun getDetails(barcode:String){
repositorySQL.getProductDetails(barcode)
}
}