How can I get String in String[]? - android

String[] string = {getResourse().getString(R.string.girl), getResourse().getString(R.string.boy), getResourse().getString(R.string.child)};// Why is this code not working
And I tried do like this
String[] string = {getString(R.string.girk)....};
And also I tried do like
String[] string = {R.string.girl...};
Everything is not working, or it is impossible?

First of all, R.string.girl is int type.
If you really want to define it programatically like below,
String[] string2 = {getString(R.string.app_name)};
You should define it when your Activity/fragment's context is avaliable. For example, you can define it inside onCreate method but it is impossible to define it as a global variable (Because getString is the method of Context class).

You can do like in this example.
For more info https://developer.android.com/guide/topics/resources/string-resource.html#StringArray

Related

How to get string from resources strings into a fragment

I tried reading a number of solutions on Stack Overflow and have found they either don't work for my scenario or I simply don't understand their explanation (I am very new to Java and Android.
I have strings set up under res/values/strings.xml that I wish to use in the class:-
public class AttractionFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.word_list, container, false);
// create an array list of details
final ArrayList<Details> details = new ArrayList<>();
// Details details
details.add(new Details(getActivity().getString(R.string.fun_bigsplash_name), getString(R.string.fun_bigsplash_addr), R.string.fun_bigsplash_num, R.drawable.bigsplash));
I've tried a number of variants (the reason they are different is just to show what I tried) but can't work it out. The R.drawable.bigsplash works fine (when I'm using literal strings for the others).
The error message states an int, which I assume means it's getting the reference and not the actual string.
How do I get the string from within the fragment?
Thanks.
You can use:
getResources().getString(R.string.my_string);
or just:
getString(R.string.my_string);
Read String value or String Array In Java Code.
Define a string array in strings.xml use string-array xml element.
Show Selection
<string name="auto_complete_text_view_car">Input Favorite Car Name</string>
<string-array name="car_array">
<item>Audi</item>
<item>BMW</item>
<item>Benz</item>
<item>Ford</item>
<item>Toyota</item>
<item>Tesla</item>
<item>Honda</item>
<item>Hyundai</item>
</string-array>
Read String Value In Java Code. Only string name
Inside Activity::
String defaultInputText = getResources().getString(R.string.auto_complete_text_view_car);
Inside Fragment::
String defaultInputText = getActivity().getResources().getString(R.string.auto_complete_text_view_car);
Read the string array in java source code. Please note car_array is just the string array name defined in strings.xml.
Inside Activity::
String carArr[] = getResources().getStringArray(R.array.car_array);
Inside Fragment::
String carArr[] = getActivity().getResources().getStringArray(R.array.car_array);
Simplest way to get string from resource in any part of your code is to override Application class and create static method to obtain "Application context".
For example, check out the application class for my app AB Music.
https://github.com/amit-bhandari/AB-Music-Player/blob/master/app/src/main/java/com/music/player/bhandari/m/MyApp.java
Refer method
public static Context getContext(){
return instance;
}
Now you can get string from resource anywhere in your code by simply calling.
MyApp.getContext().getString(R.string.mystring)
Hope it helps!
try this one
getActivity().getResources().getString(R.string.app_name)

Arrays.asList(array).contains(value) doesn't work correctly (android)

I have a string[]
String [] example = new String[]{
"one",
"two",
"three",
"four"
}
i am trying to check if this array contain next string "one", but it return false...
Here is my code:
boolean check = Arrays.asList(example).contains("one");
any ideas why it happens?
If you look at the Javadoc for List at the contains method you will see that it uses the equals() method to evaluate if two objects are the same.
Make test:
List<String> list = Arrays.asList(example);
boolean check = list.contains(list.get(1));
and
List<String> list = Arrays.asList(example);
boolean check = list.contains("one");
As this code is correct I guess that at the time/point of using this code your Array may have not been initialized yet or it's contents may have been altered.
Also try a clean and rebuild.
Maybe this is somehow related to the fact, that Arrays.asList() returns special implementation of List - java.util.Arrays.ArrayList, and not the java.util.ArrayList.

Passing Bundle into an AsyncTask

I'm struggling with bundles in an AsyncTask. I have two Strings that I want to pass to a AsyncTask, I want to use bundles to accomplish this task.
The code in the MainActivity:
Bundle adresses = new Bundle();
adresses.putString("to", textField1.getText().toString());
adresses.putString("from", textField2.getText().toString());
new PriceTask(getApplicationContext()).execute(adresses);
And in my AsycTask I do it like this:
protected Integer doInBackground(Bundle... b) {
Bundle result = b[0];
String to = result.getString("to");
String from = result.getString("from");
}
It's worth mentioning that my two strings contains something like this
"Sometext here, and sometext here 1234"
Put I can't retrieve the text, my debugger says that the Bundle contains the right information but my String will not contain the right information. When I debug and set breakpoints where my Strings are, it will just have the value:
[t, o]
What am I doing wrong here? Thanks in advance.
In MAin activity replace the below lines::
Bundle adresses = new Bundle();
adresses.putString("to", textField1.getText().toString());
adresses.putString("from", textField2.getText().toString());
new PriceTask(getApplicationContext()).execute(adresses);
with
new PriceTask(getApplicationContext(),textField1.getText().toString(),textField2.getText().toString()).execute();
And in your AsycTask add constructor like below::
String to;
String from;
Context context;
public YourAsyncTask(Context context, String to,String from) {
// TODO Auto-generated constructor stub
this._activity = _activity;
this.to = to;
this.from = from;
}
What you are doing wrong is the way you retrieve the data in the doInBackground method. The argument that method having is a Array type. Please concern the ... What you are doing in the line Bundle result = b[0]; is only getting the 0th element of that Array and pass it to a Bundle reference.
Your given code and details is not enough to give a perfect answer. If all your given codes in the same Java class, you no need to use a Bundle. Instead you can create a ArrayList of type String to contains your values witch you are getting from the TextFields. Then doInBackground also contains a ArrayList as the method argument. Then get all the List items and separate your "to" and "from" values.
If you are stick with the existing code, first try to find out what is inside the result variable.

Adding entire string array to adapter

I'm sure this has already been asked, so if somebody can refer me to another thread that'd be great.
I've got a string array:
String animalArray[] = {"Dog", "Cat", "Horse", "Snake"};
And I've got a spinner with an adapter. How do I avoid having to do this?:
adapter1.add(animalArray[0]);
adapter1.add(animalArray[1]);
adapter1.add(animalArray[2]);
adapter1.add(animalArray[3]);
Can I use a for loop or something? Or is there a better way?
Loop through your array and add them like this:
for (String s : animalArray) {
adapter1.add(s);
}
ArrayAdapter(if you are talking about it) counstructor can solve the problem.
It can use String array as data sourse, T[] objects.
UPD sample code:
for(int i=0;i<animalArray.length;i++){
adapter1.add(animalArray[i]);
}

android: how to assign value to a global variable from sql query?

i'd like to assign the resulting "reason" to a global variable for use with another process. How can i achieve this from this declaration and function?
private static String[] FROM = { _ID, EVENT01, EVENT02, EVENT03, REASON};
private static String ORDER_BY = EVENT01;
private Cursor getEvent() {
return managedQuery(CONTENT_URI, FROM, null, null, ORDER_BY);
}
many thanks for your help.
If you have the Cursor, then iterate it and access the value
while (cursor.moveToNext()) {
//access the value using getString(0), or getInt(0)
}
U need to store this global, I think its better to use static variable,
class Abc {
public static final String staticString;
----------------
----------------
}
Then you can access it anywhere using Abc.staticString
And its always better to create you Cursor inside a method and close it there itself, after its use.
Another process or thread?
For another process: If you have ContentProvider you can get Cursor from others processes.

Categories

Resources