Question

In my Android app I'm trying to build my very first custom listadapter. I now managed to get it to display one item, but unfortunately one item only where there are 8 in the list. My adapter looks like this:

public class PossibilitiesAdapter extends BaseAdapter {
    private AdapterCallbacks callbacks;
    private List<String> possibilitiesList = new ArrayList<String>();
    private LayoutInflater inflater;

    public PossibilitiesAdapter(Context context) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setPossibilitiesList(List<String> possibilitiesList) {
        for (String possibility : possibilitiesList) {
            addItem(possibility);
        }
    }

    public void addItem (final String item) {
        possibilitiesList.add(item);
        notifyDataSetChanged();
    }

    private class ViewHolder {
        TextView possibilityTitle;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.list_item_posibility, viewGroup, false);
        holder.possibilityTitle = (TextView) convertView.findViewById(R.id.text_possibility);
        holder.possibilityTitle.setText(possibilitiesList.get(position));

        return convertView;
    }

    @Override
    public int getCount() {
        return possibilitiesList.size();
    }
    // And here are some more things like getItemId() and getItem()
}

and I set the possibilitiesList from my fragment like so:

List<String> list = Arrays.asList(getResources().getStringArray(R.array.the_possibilities));
Log.e(this, new Integer(list.size()).toString()); // Outputs 8, so there are enough in the array.
adapter.setPossibilitiesList(list);

My fragment_possibilities.xml looks like this:

<ListView
    android:id="@+id/list_possibilities"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

and my list_item_possibility.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/text_possibility"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

So the problem is that it only displays the first item, and nothing more. Does anybody know what the problem is and how I can fix it?

Was it helpful?

Solution

Are you nesting your list inside a ScrollView? It would be a typically behaving that it only shows one item. Try to avoid it o use the following method.

/**
 * read all adapter views and calculate total length
 *
 * @param listView
 */
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }

    int totalHeight = 0;
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
    }

    setListViewHeight(listView, totalHeight);
}

/**
 * set listview height
 *
 * @param listView
 * @param height
 */
public static void setListViewHeight(ListView listView, int height) {
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = height + (listView.getDividerHeight() * (listView.getAdapter().getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

No that will not work, as I said, you cant put a ListView Inside a ScrollView.

Try this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
    android:id="@+id/list_possibilities"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

OTHER TIPS

Change the method addItem and remove notify from there , add notify after the for in the metghod setList

The most possible case is you are getting possibilitiesList.size() as 1 when you execute getCount() method. Print a log to check the value.

your getView Method is implemented wrong, more precisely the view holder pattern. im guessing you see the right amount of entries but all with same content

try this:

@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
    ViewHolder holder = null;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item_posibility, viewGroup, false);
        holder = new Holder();
        holder.possibilityTitle = (TextView) convertView.findViewById(R.id.text_possibility);
            convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    }

    holder.possibilityTitle.setText(possibilitiesList.get(position));

    return convertView;
}

and somehow i think your setPossibilitesMethod seems odd. id probably use something like this

public void setPossibilitiesList(List<String> possibilitiesList) {
    this.possibilitiesList = possibilitiesList;
    notifyDataSetChanged();
}

public void addItem (final String item) {
    possibilitiesList.add(item);
    notifyDataSetChanged();
}

public void addItems (List<String> possibilitiesList) {
    this.possibilitiesList.addAll(possibilitiesList);
    notifyDataSetChanged();
}

and you can shorten the code for the retrieval of the inflaterService like this:

public PossibilitiesAdapter(Context context) {
    inflater = LayoutInflater.fromContext(context);
}

In addition you dont have to cast it

updated

@Override
public int getCount(){
    for (String possibility : this.possibilities){
        System.out.println(possibility);
    }
    int size = this.possibilities();
    System.out.println("getCount: " + size);
    return size;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top