之前已经问过这个问题,但是如果这是一个重复的问题,我找不到答案。但我的gridView只是重复传递给它的最后一张图片。
这是我的onActivityResult,我从手机图库中选择最多六张图片
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
if(requestCode == PICK_IMAGE_MULTIPLE)
{
ArrayList<Bitmap> items = new ArrayList<Bitmap>();
String[] imagesPath = data.getStringExtra("data").split("\\|");
try
{
lnrImages.removeAllViews();
}
catch (Throwable e)
{
e.printStackTrace();
}
for (int i=0; i<imagesPath.length; i++)
{
yourbitmap = BitmapFactory.decodeFile(imagesPath[i]);
items.add(yourbitmap);
}
adapter = new ImageAdapter(this, R.id.grdImages, items);
gridView.setAdapter(adapter);
}
}
}
这是我的imageadapter
public class ImageAdapter extends ArrayAdapter<Bitmap>
{
Bitmap bm;
private Context context;
private ArrayList<Bitmap> items;
public ImageAdapter(Context context, int textViewResourceId, ArrayList<Bitmap> items) {
super(context, textViewResourceId, items);
this.context = context;
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null)
{
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
}
else
{
imageView = (ImageView) convertView;
}
for (int i = 0; i < items.size(); i++)
{
bm = items.get(i);
imageView.setImageBitmap(bm);
return imageView;
}
return imageView;
}
}
感谢任何反馈,作为培训的程序员,非常感谢。
答案 0 :(得分:1)
删除此块...
for (int i = 0; i < items.size(); i++) {
bm = items.get(i);
imageView.setImageBitmap(bm);
return imageView;
}
并添加此
imageView.setImageBitmap(items.get(position));
答案 1 :(得分:0)
当您调用getView方法时,您不能使用for循环〜&#39;
记住ListView和GridView需要一个数据源,无论你想在listView / gridView上更改内容,都要更改数据源。
你可以在getView方法中执行此操作.work {
max-height: auto;
background-color: #f2f2f2;
font-family: Myriad Pro;
text-align: center;
}
.work .container {
width: 100%;
height: 90%;
padding: 5% 5%;
}
.work img {
width: 100%;
height: 100%;
border: 2px solid GhostWhite;
border-radius: 4px;
margin-top: 10px;
overflow: hidden;
}
.work button {
margin-top: 10px;
float: left;
background-color: #0080ff;
border-radius: 4px;
border: 2px solid GhostWhite;
margin-bottom: 30px;
padding: 10px 20px;
text-align: center;
}
.work button a {
color: #ffffff;
font-weight: lighter;
text-decoration: none;
}
/* About me */
.about {
background-color: #000000;
#ffffff;
max-height: auto;
max-width: 100%;
font-family: Myriad Pro;
text-align: center;
}
.about .container {
max-width: 100%;
max-height: 100%;
padding: 10%;
font-size: 1.2rem;
}
public View getView(int position,View convertView,ViewGroup parent) {
<div class="work">
<div class="container">
<h3>MY WORK</h3>
<hr>
<div class="gallery">
<img id="image-1" src="file:///C:/Users/Nelson/Desktop/Portfolio/Images/taduuda-72915.jpg">
<button><a href="#">Learn More</a></button>
</div>
<div class="gallery">
<img id="image-2" src="file:///C:/Users/Nelson/Desktop/Portfolio/Images/taduuda-72915.jpg">
<button><a href="#">Learn More</a></button>
</div>
</div>
</div>
<!-- About Me -->
<div class="about">
<div class="container">
<h3>ABOUT ME</h3>
<hr>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere lacinia urna vel dapibus. Suspendisse nec porttitor ipsum. Etiam lorem dolor, pulvinar eu magna ac, lobortis scelerisque nulla. Aenean vel fringilla neque, at porta risus. Praesent
vitae congue dolor. Morbi pharetra egestas lacinia. Nam luctus dictum aliquam. Vivamus vestibulum egestas odio, ut lacinia justo. Nullam vulputate blandit efficitur.</p>
</div>
</div>