简体   繁体   中英

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. (ie setBackgroundResource)

Any help would be appreciated!

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

The approach is exactly the same. The background resource you set is applied to the ListView regardless of the type of adapter you're using.

I have a custom list item with 3 TextView using a SimpleCursorAdapter and it works fine when setting a background resource on the ListView with setCacheColorHint(0).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM