public void buttonA1(View view) {
int[] board = {R.id.A1, R.id.A2, R.id.A3, R.id.B1, R.id.B2, R.id.B3, R.id.C1, R.id.C2, R.id.C3};
int i=view.getId().intValue; //even this one didn't work
}
I would like to convert a view Id to int so that i can look it up in the array as shown. How do I do that?
I tried something like this int posNew = Arrays.asList(board).indexOf(view.getId()) but it kept saying the array has no object of type Integer.
I even tried this int posNew =Arrays.asList(board).indexOf(view.getId()), but still it didn't work.
int i=view.getId(); will return the view id as integer value.
And use sorting logic to check the position of the id in array of integer.
I don't know why you want to add them to an array, but this will work
ArrayList<Integer>arrayList;
arrayList = new ArrayList<>();
arrayList.add(new Integer(R.id.button));
arrayList.add(new Integer(R.id.button1));
.
.
.
ArrayList need objects so wrap the primitive int in an Integer object like
so
new Integer(R.id.button)
View's Id IS integer already. No need to convert anything. If you want to convert int to Integer then:
Integer viewId = new Integer(view.getId());
would do the job.
per documentation, the getId() method already returns an int. No conversion or methods are needed
Related
now i've got simple setter and getter of string array. I want to use setter to put some retrevied json info + same text to array. When i use belowe code:
met.setPlacepic(new String[]{"http://dfsdfsdfsf/" + json.getString("source")});
it looks like setter put only one string to array, despite there is many more data.
Declaration is simple
public String[] placepic
and the setter is also simple:
public void setPlacepic(String[] placepic) {
this.placepic = placepic;
}
Anybody knows reason of this?
If the number of strings is fixed (you know exactly how many element you would have in the array), then you could use String Arrays:
String[] placepic = new String[20]; //20 strings
//Then, in your loop:
placepic[i] = yourData;
If you do NOT know how many strings in your data, You should use List:
List<String> placepicList= new ArrayList<String>();
//Then, in your loop:
placepicList.add(yourData);
//Then after the loop, you get the array
String[] placepic = placepicList.toArray(new String[placepicList.size()]);
I have an arraylist of type string which is assigned to spinner. When I use following code to set spinner's selection, it returns -1 as position although string is present in the list.
int spinnerPosition = myAdap.getPosition(myString);
////here it returns -1
//set the default according to value
spinner.setSelection(spinnerPosition);
It returns correct position for some adapters but not for others. What can be possibly wrong?? String I passed in "myString" variable is 100% correct and exists in the adapter list.
I'm not positive, so I apologize if this is wrong. Don't have source code to look at right now.
but I think the getPosition() method does not perform a .equals() comparison, so you can't just pass in an equivalent string. So don't do
myString = "asdf"; //myString is getting assigned a reference to a brand new string
spinnerPosition = adapter.getPosition(myString);
because mySpring is not pointing at the same object as the string in your array, even if myString.equals(originalString) may return true;
This should work:
//assign the reference to point at the exact same object that is in the ArrayList
myString = originalString;
spinnerPosition = adapter.getPosition(myString);
I have an arraylist in which i am storing a list of values. I have to check whether the list contains the same value more than once..And i need only the non-repeating values.. How can i check the number of existences of a value in array list??
Thanks in advance..
Here is how you can do it:
ArrayList<String>numbers= new ArrayList<String>();
numbers.add("1");
numbers.add("2");
numbers.add("1");
numbers.add("3");
int count= Collections.frequency(numbers, "1");
In this way count returns 2.
use
public static int frequency(Collection c,
Object o)
to get frequency of an object in a collection(arraylist).
I would like to ask how can I get the value of predefined value to its name
The following is my code
public class Calculation_Activity extends Activity{
int a=1;
int b=2;
int c=50;
int result;
String array1[]=new String[]{"a","b","c"};
}
I would like to ask how can I get the value of the string by using array1[i]?
for instance, I would like to use array1[3]to call the value of c[ie.50]
May you give me some advice on this matter?
You might solve your issue by using a Map and its standard implementation HashMap:
Map<String, Integer> values = new HashMap<String, Integer>();
values.put("a",1);
values.put("b",2);
values.put("c",50);
String array1[] = new String[] {"a","b","c"};
int result = values.get(array1[2]); //result = 50
// or
int result = values.get("c"); //result = 50
You can use a HashMap (http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html), is a dictionary like data structure where you can store key-pair values
What you're trying to achieve would be better suited to a dynamic/scripting language. Have you considered using a Map instead of multiple varaibles?
In Java, this is not a common approach, such as it would be in scripting languages. You could try to use a Map (ie HashMap), which would enable you to achieve what you want, sort of.
In fact I think it is possible to do exactly what you want using reflection in Java, but I would not go there!
In my app, I have a bunch of images in my drawable folder which I select at random and display using imageView. I've been told about ArrayList's which can add/remove objects from the list...in order to prevent image repeats, some sample code I used below:
// create an array list
ArrayList imageHolder = new ArrayList();
int remaining = 10;
public void initArrayList(){
// add elements to the array list
imageHolder.add((int)R.drawable.child0);
imageHolder.add((int)R.drawable.child1);
imageHolder.add((int)R.drawable.child2);
imageHolder.add((int)R.drawable.child3);
imageHolder.add((int)R.drawable.child4);
imageHolder.add((int)R.drawable.child5);
imageHolder.add((int)R.drawable.child6);
imageHolder.add((int)R.drawable.child7);
imageHolder.add((int)R.drawable.child8);
imageHolder.add((int)R.drawable.child9);
}
//get random number within the current range
int randInt = new Random().nextInt((remaining-1));
//update the imageView config
ImageView image = (ImageView) findViewById(R.id.shuffleImageView);
image.setImageResource(imageHolder.get(randInt));
Eclipse reports that image.setImageResource cannot use an object argument, which is what is provided by arrayList. The actual argument should be int. Any clue how to get around this??
Thanks in advance!
Use List<Integer> imageHolder = new ArrayList<Integer>();
ArrayList contains Objects, always, never primitive types. When you set ints into it, they are autoboxed to Integer objects, when you get them back, you get Integer objects as well. A short fix will be:
image.setImageResource((int)imageHolder.get(randInt));
Be careful though, unboxing a null pointer will cause a NullPointerException, So make sure your randInt is in the range of the arraylist.
EDIT:
I totally missed that, but You initialize your ArrayList like that:
ArrayList imageHolder = new ArrayList();
Which creates ArrayList of Objects. instead, initialize the ArrayList like the following to create ArrayList of integers:
List<Integer> imageHolder = new ArrayList<Integer>();