Android的自定义布局列表视图自定义、视图、布局、列表

2023-09-04 03:27:50 作者:散步的菠萝

我使用的是列表视图如下:

I am using a list view as follows

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是象下面

Now my tv_show_each_show_name XML is like below

<?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>

我想现在要做的是让与图片相关的每个节目的名字,也许把在一张地图,如: -

What I want to do now is make each show name associated with a picture and maybe put that in a map, like :-

Map mMap = new HashMap();
mMap.put("Dexter", "imageDexter.png");
mMap.put("Breaking Bad", "imageDexter.png");

和在加载列表视图我会从这个图显示的名称,并设置相关的图像背景从这个地图以图片的名称,并从可绘制文件夹中加载它。

and while loading the list view i will show the name from this map and set the associated image background taking the image name from this map and loading it from the drawable folder.

如何做到这一点?

跳频像布局如下:

推荐答案

下面是一个教程了解您的自定义列表视图

Here is a tutorial for your custom Listview

现在对于其中设置不同的图像,每个项目,您的getView()内的适配器的方法

Now for setting different images with respect to each item ,inside your getView() method of adapter

@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;
  }

如果你的图像阵列会是这样

Where your image array will be like this

 int[] images={R.drawable.green_button,R.drawable.ic_launcher,R.drawable.shape,R.drawable.splash};

另外,还要确保你的'形象'数组的长度必须相同列表中的项目。

Also make sure your the length of your 'images' array must be same as your items in list.

 
精彩推荐
图片推荐