0

As in the topic, how to multiplicate data from column with edit text in AdapterList? e.g.

class AdapterList (context: Context, val resource: Int, val objects: List<Opryski>) :
ArrayAdapter<Opryski>(context, resource, objects){
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val inflater = LayoutInflater.from(context)
val customView = inflater.inflate(resource, parent, false)
val area= customView.findViewById<EditText>(R.id.editTextSize)
val dose= customView.findViewById<TextView>(R.id.id_dose)
val name= customView.findViewById<TextView>(R.id.id_nazme)
val what= customView.findViewById<TextView>(R.id.id_what)
val when= customView.findViewById<TextView>(R.id.id_when)
val profilaktyka = customView.findViewById<TextView>(R.id.id_profilaktyka)

val item = objects.getOrNull(position)
if(item!=null)
{
    name.text = item.name
    dose.text = item.dose.toString() * area //<--- I want to multiplicate this//
    what.text = item.what
    when.text = item.when
    profilaktyka.text = item.profilaktyka
}

return customView }

treesher
  • 3
  • 2

1 Answers1

0

You can try something like this:

dose.setText((area.text.toString().toInt() * item.dose.text.toString().toInt()).toString())

Because you get an editable from editext.text and then you convert it to string and then parse Int from that. Calculate whatever you want and then setText with string back.

Mayur Gajra
  • 8,285
  • 6
  • 25
  • 41
  • if I'm trying to do like you suggested the app is crashing and is an error : java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.widget.EditText.toString()' on a null object reference at com.example.farmerhelper.AdapterList.getView(AdapterList.kt:29) – treesher Apr 22 '21 at 15:13
  • Please debug and make sure that `area` or `dose` is not null on `AdapterList.kt:29` – Mayur Gajra Apr 22 '21 at 15:16
  • @treesher is `item.dose` an Int? – Mayur Gajra Apr 22 '21 at 15:26
  • in entity "pesticides" column 'dose' I have as var dose: Float?=null, so I changed toInt() to toFloat() and in EditText i enter value e.g. 0.9 – treesher Apr 22 '21 at 15:44
  • @treesher did you try this? `dose.text = (area.text.toString().toFloat() * item.dose).toString()` – Mayur Gajra Apr 22 '21 at 15:50
  • yes and in item.dose I have error "type mismatch, required:Float found : Float?, have you idea how to deal with it? – treesher Apr 22 '21 at 15:55
  • @treesher it means that the entity as it's type as nullable float. So you can write `item.dose!!` in calculation `dose.text = (area.text.toString().toFloat() * item.dose!!).toString()`. Caution: This will throw Nullpointer in item.dose is null. so make sure to check that before calculation – Mayur Gajra Apr 22 '21 at 16:00
  • oh I got it, now when I enter area and click button to show list of pesticides the app is backing to main activity and the error is "E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.farmerhelper, PID: 28813 java.lang.IllegalStateException: area must not be null at com.example.farmerhelper.AdapterList.getView(AdapterList.kt:29)" but I entered the value :( – treesher Apr 22 '21 at 16:07
  • @treesher I would request you to debug the code bit because it's not related to the calculation question. Mostly null pointer can be tracked by logging or debugging. You can read about this in depth here: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Mayur Gajra Apr 22 '21 at 16:10
  • ``area`` is a variable that points to your ``EditText`` (that you're trying to read input from), if ``area`` is null that means its ``findViewById`` lookup failed - are you passing it the correct ID? – cactustictacs Apr 22 '21 at 18:35
  • yes, the ID is correct. Is that possible that when I have Edit text in main activity, I enter the value and click button to go to next activity so the value is not remembered and shows null?? – treesher Apr 22 '21 at 18:40
  • @treesher Can you please confirm that, is `editTextSize` which is being assigned to `area` a part of inflated `resource` layout or it's in another layout? – Mayur Gajra Apr 23 '21 at 11:39
  • i had to take rest from this app for a moment. "editTextSize" is in main_activity_layout and inflated "resource" layout it is List so it is another layout – treesher Apr 26 '21 at 08:52
  • @treesher that's the issue. Your id is correct but it's not part of `customView` so `findViewById` won't be able find that and will assign `null`. – Mayur Gajra Apr 26 '21 at 09:30
  • is there a solution to fix that? in another post someone told me to do in this way but i want to put `editTextSize` in main activity_layout https://stackoverflow.com/a/67219696/15133607 – treesher Apr 26 '21 at 09:50
  • @treesher yes, don't do `findviewbyId` there just pass the `value` of your `area` from the activity. create a function in the adapter that takes the value. – Mayur Gajra Apr 26 '21 at 09:55
  • i hope that it will be the last question :p I have 4 activites: main, choice(apple,blueberries), choice for what (pests, diseases)and activity with list of pesticides to use for it depends what user clicked. will the value from 1st activity be able in AdapterList or Ihave to pass the value for every activity? – treesher Apr 26 '21 at 10:47
  • @treesher For every activity you'll need to pass if you don't want to do that then create a global static variable with companion object. – Mayur Gajra Apr 26 '21 at 10:53