Issue
I passed images and text in Grid view with launcherIcons class. As the below codes, works fine. But I want to change the String as
new LauncherIcon(R.mipmap.ic_launcher,getResources().getString(R.string.hello))
I want to get string from Resources(R.string.hello) and when implement getResources warning "Non-static Method getResources cannot be referenced from a static-context"
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
String Activity="MainActivity";
Context activity_context=MainActivity.this;
static final LauncherIcon[] ICONS = {
new LauncherIcon(R.mipmap.ic_launcher,"Hello"),
new LauncherIcon(R.mipmap.ic_launcher, "About me"),
new LauncherIcon(R.mipmap.ic_launcher, "Venky"),
new LauncherIcon(R.mipmap.ic_launcher, "Noctilien"),
new LauncherIcon(R.mipmap.ic_launcher, "Metro"),
new LauncherIcon(R.mipmap.ic_launcher, "RER"),
new LauncherIcon(R.mipmap.ic_launcher, "Bus"),
new LauncherIcon(R.mipmap.ic_launcher, "Metro"),
new LauncherIcon(R.mipmap.ic_launcher, "RER"),
new LauncherIcon(R.mipmap.ic_launcher, "Bus"),
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//blah blah.............
}
LauncherIcon Class
static class LauncherIcon {
final String text;
final int imgId;
//final String map;
public LauncherIcon(int imgId, String text) {
super();
this.imgId = imgId;
this.text = text;
// this.map = map;
}
}
How to use getResources String in there? Thanks in Advance.
Solution
The things I'd change is remove the static
in your LauncherIcon
class, and it could be like this :
public class LauncherIcon {
final String text;
final int imgId;
public LauncherIcon(int imgId, String text) {
super();
this.imgId = imgId;
this.text = text;
}
}
Then you create the array of LauncherIcons
without static
as follows :
LauncherIcon[] ICONS = {
new LauncherIcon(R.mipmap.ic_launcher,"Hello"),
new LauncherIcon(R.mipmap.ic_launcher, "About me"),
new LauncherIcon(R.mipmap.ic_launcher, "Venky"),
new LauncherIcon(R.mipmap.ic_launcher, "Noctilien"),
new LauncherIcon(R.mipmap.ic_launcher, "Metro"),
new LauncherIcon(R.mipmap.ic_launcher, "RER"),
new LauncherIcon(R.mipmap.ic_launcher, "Bus"),
new LauncherIcon(R.mipmap.ic_launcher, "Metro"),
new LauncherIcon(R.mipmap.ic_launcher, "RER"),
new LauncherIcon(R.mipmap.ic_launcher, "Bus"),
};
I passed images and text in Grid view with launcherIcons class. As the below codes, works fine. But I want to change the String as :
new LauncherIcon(R.mipmap.ic_launcher,getResources().getString(R.string.hello));
If you get error doing this, you can call it doing (the first way if you change it as my answer should work):
LauncherIcon(R.mipmap.ic_launcher, YOURCONTEXT.getResources().getString(R.string.hello));
Answered By - Skizo-ozᴉʞS
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.