Tag: 安卓

EditText失去了重点TextTextChange

我正在使用Anvil和Kotlin构建的Android应用程序,目前我已经有一个RecyclerView呈现应该由用户编辑的键值对的列表。 为了保存这些编辑,我添加了一个onTextChanged监听器,如下所示: textInputLayout { size(MATCH, WRAP) /* ommitted style attributes */ editText { size(MATCH, WRAP) /* ommitted style attributes */ init { text(getPropertyValue(item)) onTextChanged { onPropertyChange(item.first, it.toString()) } } } } 在上面的例子中, item是Pair<String, Property>类型Pair<String, Property>其中Property是一个自定义对象,提供有关格式类型(字符串,日期,数字等)的信息, getPropertyValue调用具有签名getPropertyValue(item: Pair<String, Property>): String 。 出于某种原因,每次我输入文本字段看到没有变化,但失去了重点。 onTextChanged监听器肯定是被调用的,因为当它执行时,我可以看到由onPropertyChange生成的更新值被记录到控制台。 我已经尝试了这里列出的解决方案,但没有效果。 有没有人有任何其他的建议? 一直卡在这一段时间了。

Kotlin中的房间持久性库实现(Gradle错误)

我在kotlin中为我的数据库实现实现了Room persistence lib。 在Android Room Persistences库和Kotlin线程中提出了同样的问题,应用这些解决方案导致了不同的gradle错误: 以下是我的Entity , Dao和Database类: Food.kt @Entity class Food(@ColumnInfo(name = "food_name") var foodName: String, @ColumnInfo(name = "food_desc") var foodDesc: String, @ColumnInfo(name = "protein") var protein: Double, @ColumnInfo(name = "carbs") var carbs: Double, @ColumnInfo(name = "fat") var fat: Double) { @ColumnInfo(name = "id") @PrimaryKey(autoGenerate = true) var id: Long = 0 @ColumnInfo(name […]

如何调用Kotlin中的ArrayAdapter.addAll方法?

我有一个非常简单的列表(所以没有必要创建一个扩展BaseAdapter的自定义适配器)。 我使用ArrayApdater来实现这一点。 当我想改变适配器的值时,有一个构建错误说有两个allAll方法,kotlin不知道要使用哪一个。 以下是示例代码: val list = Collections.emptyList<Any>() // Just for sample. val adapter = ArrayAdapter<Any>(this, android.R.layout.simple_spinner_item, list) // This line is OK // Some work later adapter.clear() adapter.addAll(list) // Here's the error 错误消息是: Error:(79, 35) Overload resolution ambiguity: public open fun addAll(p0: (MutableCollection<out Any!>..Collection<Any!>?)): Unit defined in android.widget.ArrayAdapter public open fun addAll(vararg p0: Any!): […]

MediaStore.Images从拇指Uri / id获取完整图像

关于如何从整个图像中获取缩略图,有几个解决方案,比如 android获取存储在路径已知的SD卡上的图像的缩略图 但是, 我需要相反,从缩略图Uri(或缩略图ID)接收完整的图像Uri 。 以下是我如何获得缩略图: fun getGalleryImages(): List<LocalImage> { val baseUri: Uri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI val listOfAllImages = ArrayList<LocalImage>() // Set up an array of the Thumbnail Image ID column we want val projection = arrayOf(MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID) // Create the cursor pointing to the SDCard val cursor = context.contentResolver.query( baseUri, projection, null, null, null) // […]

图像视图不会出现在Activity上(在AppCompatActivity上可以)

我有简单的应用程序,它有一个ImageView和文本框 首先,我从AppCompactActivity开始,它工作。 但是,当我将AppCompatActivity更改为活动。 (我想删除标题栏) ImageView消失了(Droid)。 我想我有ImageView的约束。 我如何显示ImageView? package jp.virararara.antbuid.bb; //import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.app.Activity //class TopActivity : AppCompatActivity() { class TopActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_top) } } activity_top.xml <ImageView android:id="@+id/imageView3" android:layout_width="105dp" android:layout_height="208dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@+id/editText2" app:srcCompat="@mipmap/ic_launcher" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="16dp" app:layout_constraintHorizontal_bias="0.0" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="153dp" android:layout_marginTop="132dp" android:ems="10" […]

房间Kotlin:实体和Pojos必须有一个可用的公共构造函数

当我运行我的应用程序时,我得到了跟进错误。 Error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). Tried the following constructors but they failed to match: ChatMsg(java.lang.String,int) : [arg0 : null, arg1 : null] Error: Entities and Pojos must have a usable public […]

Android Kotlin类实现的问题

当我想从sharedpreferences得到int值时,我得到了UnsupportedOperationException但是我从logcat显示,这个类是Int 。 怎么了? operator inline fun <reified T : Any> get(@XMLS xml: String, @KEYS key: String, defaultValue: T? = null): T { Timber.d("${T::class} + $xml + $key + $defaultValue") return when (T::class) { String::class -> getShared(xml)?.getString(key, defaultValue as? String ?: "") as? T ?: "" as T Int::class -> { Timber.d("not triggered") //<< getShared(xml)?.getInt(key, defaultValue […]

只有最后一个元素添加到RecyclerView Android(Kotlin)

我将7个物品的清单传递给recyclerview,它只是绑定最后一个。 通过使用日志我cal看到getItemCount()返回7,但onBindViewHolder只被调用一次。 我知道(再次通过日志记录),我传递给适配器的列表是完整的列表。 你能帮我找出为什么只显示最后一个元素,而不是整个列表。 谢谢。 活动代码: class HabitActivity : AppCompatActivity() { var adapter: ListHabitsAdapter? = null private var habitList: ArrayList<Habit>? = null private var habitListItems: ArrayList<Habit>? = null private var layoutManager: RecyclerView.LayoutManager? = null var dbHandler:HabitDataAdapter? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_habit) // Initialize the Class global variables //val mContext = applicationContext […]

Kotlin没有拿起资源ID

我正在尝试使用Kotlin开发一个应用程序。 我通过直接使用Kotlin扩展引用id来了解不使用findViewById的可能性。 我已经适当的插件与: apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' 在我的build.gradle 但是,当我尝试引用: kotlinx.android.synthetic.main.item_event.view.event_cover Android Studio只能识别视图部分,也就是说 kotlinx.android.synthetic.main.item_event.view 这意味着我可以使用Ctrl + Space工作。 但是在“查看”后,我没有任何建议,如果我把这个ID,那么它说无法识别的引用。 我究竟做错了什么? 为了补充这一点,这个过去一直工作,直到我重构了我的类,并将其移动到另一个包。

单位转换公里到英里不会返回预期的结果

我试图把我的结果从公里转换成英里,但是我得到的结果是不正确的。 这里似乎是什么问题? when { // TIME: To calculate your time, fill in your distance and pace time == null -> { val calculatedTime = distance!!.toLong() * timeToSeconds(pace.toString()) result.text = "The runner's time is ${secondsToTime(calculatedTime)}" } // DISTANCE: To calculate your distance, fill in your time and pace distance == null -> { val calculatedDistance = […]