我正在使用列表视图如下
String[] Shows = new String[] { "Dexter", "Breaking Bad", "The Big Bang Theory", "Leverage"};
ListView tv_show_list = (ListView) view_tvshowAddNew.findViewById(R.id.lv_tv_show_list);
ArrayAdapter<String> showNameAdapter = new ArrayAdapter<String>(getActivity(), R.layout.tv_show_each_show_name, Shows);
tv_show_list.setAdapter(showNameAdapter);
Run Code Online (Sandbox Code Playgroud)
现在我的tv_show_each_show_name XML如下所示
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="58dp"
android:background="@drawable/ic_launcher"
>
</TextView>
Run Code Online (Sandbox Code Playgroud)
我现在要做的是让每个节目名称与图片相关联,并将其放在地图中,如: -
Map mMap = new HashMap();
mMap.put("Dexter", "imageDexter.png");
mMap.put("Breaking Bad", "imageDexter.png");
Run Code Online (Sandbox Code Playgroud)
在加载列表视图时,我将显示该地图的名称,并设置相关的图像背景,从该地图获取图像名称并从drawable文件夹加载.
这该怎么做?
像布局一样跳跃如下

Jit*_*Dev 13
这是您的自定义Listview 的教程
现在,在适配器的getView()方法内设置关于每个项目的不同图像
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
// Change the icon for Windows and iPhone
String s = values[position];
imageView.setImageResource(images[position]);
return rowView;
}
Run Code Online (Sandbox Code Playgroud)
你的图像数组将是这样的
int[] images={R.drawable.green_button,R.drawable.ic_launcher,R.drawable.shape,R.drawable.splash};
Run Code Online (Sandbox Code Playgroud)
还要确保"图像"数组的长度必须与列表中的项目相同.
看看这个向文本视图添加 2 个按钮 您基本上需要一个用于列表的自定义适配器,并且需要重写它的 getView 方法,您可以在其中指定项目的 UI 布局。
对于您发布的代码,不要使用 ArrayAdapter 的实例,而是创建您自己的从 ArrayAdapter 派生的适配器 YourData 应该是一个包含对项目文本和项目图像的引用的类。
现在重写自定义 Adapter 类中的 getView 方法。该方法用于使用来自的位置参数填充项目的 UI
View getView(intposition,ViewconvertView,ViewGroupparent)
您可以使用 ArrayAdapter.getItem 获取该项目
有关更多详细信息,请参阅上面的链接和http://developer.android.com/reference/android/widget/ArrayAdapter.html
| 归档时间: |
|
| 查看次数: |
36200 次 |
| 最近记录: |