ask with getView method in gridView - java

I want to load images each one for each gridview element but it loads me only last image for all elements of gridview. How can I solve it ? My example images are like a50.jpg
Here is my code:
for (int i = 0; i < items.size(); i++) {
String s = Integer.valueOf(items.get(position).id_of_field).toString();
int resourceId = context.getResources().getIdentifier("a" + s + i,
"drawable", context.getPackageName());
view.ivTowar.setImageResource(resourceId);
}

position is already giving a position of cell which is being viewed. Remove your loop. For code and detail about custom gridview follow this
or google for good tutorial

In a getView method you are creating View for a specific element, not for all elements at all. So the correct code shouldn't contain for loop.
EDIT The correct code should be like that, without for loop (and you are trying to parse a string to integer and then get a string again):
String s = items.get(position).id_of_field;
int resourceId = context.getResources().getIdentifier("a" + s + position,
"drawable", context.getPackageName());
view.ivTowar.setImageResource(resourceId);

Related

Getting an incremental layout id programatically

I have a set of textviews like this..
TextView d1,d2,d3,d4,d5,d6,d7,d8.....etc
How can I set text in a loop like this by increment the number after d like this
for(int i=1;i<=8;++i)
{
d+i.setText("foo");
}
You can do like below.
Create ArrayList<TextView> and add all TextView in list.
Then after you have to do loop with List size.
and you can update text to TextView.
With this:
for(int i = 1; i <= 8; i++) {
int id = getResources().getIdentifier("d" + i, "id", getPackageName());
TextView tv = (TextView) findViewById(id);
tv.setText("foo");
}
the method getIdentifier() gets the integer id of a View by its id "name".
If this code is not inside an activity, you must provide a valid Context for the methods getResources() and getPackageName(), like:
context.getResources() and context.getPackageName().

How to get drawable name from imageView

I have an app that create a random image when the activity opens. There are like 30 images in the random image array. The image is generated, but what I want is to write the drawable name into a textview. It looks like this:
ImageView RImage= (ImageView) findViewById(R.id.image);
RImage.setImageResource(generator());
Drawable myDrawable = RImage.getDrawable();
TextView writeID = (TextView) findViewById(R.id.idtext);
writeID.setText(String.valueOf(generator()));
}
private int generator() {
TypedArray imgs = getResources().obtainTypedArray(R.array.list3);
int imgid = imgs.getResourceId(new Random().nextInt(imgs.length()), -1);
imgs.recycle();
return imgid;
}
Currently I receive numbers, and I cant figure out how can I convert the int to a string, or how can I use the generator to generate a string, display in the textview and then convert it to int to display it in the imageview.
I created an another activity where I can pick the image I want to see from a spinner, it simply send the selected option with string to the next activity on button press, and in the other activity i remove the .png and res/drawable/ from the string, and convert the string into an int to display the image.
Bundle extras = getIntent().getExtras();
String transportItemChosen = extras.getString("SpinnerValue");
transportItemChosen = transportItemChosen.replace(".png", "");
transportItemChosen = transportItemChosen.replace("res/drawable/", "");
String uri = ("#drawable/" + transportItemChosen);
int id = getResources().getIdentifier(uri, null, getPackageName());
ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.selectedimage);
mImageView.setImageResource(id);
So I need the generator to generate to string and then convert it to int. Thanks!
Change your code by converting your int ids into String names by Using the method
getResourceEntryName(int id);
Change the line:
writeID.setText(String.valueOf(generator()));
To Something like:
writeID.setText(getResources().getResourceEntryName(generator()));
And for Viceversa:
And if you are having a String lets assume this String you get from a textView or using above method to convert it back to an int id you do the following:
int id= getResources().getIdentifier("your_string_here", "drawable", getPackageName());
And if you are calling the code from a Fragment not an Activity remember to put getActivity() before calling getResources()or before calling getPackageName().
I have not tested the code as I am typing, but I am sure it will work (may be a typo) for more information consider visting the Documentation on Resources from this official link.
If I understand you correctly, what you need is to take a look at getResourceName(int) method - it returns resource name by its id. Also you can use getIdentifier(String,String,String) method to retrieve resource id by its name.
If you want to display name of the randomly picked image, you should save the generated resource id into the variable and get corresponding resource name for it instead of calling generator() method twice as you'll get two different ids.

How to add a count to resource id's

I was wondering if what i want is even possible.
Trying some coding for Android but i have a little issue and i hope someone can help me with this.
I have
ImageView img1 = (ImageView) findViewById(R.id.circleselect1);
Now what is want is the Integer at the end of my resource id incremented with one
Like
ImageView img1 = (ImageView) findViewById(R.id.circleselect+integer);
I know this is not working but is there a way to accomplice what i want?
I saw your comment I have a counter, and i have 4 R.id.circleselect's like R.id.circleselect1, R.id.circleselect2, R.id.circleselect3. R.id.circleselect4 i want to get the right source if the counter hits the next intand the solution you are asking for is a bad idea.
Instead try this one out. Create an array of the ID's that you want and then use them as you want.
int[] array = new int[]{R.id.circleselect1,R.id.circleselect2,R.id.circleselect3,R.id.circleselect4};
IDs as they appear in the R class (more precisely, in the id class inside R) are just static fields.
You can access them dynamically by using
R.id.class.getDeclaredField("circleselect" + i).getInt(null);
where i is the integer at the end of the name.
This is not advised as this is very un-OOP and will break if the field does not exist.
Something Like this?
String circle = "circleselect" + integer; // or integer.toString()
int resID = getResources().getIdentifier(circle, "id", getPackageName());
ImageView img = (ImageView) findViewById(resID);

Setting tag for an imageview

I use an adapter to create views, with mIDS an array of numbers.
The setTag function doesn't seem to work, as the system.out line produces null for every view.
What am I doing wrong?
ImageView view = imageViews[position];
view.setImageBitmap(bmparray[position]);
view.setTag(mIDs[position]);
System.out.println("tag[pos] = " + view.getTag(mIDs[position]));
view.getTag(mIDs[position])
Change to
view.getTag()

Applying a background image to listview with textview

I'm trying to create a listview with a static background image (i.e. not contained within the individual list cells, but fills the display and does not move when list is scrolled). I've found a couple references here and am trying to implement, but no joy. The first is;
Set a background for a listview
In particular, I'm trying this;
//set background to Drawable
listView.setBackgroundDrawable(myDrawable);
To create the "myDrawable" variable, I'm using a suggestion from;
How can I access an android drawable by a variable
Again, the particular code I'm trying is;
String icon="logo" + cnt;
int resID = getResources().getIdentifier(icon, "drawable", getPackageName());
logo.setImageResource(resID);
Here is the code I created;
ListView lv = getListView();
lv.setTextFilterEnabled(true);
String bg="football_turf_subtle";
int resID = getResources().getIdentifier(bg, "drawable", getPackageName());
myDrawable.setImageResource(resID);
lv.setBackgroundDrawable(myDrawable);
The problem I'm running into in my code is "myDrawable cannot be resolved"? If it's not obvious, I'm new to Android/Java.
Thanks in advance for any help!
Just do lv.setBackgroundResource(R.drawable.football_turf_subtle).

Categories

Resources