Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - ListView only show the first result

I am working on an android app which interacts with Twitter using their search API. Everythings works well except that when I want to show the result using a ListView, only the first result is shown.

ArrayList<TwitterJSONResults> arrayList = new ArrayList<TwitterJSONResults>(data.getResults().size());
for (int i = 0; i < data.getResults().size(); i++) {
    arrayList.add(data.getResults().get(i));
}

ArrayAdapter<TwitterJSONResults> resultAdapter = new ArrayAdapter<TwitterJSONResults>(
                this, android.R.layout.simple_list_item_1, arrayList);
listview.setAdapter(resultAdapter);
resultAdapter.notifyDataSetChanged();

The code snippet above show how I add the results to the adapter and set this adapter to the the listview, What am I doing wrong?

like image 499
Rorchackh Avatar asked Mar 23 '12 03:03

Rorchackh


4 Answers

Don't put ListView inside of a ScrollView :)

like image 102
207 Avatar answered Nov 13 '22 13:11

207


You can use ListView in ScrollView.

public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        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();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}
}

Call this function right after you change ListView items, like that:

Utility.setListViewHeightBasedOnChildren(myListView);

Read source.

like image 28
iProgrammer Avatar answered Nov 13 '22 15:11

iProgrammer


Thanks to @207 I realized that the problem in my case was because I was using NestedScrollView, so for me the solution was used

android:fillViewport="true"

Here my code:

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v4.widget.NestedScrollView>
like image 12
Jorge Casariego Avatar answered Nov 13 '22 15:11

Jorge Casariego


I have had this error like you when put listview inside ScrollView. And my solution is following as:

1. Create a custom listview which is non scrollable

public class NonScrollListView extends ListView {

public NonScrollListView(Context context) {
    super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();    
}

}

2. Use above custom class for xml file

  <xx.xx.NonScrollListView
        android:id="@+id/lv_nonscroll_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </xx.xx.NonScrollListView>

It worked well on all OS-version for me. Hope best for you.

like image 5
Hai Rom Avatar answered Nov 13 '22 14:11

Hai Rom