(Android)在自定义的Adapter添加图片

lyb2518 2012-07-30 04:25:07
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:divider="#00000000"
android:cacheColorHint="#00000000"/>

</LinearLayout>


list_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal" >

<ImageView
android:id="@+id/image_1"
android:layout_width="80dip"
android:layout_height="110dip"
android:layout_marginTop="15dip" />
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal" >
<ImageView
android:id="@+id/image_2"
android:layout_width="80dip"
android:layout_height="110dip"
android:layout_marginTop="15dip" />
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal" >

<ImageView
android:id="@+id/image_3"
android:layout_width="80dip"
android:layout_height="110dip"
android:layout_marginTop="15dip" />
</LinearLayout>
</LinearLayout>


问题是:上面的布局文件描述的信息是一行显示三列,怎样在自定义的Adapter添加图片,难点是我将URL存放在集合里,在集合里取出来的URL代表的是一个数据,而我要填充一行三列的数据(也就是三个数据),不知道如何按顺序地填充图片,望指教。
...全文
441 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hvk687 2012-07-31
  • 打赏
  • 举报
回复
你把你的getcount返回n/3不就可以了吗
lyb2518 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

难道你集合中的数据没有结构化吗?如果没有的话,那只能通过index进行了。自己利用数学计算映射一下。index = position * 3 + i ; (i = 0,1,2);
如果我没理解错的话,呵呵
[/Quote]

继承BaseAdapter,覆盖getView(int position, View convertView, ViewGroup parent)
我尝试过你所说的方法,运行的结果如下:

1 0 0
0 1 0
0 0 1

以上是三行三列的,1代表有图片,0代码没有图片。
getView方法是根据position变量来确定多少行,假设我集合中存储了三个数据,那个意味着getView会调用三次,也就是三行,这样就会导致上面的运行结果。
ItemBean item = new ItemBean() // 这里面存储了一个变量,Bitmap picture.生成它的getting和setting方法
ImageView image_01 = (ImageView)findViewById(R.id.xx);
ImageView image_02 = (ImageView)findViewById(R.id.xx);
ImageView image_03 = (ImageView)findViewById(R.id.xx);

image_01.setImageBitmap(item.getPicture());

// image_02, image_03就不知道怎么为它初始化了。
不要说在item里面加两个变量哦,因为我获到的图片是在同一个服务器地址上,通过服务器的地址用json解析获得所有图片的URL。将URL放进集合中,List<ItemBean> list = new ArrayList<ItemBean>();所以,不能再为ItemBean创建变量了。
可能讲的不是很清楚,望见谅。呵呵!
lyb2518 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

引用 8 楼 的回复:
自己解决了,谢谢各位。
大概的思路是:
1.从网络上获取到的图片URL存储在集合里
2.继承BaseAdapter,
在重写的getCount方法中写成
return list.size() % 3 == 0 ? list.size() : list.size() + 1
难点是在重写的getView方法中判断,将集合中的数据拆分成每一行三列,如果有第二行……
[/Quote]
呵呵,谢谢!
Ada168855 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
自己解决了,谢谢各位。
大概的思路是:
1.从网络上获取到的图片URL存储在集合里
2.继承BaseAdapter,
在重写的getCount方法中写成
return list.size() % 3 == 0 ? list.size() : list.size() + 1
难点是在重写的getView方法中判断,将集合中的数据拆分成每一行三列,如果有第二行的话,再重新迭代getView……
[/Quote]

遇到问题自己解决后印象会非常深刻,既学到了知识又有成就感
lyb2518 2012-07-31
  • 打赏
  • 举报
回复
自己解决了,谢谢各位。
大概的思路是:
1.从网络上获取到的图片URL存储在集合里
2.继承BaseAdapter,
在重写的getCount方法中写成
return list.size() % 3 == 0 ? list.size() : list.size() + 1
难点是在重写的getView方法中判断,将集合中的数据拆分成每一行三列,如果有第二行的话,再重新迭代getView方法,直到数据完成。哈哈!
hvk687 2012-07-30
  • 打赏
  • 举报
回复
难道你集合中的数据没有结构化吗?如果没有的话,那只能通过index进行了。自己利用数学计算映射一下。index = position * 3 + i ; (i = 0,1,2);
如果我没理解错的话,呵呵
lyb2518 2012-07-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

listview,然后自定义adapter,在getview中,inflate这个layout,然后根据inflate出来的view,通过findviewbyid来找到子view,然后设置子view的属性就可以了
[/Quote]
自定义的适配器我会,可能我的问题没有描述清楚吧,不好意思,从网络上获取到的图片。放在集合中,我怎样知道,该集合里的图片应该填充哪一行哪一列呢?因为每行三列中的数据都是ImageView.怎么在集合中将其区分。顺序点填充ImageView.
lyb2518 2012-07-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

你在自定义的对象里面,进行赋值.
[/Quote]
我要获取网络上的URL,在其放在自定义对象里面,但是,这只能使得一个ImageView被填充啊(也就是一列)。
自定义的对象如

public class ItemBean
{
private String logoAddress;

public String getLogoAddress()
{
return logoAddress;
}

public void setLogoAddress(String logoAddress)
{
this.logoAddress = logoAddress;
}
}
hvk687 2012-07-30
  • 打赏
  • 举报
回复
listview,然后自定义adapter,在getview中,inflate这个layout,然后根据inflate出来的view,通过findviewbyid来找到子view,然后设置子view的属性就可以了
ericllzh 2012-07-30
  • 打赏
  • 举报
回复
你在自定义的对象里面,进行赋值.

80,360

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧