I have question
android studio kotlin
unresolved reference : .append
What is the solution?
thank you
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
var a: String = ""
var list = mutableListOf<String>()
var b: Long = 0L
var c: Long = 1L
var d: Double = 1.0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
num1.setOnClickListener {
all_result.setText(all_result.getText().append("1"))
a = all_result.getText().toString()
}
}
}
It seems that all_result in your case is EditText. In this case getText() returns Editable, but not String or StringBuilder. So, you should write something like this: "${all_result.getText().toString()}1" to concatenate existing string in EditText with '1'
Related
I have an adapter for my RecyclerView where I program that when I click on the element (of my RecyclerView) it executes an Intent with a putExtra to take me to another activity, the variable that contains my putExtra comes from the element that I clicked, but now I need to add a More variable that comes from the activity. The issue is that I don't know how to send it from the adapter.
this is my adapter.
package com.example.atipicoapp
import android.app.Activity
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.view.menu.ActionMenuItemView
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.list_item.view.*
class MyAdapter(private val platoList : ArrayList<Plato>
) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyAdapter.MyViewHolder {
val itemView =
LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false)
itemView.platoTouch.setOnClickListener(View.OnClickListener { v: View ->
})
return MyViewHolder(itemView)
}
override fun onBindViewHolder(holder: MyAdapter.MyViewHolder, position: Int) {
val plato: Plato = platoList[position]
holder.platoName.text = plato.platoName
holder.platoDescription.text = plato.platoDescription
holder.platoPrecio.text = plato.platoPrecio.toString()
holder.platoCantidad.text = plato.platoCantidad.toString()
when(holder){
is MyViewHolder -> {
holder.bind(platoList[position])
}
}
}
override fun getItemCount(): Int {
return platoList.size
}
public class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val platoName: TextView = itemView.findViewById(R.id.platoNombre)
val platoDescription: TextView = itemView.findViewById(R.id.platoDescripcion)
val platoPrecio: TextView = itemView.findViewById(R.id.platoPrecio)
val platoTouch: LinearLayout = itemView.findViewById(R.id.platoTouch)
val platoCantidad: TextView = itemView.findViewById(R.id.platoCant)
private val mActivity = itemView.context as Activity
private val intent = Intent(mActivity,SlotActivity::class.java)
fun bind(plato: Plato){
platoTouch.setOnClickListener{
intent.putExtra("id", platoName.text.toString())
mActivity.startActivity(intent)
}
}
}
}
And this is my Activity which contains my RecyclerView and the variable I want to send.
package com.example.atipicoapp
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.firebase.firestore.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_menu_atipico.*
class MenuAtipicoActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var platoArrayList: ArrayList<Plato>
private lateinit var myAdapter: MyAdapter
private lateinit var db: FirebaseFirestore
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_menu_atipico)
recyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.setHasFixedSize(true)
platoArrayList = arrayListOf()
myAdapter = MyAdapter(platoArrayList)
recyclerView.adapter = myAdapter
pedidoId = intent.extras?.getString("pedidoId") //This is the variable I need to send
EventChangeListener()
Setup()
}
private fun EventChangeListener() {
db = FirebaseFirestore.getInstance()
db.collection("Platos").addSnapshotListener(object : EventListener<QuerySnapshot> {
override fun onEvent(
value: QuerySnapshot?,
error: FirebaseFirestoreException?
) {
if (error != null) {
Log.e("Firestore Error", error.message.toString())
return
}
for (dc: DocumentChange in value?.documentChanges!!) {
if (dc.type == DocumentChange.Type.ADDED) {
platoArrayList.add(dc.document.toObject(Plato::class.java))
}
}
myAdapter.notifyDataSetChanged()
}
})
}
private fun Setup() {
botonAceptar.setOnClickListener {
val SlotIntent = Intent(this, SlotActivity::class.java).apply {
}
startActivity(SlotIntent)
}
}
}
How can I send the variable if the Intent is executed from the Adapter?
Or... If it is not recommended to send intent from my Adapter, how can I send them from the activity?
Knowing that I have to carry a variable that is in the item of the RecyclerView.
Thanks for your help <3
Firstly, create a MyAdapter constructor where you pass arrayList as well as pedidoId like, your MyAdapter should be something like below:
MyAdapter.class
class MyAdapter(private val platoList : ArrayList<Plato>, val pedidoId:String
) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
........
.....
....
//in your bind(..) method
fun bind(plato: Plato){
platoTouch.setOnClickListener{
intent.putExtra("id", platoName.text.toString())
intent.putExtra("pedidoId", pedidoId)
mActivity.startActivity(intent)
}
}
}
And, in your MenuAtipicoActivity you need to do something like:
MenuAtipicoActivity
class MenuAtipicoActivity : AppCompatActivity() {
...............
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_menu_atipico)
recyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.setHasFixedSize(true)
platoArrayList = arrayListOf()
pedidoId = intent.extras?.getString("pedidoId") //This is the variable I need to send
myAdapter = MyAdapter(platoArrayList,pedidoId)
recyclerView.adapter = myAdapter
EventChangeListener()
Setup()
}
..........
........
}
I try to build a speechrecognition in Kotlin.
Most of my code is cut an paste from internet. Nevertheless I fail.
I have a simple android.xml with 1 textview, 1 editview and 1 button with "onclick" = mybuttondo.
my Kotlin Code:
package com.example.mytestspeech
import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.speech.RecognizerIntent
import android.view.ActionMode
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
import kotlin.collections.ArrayList
class MainActivity : AppCompatActivity() {
val SPEECHINTENTRQ = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView.text = "newworld"
}
fun mybuttondo(view: View) {
// only my test for ListOf<String>
var mytext = "mystring"
var myarr = listOf<String>("one","two")
editText.setText(myarr[1])
// the speechrecognition
val speechRecognitionIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
speechRecognitionIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault().toString())
startActivityForResult(speechRecognitionIntent, SPEECHINTENTRQ)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
//var speechresult2: kotlin.collections.ArrayList<String>
var speechresult2 : kotlin.collections.ArrayList<String> = kotlin.collections.ArrayList()
//var speechresult2: ArrayList<String> = arrayListOf()
//var speechresult2: kotlin.collections.List<String> = kotlin.collections.ArrayList() // compiled successfully
//var speechresult2 = java.util.ArrayList(String)
//var spokenText: java.util.ArrayList<String> //ArrayList<String>
var speechresult: String? = String()
if (requestCode == SPEECHINTENTRQ && resultCode == Activity.RESULT_OK) {
// this works with String
speechresult = data?.getStringExtra(RecognizerIntent.EXTRA_RESULTS)
editText.setText(speechresult)
//this does not work
//no matter, which var definition from above i use
speechresult2 = data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
// here I get compiler message:
//Type mismatch: inferred type is java.util.ArrayList<String!>? but kotlin.collections.ArrayList<String> /* = java.util.ArrayList<String> */ was expected
//spokenText = speechresult2[0]
//textView.text = spokenText
}
else
editText.setText("Keine Eingabe"+requestCode)
}
}
I wrote into the code the problem
on compile the
speechresult2 = data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
does not work with type mismatch ......
i tried several array list definitions (in code as comment with //)
none of them works
When I try it with
var speechresult2 = data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
without predefinied array list, compiler works. But then I have got the problem, that
for example
var spokenText = speechresult2[0]
does not work. The same compiler message.
What is wrong in my definition of array-list?
Well data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS) returns ArrayList? i.e ArrayList which can be nullable.
You can create a nullable ArrayList variable or initialize it with a new ArrayList if that returns null.
Example
// to populate speech result with empty arraylist if the call returns null
val speechresult2: ArrayList<String> =
data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS) ?: ArrayList()
// or just take nullable array list
val speechresult2: ArrayList<String>? =
data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
package com.example.application.myapplicationenter code here
import android.content.Contextenter code here
import android.os.Bundleenter code here
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var c = Antal.text.toString().toInt()
val MY_PREFS_NAME = "MyPrefsFile";
val editor = getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE).edit()
editor.putString("name", "Elena")
editor.putInt("idName", 12)
editor.apply()
val prefs = getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE)
val name = prefs.getString("name", "No name defined") value.
val idName = prefs.getInt("idName", 0)
Navn.text = name.toString()`enter code here`
Alder.text = idName.toString()
button.setOnClickListener {
val pref = applicationContext.getSharedPreferences("MyPref", Context.MODE_PRIVATE)
var a = Antal.text.toString().toInt();`enter code here`
Alder.text = pref.edit(a + c)`enter code here`
}
}
}
I'm pulling JSON from a URL and i'm using Gson to convert it to a usable PIN code i need to show to the user.
package example
import android.app.IntentService
import android.content.Intent
import com.google.gson.Gson
import java.net.URL
class SendOrderIntentService : IntentService("SendOrderIntentService") {
override fun onHandleIntent(intent: Intent?) {
val orderNumber = (0..999999999999999).random()
val goodsCode = "1001004"
val orderURLPart1 = "URL"
val orderURLPart2 = "URL"
val orderURLPart3 = "URL"
val orderURL = orderURLPart1 + orderNumber + orderURLPart2 + goodsCode + orderURLPart3
val orderJson = URL(orderURL).readText()
println("Order number $orderNumber")
println(orderJson)
val pinCode = Gson().fromJson(orderJson, OrderData::class.java)
println(pinCode)
}
}
Everything works so far, pinCode has the right data.
package example
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_meal_reserved.*
import example.R
class MealReservedActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_meal_reserved)
setSupportActionBar(my_toolbar)
textViewCode.text = SendOrderIntentService().pinCode
}
}
But i can't get the textViewCode to display the pinCode, it just tells me its an
"Unresolved reference: pinCode"
I've been searching for an answer for hours so i think i'm missing something obvious and simple.
Thank you for reading.
Im new to kotlin and i am trying to follow this tutorial:https://www.youtube.com/watch?v=EOfCEhWq8sg have a problem where the line
val rand = Random().nextInt(seekBar.progress)
gives a error
Expected a value of type boolean
Full code:
package pro.bwac.randomizer
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.SeekBar
import android.widget.TextView
import java.util.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rollButton = findViewById<Button>(R.id.RollButton)
val seekBar = findViewById<SeekBar>(R.id.seekBar)
val textView = findViewById<TextView>(R.id.preNumberView)
val ResultsTextView = findViewById<TextView>(R.id.ResultsTextView)
rollButton.setOnContextClickListener{
val rand = Random().nextInt(seekBar.progress)
}
}
The lambda needs to return a boolean value.
Change to:
rollButton.setOnContextClickListener{
val rand = Random().nextInt(seekBar.progress)
true
}