Search code examples
androidlistviewlistadaptersimpleadapter

Applying a background image to ListView using SimpleAdapter


I recently created a ListView using ListAdapter and applied a static background image behind the lists;

  String[] teams = getResources().getStringArray(R.array.array);
  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_view, teams));

  ListView lv = getListView();
  lv.setTextFilterEnabled(true);

  lv.setBackgroundResource(R.drawable.football_turf_subtle);

  lv.setCacheColorHint(0);

I'm now switching to using SimpleAdapter to include a variety of items within each list item.

  setContentView(R.layout.custom_list_view);

  SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.row_view,
  new String[] {"pen","price","color"},
  new int[] {R.id.text1,R.id.text2, R.id.text3} );

  populateList();

  setListAdapter(adapter);

FYI, the items in the list above are from an example I found. The problem I'm running into is I'm not sure how to apply a background image as I had previously done with the ListAdapter version. (i.e. setBackgroundResource)

Any help would be appreciated!


Solution

  • <ListView android:cacheColorHint="#00000000" android:textSize="10dip"
        android:dividerHeight="1dip" 
        android:smoothScrollbar="true" android:background="@drawable/worldmap4"
        android:id="@+id/ListView01" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:clickable="true"
        android:drawingCacheQuality="auto" android:fastScrollEnabled="true"
        android:footerDividersEnabled="true" android:focusableInTouchMode="true"
        android:isScrollContainer="true" android:keepScreenOn="true"
        android:scrollbarStyle="outsideOverlay"></ListView>
    

    android:background="@drawable/worldmap4" add this element in your xml file it is use for setting the static background of ListView. I hope this is help.