1

How do I get getString in fragment, I have tried to solve it with snippets of code on this but it did not work to tap

chronology -ok so in the uploaded image there are some 6 items in the recyclerview that line up the grid, I try to make it to the String of the name as I have included the code but the code I press [commant + tap] displays the message {public statuc int} , but it can't be tapped to go to the next page, but if I try to use the usual string as I have included the code, it works on the next page

get an error public static int

dataProduct.add(Products(R.drawable.ic_electricity, getString(R.string.electricity), true))

I've looked for a solution in this forum but it still hasn't worked, but if I use the code below it works

 dataProduct.add(Products(R.drawable.ic_electricity, "Electricity", true))

maybe I'll send a picture, below, in this case I will take the string but can't be tapped like the image below

image getString

hashtag
  • 83
  • 2
  • 8
  • Can you clarify, is this a compiler error, and what is the full text? What do you mean by "did not work to tap"? – Tenfour04 Mar 31 '20 at 18:22
  • ok so in the uploaded image there are some 6 items in the recyclerview that line up the grid, I try to make it to the String of the name as I have included the code but the code I press [commant + tap] displays the message {public statuc int} , but it can't be tapped to go to the next page, but if I try to use the usual string as I have included the code, it works on the next page @Tenfour04 – hashtag Mar 31 '20 at 18:29

1 Answers1

1

getString() is a method that comes from a context. In a fragment, this context is commonly provided by the activity hosting the fragment. To use the activity to get a resource, just call getActivity() (which in Kotlin can be written just as activity):

activity.getString(…)
RedBassett
  • 3,469
  • 3
  • 32
  • 56
  • I tried this code but it didn't work dataProduct.add(Products(R.drawable.ic_mobile_credit, activity.getString(R.string.mobile_credit), true)) @RedBassett – hashtag Mar 31 '20 at 18:51
  • Can you add the error that you are seeing to your question? What do you mean by "didn't work"? – RedBassett Mar 31 '20 at 18:52
  • can't be tapped, this display -> V/FA: Inactivity, disconnecting from the service @RedBassett – hashtag Mar 31 '20 at 18:55