Custom ListAdapter's ViewHolder returning null

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Custom ListAdapter's ViewHolder returning null

Postby thelegendofzaku » Sun Jan 17, 2010 11:14 am

For some reason or another, when I try using my custom list adapter within a ListView declared in an XML file, the holder in the adapter's getView method is returning null, even after instantiated. In other words, if I just set the ListAdapter as the only view, it renders without any problems in my activity. However, when I set the content view to my activity's XML layout file (which contains a text view for the activity's custom title bar, and the ListView with the id set to android:list), the holder ends up returning null, thus throwing a NullPointerException. My question is, is there a way to create an activity layout that renders both a textview as a title and my list with no problems?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class Items extends ListActivity {
  3.  
  4.        
  5.  
  6.         public static final String TAG = "Items";
  7.  
  8.        
  9.  
  10.         static String[] m_itemList;
  11.  
  12.     private TextView m_title;
  13.  
  14.  
  15.  
  16.         private static class ItemListAdapter extends BaseAdapter{
  17.  
  18.         private LayoutInflater m_inflater;
  19.  
  20.         Bitmap m_firstIcon;
  21.  
  22.         Bitmap m_secondIcon;
  23.  
  24.         Bitmap m_thirdIcon;
  25.  
  26.         Bitmap [] m_iconHolder = {m_firstIcon, m_secondIcon, m_thirdIcon};
  27.  
  28.        
  29.  
  30.         public ItemListAdapter(Context ctx){
  31.  
  32.                 m_inflater = LayoutInflater.from(ctx);
  33.  
  34.                 m_firstIcon = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.first_icon);
  35.  
  36.                 m_secondIcon = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.second_icon);
  37.  
  38.                 m_thirdIcon =  BitmapFactory.decodeResource(ctx.getResources(), R.drawable.third_icon);
  39.  
  40.         }
  41.  
  42.  
  43.  
  44.                 public int getCount() {
  45.  
  46.                         // TODO Auto-generated method stub
  47.  
  48.                         return m_itemList.length;
  49.  
  50.                 }
  51.  
  52.  
  53.  
  54.         public Object getItem(int position) {
  55.  
  56.             return position;
  57.  
  58.         }
  59.  
  60.  
  61.  
  62.         public long getItemId(int position) {
  63.  
  64.             return position;
  65.  
  66.         }
  67.  
  68.  
  69.  
  70.                 public View getView(int position, View convertView, ViewGroup parent) {
  71.  
  72.                         ViewHolder holder;
  73.  
  74.                        
  75.  
  76.                         if (convertView == null) {
  77.  
  78.                 convertView = m_inflater.inflate(R.layout.item_element, null);
  79.  
  80.                 holder = new ViewHolder();
  81.  
  82.                 holder.text = (TextView) convertView.findViewById(R.id.elementText);
  83.  
  84.                 holder.icon = (ImageView) convertView.findViewById(R.id.elementIcon);
  85.  
  86.                 Log.d("ItemListAdapter", "populated holder");
  87.  
  88.                         } else {
  89.  
  90.                                  holder = (ViewHolder) convertView.getTag();
  91.  
  92.                                  Log.d("ItemListAdapter", "retrieved holder");
  93.  
  94.                         }
  95.  
  96.                         holder.text.setText(m_itemList[position]);
  97.  
  98.                         //holder.icon.setImageBitmap(m_iconHolder[position]);
  99.  
  100.                         if(m_itemList[position].equals("Item 1")){
  101.  
  102.                                 holder.icon.setImageBitmap(this.m_firstIcon);
  103.  
  104.                         } else if(m_itemList[position].equals("Item 2")){
  105.  
  106.                                 holder.icon.setImageBitmap(this.m_secondIcon);
  107.  
  108.                         } else if (m_itemList[position].equals("Item 3")){
  109.  
  110.                                 holder.icon.setImageBitmap(this.m_thirdIcon);
  111.  
  112.                         }
  113.  
  114.                         return convertView;
  115.  
  116.                 }
  117.  
  118.                
  119.  
  120.         static class ViewHolder {
  121.  
  122.             TextView text;
  123.  
  124.             ImageView icon;
  125.  
  126.         }
  127.  
  128.        
  129.  
  130.         }
  131.  
  132.        
  133.  
  134.        
  135.  
  136.        
  137.  
  138.         public void onCreate(Bundle savedInstanceState){
  139.  
  140.                 super.onCreate(savedInstanceState);
  141.  
  142.                 Resources res = getResources();
  143.  
  144.                 m_itemList = res.getStringArray(R.array.m_itemLabels);
  145.  
  146.                 setContentView(R.layout.item_title);
  147.  
  148.                 m_title = (TextView) findViewById(R.id.itemTitle);
  149.  
  150.                 m_title.setText("Items");
  151.  
  152.                 setListAdapter(new ItemListAdapter(this));
  153.  
  154.         }
  155.  
  156. }
  157.  
  158.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <RelativeLayout
  5.  
  6.  xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.  android:layout_width="wrap_content"
  9.  
  10.  android:layout_height="wrap_content"
  11.  
  12.  >
  13.  
  14. <TextView
  15.  
  16.         android:layout_height="wrap_content"
  17.  
  18.         android:id="@+id/itemTitle"
  19.  
  20.         android:textSize="18sp"
  21.  
  22.         android:layout_gravity="center_vertical|center_horizontal"
  23.  
  24.         android:layout_width="fill_parent"
  25.  
  26.         android:layout_above="@+id/android:list"
  27.  
  28.         >
  29.  
  30.         </TextView>
  31.  
  32.        
  33.  
  34. <ListView
  35.  
  36.         android:id="@+id/android:list"
  37.  
  38.         android:layout_width="wrap_content"
  39.  
  40.         android:layout_height="wrap_content"
  41.  
  42. >
  43.  
  44. </ListView>
  45.  
  46. </RelativeLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
thelegendofzaku
Junior Developer
Junior Developer
 
Posts: 10
Joined: Thu Mar 19, 2009 5:53 pm

Top

Postby thelegendofzaku » Sun Jan 17, 2010 6:56 pm

I just realized that I forgot to set the tag to my holder for my view in getView. After I did that, it managed to render just fine. However, for some reason, I can't see the textview that's supposed to appear above my list.
thelegendofzaku
Junior Developer
Junior Developer
 
Posts: 10
Joined: Thu Mar 19, 2009 5:53 pm

Postby qlimax » Sun Jan 17, 2010 11:20 pm

the guy there
http://www.anddev.org/listview_with_oth ... t9601.html
had a similar layout problem.

look at the xml i posted there, it's like yours but with an addictional edittext...

hope it helps.
bye :wink:
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests