问题
I have a 200 images in resource folder i named them as name_1.png
,name_2.png
. and I have a array in that I have few numbers such as 2, 4 , 6 ,30 , 40 ... and so on
.
I want to load image and text view into android listview
and show the images according to the number from resource folder.
For this I have created a activity class
and image adapter class
for custom list view
.
I am able to show the text view
data but not the images according to the array data numbers .
can any one suggest me how to do this. I am thinking that I have to write code in getview
method to change the image.
This is getview
method that I tried
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
TextView tv1 = (TextView)convertView.findViewById(R.id.tvtype);
TextView tv2 = (TextView)convertView.findViewById(R.id.tvnumber);
ImageView i1= (ImageView)convertView.findViewById(R.id.ivlfirst);
//int i = Integer.parseInt("normal_"+mStrings.get(position)+".png");
i1.setBackgroundResource("normal_"+mStrings.get(position)+".png");
tv1.setText(mStrings1.get(position));
tv2.setText(mStrings.get(position));
return convertView;
}
回答1:
You can not easily do that when your images are in the res folder since each resource is accessed by an int id.
You should consider to place your images into the assets folder which you can access like a regular file system. Have a look at the AssetManager.
回答2:
Try this if you want to fetch images from assets
:
InputStream is = ctx.getAssets().open("normal_"+mStrings.get(position)+".png");
Drawable d = Drawable.createFromStream(is, "resourceName");
i1.setBackgroundDrawable(d); //i1 is your imageView
hope it helps !!
回答3:
It is simple just do as given below in your getView() method
String yourimagename="normal_"+ mStrings.get(position); // In this no need of image extension like .png,.jpg etc
int resImgId= YourActivityName.this.getResources().getIdentifier("Your Package Name:drawable/" +yourimagename, null, null);
or try this
int resImgId= YourActivityName.this.getResources().getIdentifier("Your Package Name:drawable/" +yourimagename, null, null);
or try this
int resImgId= context.getResources().getIdentifier("Your Package Name:drawable/" +yourimagename, null, null);
i1.setImageResource(resImgId);
回答4:
If you want to use an image from drawable folder based on its name (getting resource id from resource name) use public int getIdentifier (String name, String defType, String defPackage) which returns a resource identifier for the given resource name like:
int image = getResources().getIdentifier(image_name, "drawable",getPackageName());
ImageView i1= (ImageView)convertView.findViewById(R.id.ivlfirst);
i1.setBackgroundResource(image);
For your particular case you can use:
i1.setBackgroundResource(getResources().getIdentifier("normal_"+mStrings.get(position), "drawable",getPackageName()));
回答5:
Take One Integer array
for R.drawable.imageView1......200
and pass its value Like
private Integer[] Imgid = {R.drawable.imageview1,....,200 };
i1.setBackgroundResource(Imgid[j]);
来源:https://stackoverflow.com/questions/10862153/how-to-load-images-from-resources-folder-into-android-listview