2

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>
MikeM
  • 342
  • 2
  • 10

4 Answers4

1

You are Re-declaring List<foodItem> itemList in getDataForListView. This overrides the member variable. Change to

itemList = new ArrayList<>();

Also you should do this in the constructor, not a member method and prepending an m (so mItemList) to your member variables is good style and helps prevent this problem. So all together:

private class CustomAdapter extends BaseAdapter {
    private List<foodItem>  mItemList; //camel case isn't usually applied to classes os this should be FoodItem

    public CustomAdapter() {
        mItemList = new ArrayList<>();
        for (...) {
            ...
        }
    }

    ...
}

Better still, pass the List to the Constructor:

private class CustomAdapter extends BaseAdapter {
    private List<FoodItem> mItemList;

    public CustomAdapter(List<FoodItem> itemList) {
        mItemList = itemList;
    }
    ...
}
h0x0
  • 485
  • 3
  • 11
  • yes, but no, because the method returns a list which is affected to the member variable – njzk2 Feb 06 '15 at 17:21
1

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.

MikeM
  • 342
  • 2
  • 10
1

I had the similar problem, and I resolved by putting

adapter.notifyDataSetChanged();

OUTSIDE of my try-catch block.

user2807083
  • 2,962
  • 4
  • 29
  • 37
0

Implement a constructor for your adapter so you can pass an items list to it:

public CustomAdapter(List<foodItem> itemList) {
    this.itemList = itemList;
}

Create your list beforehand and pass it to the adapter constructor:

ArrayList<foodItem> items = buildItemsList();
CustomAdapter customAdapter = new CustomAdapter(items);
ListView lv1 = (ListView) findViewById(R.id.listView);
lv1.setAdapter(customAdapter);

If you need a mutable adapter, implement a method (in the adapter) to add items and notify data set changes after that:

public void add(foodItem item) {
    itemList.add(item);
    notifyDataSetChanged();
}

You can do the same to remove and update items, just remember to notify changes so the list view will refresh.

Juanjo Vega
  • 1,410
  • 1
  • 12
  • 20
  • how is this related to the problem the OP experiences? – njzk2 Feb 06 '15 at 17:32
  • 1
    I'm pretty sure that the reason causing the empty list is because he is not invoking notifyDatasetChanged, but as I don't have reputation enough, I can't comment to ask him to check if "getCount" is returning zero. So, I can only answer, and rules say that answers must be used to solve the problem. I posted what, according to my experience, is the recomended way to work with lists avoiding problems like the one he is experiencing. – Juanjo Vega Feb 06 '15 at 17:39
  • @JuanjoVega getCount() returns 50 every time that it runs. I put in a constructor as you said, and added the call to notifyDataSetChanged() after setting the adapters and updated my code above. Why might getView( ... ) never get called? My print statements in that method never get hit, but it seems that this method must occur for the listview to populate. – MikeM Feb 06 '15 at 19:40