Android custom layout to view list

I use 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);

Now my tv_show_each_show_name XML looks 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>

Now I want to make each display name associated with the image, and possibly put it on the map, for example: -

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

and when loading the list view, I will show the name from this map and set the associated background of the image, take the name of the image from this map and load it from the drop-down folder.

How to do it?

Popping up as a layout as follows

enter image description here

+4
source share
3 answers

Here is a tutorial for your custom Listview list

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

, "" , .

+10

2 getView, .

, , ArrayAdapter , ArrayAdapter YourData , .

getView . position

getView (int position, View convertView, ViewGroup)

, ArrayAdapter.getItem

. http://developer.android.com/reference/android/widget/ArrayAdapter.html

+1

look at this example here it shows how to upload images to listview in android, you need to expand BaseAdapter to create a custom adapter

0
source

All Articles