1

Upgrading to Kotlin I came to the point of a Java Interface I was using to get data out of a Custom Recycler Adapter. Now in Kotlin I do not fully understand how to access the interface now. This is my Java Code I'm trying to get working in my Kotlin App.

btOK is a Button in my XML which collects all the selected Items my user has picked inside a ExpListView (You can find the full code here) and due the interface "SelectedDrink" I'm able to access the data.

Here the Button with the ClickListener:

btOk.setOnClickListener(view -> {

   Button button = (Button) view;
   msg = "Upload!\n";
   ArrayList<SelectedDrink> selectedDrinks = expandableListAdapterDrinks.getOrderList();
   Gson gson = new Gson();
   for (SelectedDrink selectedDrink : selectedDrinks) {
   msg += "aid=" + selectedDrink.content + "+qty=" + selectedDrink.qty + "\n";
   }
   final String jsonOrder = gson.toJson(selectedDrinks);
   sendToServer(jsonOrder,sessionId);
 }
});

this is the Interface:

public class SelectedDrink {
    String content;
    Double qty;
}

Now in Kotlin it gives me an error on these two boys here:

selectedDrink.content 
selectedDrink.qty

that

"Cannot access: 'content/qty': it is public/package in SelectedDrink"

I just don't understand what's the error about, neither how to fix it.

KayD
  • 372
  • 5
  • 17

2 Answers2

0

Fields must be public

public class SelectedDrink {
    public String content;
    public Double qty;
}
Artem Botnev
  • 2,267
  • 1
  • 14
  • 19
0

You can't access member variables directly in Java except public, You need getter/setter to access it. Just create Getter methods

public class SelectedDrink {
private String content;
private Double qty;

public String getContent() {
    return content;
}

public Double getQty() {
    return qty;
}    
}
Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51