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]);
}
Related
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
My array is a list of events and I need to intialize each event with its information from my JSON file. I created this simple setup based on what I found on other answers here using GSON but I'm super confused on how the gson.fromJSON call works
I have 3 variables in Event that I want to retrieve from the JSON file - start date, end date, and summary. Does fromJSON automatically assign them from the JSON to the their values in Event?
this is what my json file looks like:
[
{
"dtstart": "10/31/2015",
"dtend": "10/31/2015",
"summary": "Halloween"
},
.....
]
there are about half a dozen more of those such events.
This is my code in my Main java file:
public class MainActivity extends AppCompatActivity {
Event[] mobileArray;
Gson gson = new Gson();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("academiccalendar.json"));
} catch (FileNotFoundException e){
e.printStackTrace();
}
mobileArray = gson.fromJson(br, Event[].class);
And this is my event class:
public class Event {
private String dtsart;
private String dtend;
private String summary;
I know this must make me look like a complete fool. But I can't wrap my head around how to turn that JSON file into an array of events. Could someone please point me in the right direction? I've tried a lot of other methods on StackOverflow and elsewhere but none seem right for my situation
EDIT: Removed the loop I had for mobileArray with the line Gil posted. IMPORTANT to future people reading this message - fromGSON was NOT initializing the same named variables in my Event class and I figured out it was because they were set to private and I was trying to assign them from my main activity class. I had to change to public
Why do you need that for loop ?
Did you try this line of code:
mobileArray = gson.fromJson(br, Event[].class);
try to get the whole array at once, instead of one object at a time.
Also, what is the error that you get when trying to do so?
And for your question about fromJson, the answer is yes, it assign them automatically if the variables names are the same in the POJO and in the JSON file.
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.
I am working in a translator kind of app and i need some help.
I have a class with getters and setters for my Array List objects. Each object has a phrase, a meaning, and usage.
so i have this to create my list:
ArrayList<PhraseCollection> IdiomsList = new ArrayList<PhraseCollection>();
now how do i add these objects to the list, each object containing the phrase, its meaning, and a use in a sentence?
For Example: The Layout would be something like this
Phrase
Kick the bucket
Meaning
When someone dies
Usage
My grandfather kicked the bucket
Thanks a lot
this is what i came up with that worked for me
private void loadIdioms() {
//creating new items in the list
Idiom i1 = new Idiom();
i1.setPhrase("Kick the bucket");
i1.setMeaning("When someone dies");
i1.setUsage("My old dog kicked the bucket");
idiomsList.add(i1);
}
ArrayList has a method call add() or add(ELEMENT,INDEX);
In order to add your objects you must first create them
PhraseCollection collection=new PhraseCollection();
then create the ArrayList by
ArrayList<PhraseCollection> list=new ArrayList<PhraseCollection>();
add them by :
list.add(collection);
Last if you want to render that in your ListView item, you must override the toString() in your PhraseCollection.
I suppose you would use the add(E) method (http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#add(E)).
Here is an example using your example provided.
public class Phrase {
public final String phrase, meaning, usage;
//TODO: implement getters?
public Phrase(String phrase, meaning, usage) {
this.phrase = phrase;
this.meaning = meaning;
this.usage = usage;
}
}
and use it like this:
// create the list
ArrayList<Phrase> idiomsList = new ArrayList<Phrase>();
// create the phrase to add
Phrase kick = new Phrase("kick the bucket", "When someone dies", "My grandfather kicked the bucket");
// add the phrase to the list
idiomsList.add(kick);
I have global array. When i want to save something in some method and after that show it from that array it has error NullPointerException. Array is Object type. Code is like this
class Something {
public CoordinatesObject[] coordinates;
Something() {
coordinates = new CoordinatesObject[4];
}
public String myMethod() {
if (coordinates.length==0){
coordinates[0] = new CoordinatesObject(0,0);
}
}
return Integer.toString(coordinates[0].getX());
}
What's wrong?
Sorry I have updated the code. I've created a new array in constructor
You created an array with this line:
coordinates = new CoordinatesObject[4];
and then you're trying to create CoordinatesObject like that:
if (coordinates.length==0){
coordinates[0] = new CoordinatesObject(0,0);
}
but coordinates.length is going to be equal to 4 which means an object of CoordinatesObject class won't be created.
You need to actually allocate space for the array, right now you just have a reference to nothing.
Also note that arrays are fixed-length, you may want to consider using a collection (like a list) instead, depending on your needs.
You have to create the Array with new as well.
Currently, you have a NULL reference. As the previous poster points out, you need to define and create some space for your array.