Tag: kotlin android extensions

将AutoCompleteTextView中的Observable更改为EditText

我正在尝试使用Kotlin和RxJava编辑一些预先存在的代码,以便为用户提供更好的UI布局。 初始ui使用AutoCompleteTextEdit将用户的Google Places结果显示为用户类型。 我试图改变它,所以用户键入,结果显示在输入字段下的ListView而不是附加到输入字段。 我的问题是,我只能得到数据显示在列表视图中,如果我离开当前的代码,以便它也显示在AutoCompleteTextView由于代码是为observable和自定义RxFilterableArrayAdapter编写的方式。 有了这个代码,搜索工作,但显示在下拉菜单,以及列表视图。 (见下文) private fun setupView(view:View) { val tripPlanningResult = view.findViewById(R.id.trip_planning_api_results) as ListView val starting_location = view.findViewById(R.id.trip_planning_starting_location) as AutoCompleteTextView val destination_address = view.findViewById(R.id.trip_planning_destination_address) as AutoCompleteTextView val plan_route_button = view.findViewById(R.id.trip_planning_plan_route_button) val tripPlanningService = TripPlanningService.sharedTripPlanningService(context) val cityBounds0 = CityCache.getInstance(context, "cities", true) .getCityRegion(cityId!!) //Creates an Observable to watch for text input from the AutoCompleteTextView […]

如何从单独的AsyncTask修改实例变量

比方说,我有一个活动与实例变量loadedMovie和一个方法,执行另一个文件中的AsyncTask class MainActivity:AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener { var loadedMovie: Movie? = null …. fun loadMovie() { val task = LoadMovieTask(this) task.execute() } } 单独的AsyncTask class LoadMovieTask(val ctx: Activity) : AsyncTask<Void, Void, Void>() { var movie: Movie? = null override fun onPreExecute() { …. } // loading information from network override fun doInBackground(vararg params: Void?): Void? { movie […]

Out-projected type'ArrayList <*>'禁止使用'public open fun add(index:Int,element:E):java.util.ArrayList'

我有这个片段: class RecyclerViewAdapter internal constructor( val clazz: Class<out RecyclerViewViewHolder>, val layout: Int, var dataList: MutableList<*>) … … … fun RecyclerView.getDataList() : ArrayList<*> { return (adapter as RecyclerViewAdapter).dataList as ArrayList<*> } … … … 那么我使用这个: recyclerView.getDataList().add(Person("Lem Adane", "41 years old", 0)) 但我得到这个错误: Error:(19, 31) Out-projected type 'ArrayList<*>' prohibits the use of 'public open fun add(index: Int, […]

用Kotlin在android中声明变量类型的正确方法是什么?

我想声明一些变量类型的gatter和setter模型。 我可以声明简单和干净的代码,如在Java中? private String firstName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } 因为当它转换成Kotlin它看起来像: private var firstName: String? = null fun getfirstName(): String { return firstName } fun setfirstName(firstName: String) { this.firstName = firstName } 什么是正确和最清洁的方式?

如何使用kotlin在android中初始化小部件

我已经开始学习在android中使用kotlin语言,并在初始化我的按钮变量时得到问题,因为在定义我的变量的时候,它要求给予一些初始值,当我用null值初始化,并绑定变量oncreate函数它的给 kotlin.KotlinNullPointerException 这是我的代码 class AddsFragment : Fragment() { var Add: Button = null!! override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { val Rootview = inflater!!.inflate(R.layout.clubsfragment, null, false) Add = Rootview.findViewById(R.id.add) as Button return Rootview } }

更改导航抽屉项目颜色过滤器也改变了使用相同的图像ID的imageView

在应用程序中有导航抽屉包含很多菜单项。 局限性是,有一个项目会显示其色调的颜色实例,而另一个则显示色调。 有了这个限制,我决定使用彩色滤镜来处理物品抽屉的图标颜色阶段。 这条路径工作得很好。 我的菜单看起来像这样 <item android:id="@+id/menu_main" android:icon="@drawable/ic_my_logo_24dp" android:title="@string/app_name" /> <item android:id="@+id/menu_setting" android:icon="@drawable/ic_settings_black_24dp" android:title="@string/text_menu_settings" /> <item android:id="@+id/menu_contact_us" android:icon="@drawable/ic_markunread_black_24dp" android:title="@string/text_menu_contact_us" /> <item android:id="@+id/menu_faq" android:icon="@drawable/ic_faq_black_24dp" android:title="@string/text_menu_faq" /> 每个菜单期望主菜单应该像这样显示颜色状态 选中:蓝色 未选中:灰色 主菜单 选中:显示其颜色 未选中:灰色 处理状态的代码看起来像这样 fun setSelectedMenuItemColor(selectedId: Int) { val menu = binding?.navView?.menu repeat(menu!!.size()) { i -> menu.getItem(i)?.icon?.colorFilter = when { menu.getItem(i).itemId == selectedId && selectedId == R.id.menu_main […]

应用程序的伴随对象中的应用程序上下文和资源的静态引用

我读过一些文章和关于这个问题(主要是上下文而不是资源)的stackoverflow帖子。 很多人都说这是可以的,因为Application类和应用程序上下文应该在应用程序的整个生命周期中一直存在,但是有些人仍然不愿意做这种可能的未知的可能性(这使我很怀疑 – 因此这篇文章)。 另一方面,将Context传递给每个对象的函数是相当艰巨的。 在Java中,解决这个问题的一个办法是做一些类似UtilClass.getInstance(context)事情,但是你不能用Kotlin对象来做这件事,而且我也不太确定用参数实现Kotlin Singletons 。 所以,我有以下代码: class App : MultiDexApplication() { override fun onCreate() { super.onCreate() _resources = resources _context = applicationContext //..other code } companion object { val resources: Resources get() = _resources!! val context: Context get() = _context!! private var _resources: Resources? = null private var _context: Context? = null […]

在Java代码中集成Kotlin?

例如:在C代码中,可以调用部分汇编代码,如: int main() { //… stuff __asm { lea ebx, hal mov ecx, [ebx]hal.same_name ; mov esi, [ebx].weasel ; } // .. further stuff return 0; } Java(* .java)文件中的Kotlin代码是否可以实现这样的代码集成? (我不是在谈论Java中的JNI或C / C ++!)我想用Kotlin语言扩展已经存在的(AndroidStudio-)Java-Source-Code。 //.. this is *.java file public class MyAlreadyExistingJavaClass { private int memberVar; public MyAlreadyExistingJavaClass() { } // this is Kotlin within *.java […]

在java类中调用扩展函数作为任何RX操作符

我创建了一个扩展函数, fun <T> Observable<T>.subscribeWithErrorHandling(onNext: (T) -> Unit ,onError: ((throwable: Throwable) -> Unit)? = null): Subscription { //doing stuff } 在kotlin类中,我将能够以这种方式使用它 observable.subscribeWithErrorHandling(…) 现在,我想在我的java类中使用这个函数。 我已经看到,你可以静态调用它: MyExtensionFile.subscribeWithErrorHandling 但在我的情况下,你需要别的东西,因为它是一个RX流的中间部分。 这是我坚持的部分。 这甚至可能吗? 或没有办法做这样的事情,从Java代码?

Android本地VPN服务:无法获得响应

我对Android及其服务相当陌生。 我试图在我的应用程序(与Kotlin和Java)实现本地 VPN服务。 题 我的VPN服务采取从ToyVpn谷歌的例子,结合从1,2,3的例子来使用它在本地(没有连接到远程服务器)不起作用。 我的APP PRINCIPE 我看到这个和这个问题,但答案不是很有见地,我找不到解决方案。 所以这个应用程序非常简单 :当用户点击主要活动的“是”按钮时,它应该转发所有的数据包,而当点击“否”时 – 阻止它。 目的是: 将其用作防火墙 ,就像这样: 我所有的代码都是用Kotlin语言编写的,但是它并不复杂,对于JAVA开发人员来说非常清楚。 所以我希望上面的代码非常清楚,因为它是从这里获取的 (由Google提供的ToyVpn示例),并转换为kotlin。 我的配置和代码 为了在我的应用程序中启用VPN服务,我将AndroidManifest.xml放入<application>标签中: <service android:name="com.example.username.wifictrl.model.VpnFilter" android:permission="android.permission.BIND_VPN_SERVICE" > <intent-filter> <action android:name="android.net.VpnService" /> </intent-filter> </service> 我的MainActivity代码包含: override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) … // omitted for the sake of brevity val intent = VpnService.prepare(this); if (intent != null) { startActivityForResult(intent, […]