这是给出错误的代码
DataSet Music = new DataSet();
Music= DBI.musicall();
comboBox1.DisplayMember= "music1.name"; // with .
comboBox1.ValueMember="Id";
comboBox1.DataSource=Music.Tables[0];
Run Code Online (Sandbox Code Playgroud)
这是正在运行的代码.
DataSet Music = new DataSet();
Music= DBI.musicall();
comboBox1.DisplayMember= "music1name"; // no point -- no .
comboBox1.ValueMember="Id";
comboBox1.DataSource=Music.Tables[0];
public static DataSet musicall()
{
SqlConnection connect= new SqlConnection(connectway);
string sql = "select *from Music";
SqlCommand command= new SqlCommand();
command.CommandText=sql;
command.Connection=connect;
SqlDataAdapter adaptor = new SqlDataAdapter();
adaptor.SelectCommand=command;
DataSet finalDs= new DataSet();
connect.Open();
adaptor.Fill(finalDs);
connect.Close();
return finalDs;
}
Run Code Online (Sandbox Code Playgroud)
当我在列名中使用点(.)时,为什么会遇到此问题.
为什么当我在列名中使用"."时,组合框会显示错误?
我可以在不使用"."的情况下解决这个问题,但我不想这样解决这个问题.
我只是 kotlin 协程的新手。我刚刚创建了用于测试实时数据的新项目,但我无法观察到数据更改。我不明白 livedata 的概念。什么时候触发?因为当我观察 ROOM 数据库时(不是协程方式。我使用了 MutableLiveData)它工作得很好。每当数据发生变化时,总是会触发观察者。
我只是想清理和现代代码。我的期望:当我单击 btnLogin 按钮时(当用户使用另一个帐户登录时或者您可以说数据更改时)必须触发 livedata。
这是我的例子:
改造界面:
interface RetroMainClient {
@POST("login.php")
suspend fun login(@Body model: UserLoginModel): Response<UserLoginModel>
companion object {
val getApi: RetroMainClient by lazy {
Retrofit.Builder().baseUrl("https://example.com/")
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create())).build()
.create(RetroMainClient::class.java)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的存储库:
class Repository {
suspend fun getLoginApi(model: UserLoginModel) = RetroMainClient.getApi.login(model)
}
Run Code Online (Sandbox Code Playgroud)
我的视图模型:
class MainViewModel : ViewModel() {
fun login(model: UserLoginModel) = liveData(IO) {
try {
emit(Repository().getLoginApi(model))
} catch (e: Exception) {
Log.e("exception", "${e.message}")
}
}
}
Run Code Online (Sandbox Code Playgroud)
和我的主要活动:
class MainActivity : AppCompatActivity() …Run Code Online (Sandbox Code Playgroud) android kotlin android-livedata android-architecture-components kotlin-coroutines
我已经使用数据存储很长时间了。今天我必须读取主线程中的值。查看文档后,我决定使用runblocking。我创建了一个长值,其名称为lastInsertedId。
我在片段 A 中读取了lastInsertedId,然后导航到片段B,并且我正在更改lastInsertedId 的值。当我回到片段 A 时,我再次阅读了lastInsertedId。但lastInsertedId的值仍然相同。实际上它的值正在改变,但我无法读取它的最后一个值。
我认为这是因为碎片A没有被破坏。只有 onDestroyView 是从 onCreateView 调用并创建的。我想要的是只要我想在主线程中访问lastInsertedID 的当前值。
当我将其创建为变量时,它总是返回相同的值。但当我将它转换为函数时,它运行良好。但我不认为这是最佳实践。访问此值的最佳方式是什么?谢谢。
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "main")
@Singleton
class DataStoreManager @Inject constructor(@ApplicationContext appContext: Context) {
private val mainDataStore = appContext.dataStore
suspend fun setLastInsertedId(lastId: Long) {
mainDataStore.edit { main ->
main[LAST_INSERTED_ID] = lastId
}
}
// Returns always the same value
val lastInsertedId: Long = runBlocking {
mainDataStore.data.map { preferences ->
preferences[LAST_INSERTED_ID] ?: 0
}.first()
}
// Returns as expected
fun lastInsertedId(): Long = …Run Code Online (Sandbox Code Playgroud)