我正在使用列表视图如下
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);
现在我的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>
我现在想做的是使每个显示名称都与图片相关联,并可能将其放在地图中,例如:-
Map mMap = new HashMap();
mMap.put("Dexter", "imageDexter.png");
mMap.put("Breaking Bad", "imageDexter.png");
在加载列表视图时,我将显示该地图的名称,并使用该地图的图像名称设置关联的图像背景,然后从drawable文件夹中加载它。
这个怎么做?
像布局一样跳动如下
这是您的自定义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;
}
您的图像阵列将在哪里
int[] images={R.drawable.green_button,R.drawable.ic_launcher,R.drawable.shape,R.drawable.splash};
另外,请确保“图像”数组的长度必须与列表中的项目相同。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句