Search code examples
javaandroidlistviewcustom-adapter

Custom ListView doesn't show anything


I'm trying to populate a ListView with Strings (food item names) on the left side, and item count numbers on the right. I've made a CustomAdapter class (see below) which extends BaseAdapter after following some tutorials, where I could see (via print statements) that at least the getCount() method is called, but my getView() method doesn't get called and my ListView doesn't appear on my app. What am I doing wrong? I assume that it's because I don't know how to make getView() run in the right place. It works fine when I use a normal ArrayAdapter with an array of strings.

Here is how I tried populating the ListView in my MainActivity class:

CustomAdapter customAdapter = new CustomAdapter();
ListView lv1 = (ListView) findViewById(R.id.listView);
lv1.setAdapter(customAdapter);
customAdapter.notifyDataSetChanged();

Now, here is my CustomAdapter class:

import java.util.ArrayList;
import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter {

    List<foodItem> mItemList = getDataForListView();

    public CustomAdapter(List<foodItem> list){
        this.mItemList = list;
        // tried this as an alternative
    }

    @Override
    public int getCount() {
        return mItemList.size();
    }

    @Override
    public foodItem getItem(int arg0) {
        return mItemList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup parent) {
        System.out.println("Item list size: " + mItemList.size()); // doesn't show anything in console

        if(arg1==null) {
            LayoutInflater inflater = (LayoutInflater) LayoutInflater.from(parent.getContext());
            arg1 = inflater.inflate(R.layout.listrow, parent ,false);
// R.layout.listrow is an XML file defined and formatted at 60sp tall
// as that is how tall I want each item in the ListView to be
        }

        TextView foodItem = (TextView)arg1.findViewById(R.id.foodTitle);
        TextView likeCount = (TextView)arg1.findViewById(R.id.likeCount);
        foodItem i = mItemList.get(arg0);
        foodItem.setText(i.getItem());
        likeCount.setText(i.getLikes());

        return arg1;
    }

    public List<foodItem> getDataForListView() {

        List<foodItem> itemList = new ArrayList<foodItem>();
// Test method populating with some data
        for(int i=0; i<50; i++) {
            itemList.add(new foodItem(i, "Item " + i));
        }
        return itemList;
    }
}

I also tried adding my own constructor, but the result was the same. Any help would be greatly appreciated!

EDIT: The layout for my main activity:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.inventoryList.MainActivity" >

    <ListView
        android:id="@+id/foodOptions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp" />

</RelativeLayout>

Last, here is my listrow.xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="60sp"
    android:orientation="horizontal"
    android:padding="5sp" >

    <TextView
        android:id="@+id/foodTitle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="12sp"
        android:text="123"
        android:gravity="center" />

    <TextView
        android:id="@+id/likeCount"
        android:layout_width="12sp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:text="@+string/likeCount"
        android:textSize="10sp"
        tools:ignore="SmallSp" />

</RelativeLayout>

Solution

  • I fixed it. My Main Activity has three tabs, and for each I set a tab listener that changes what's in the adapter. I had assumed that these tab listeners wouldn't be invoked upon starting the app, but apparently that was not true. In consequence, from things I had commented out in testing, I had been called setAdapter() and passing in an empty adapter.

    Thanks for the help all, sorry that it turned out to be a mistake in code that I hadn't provided.