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?
Don't put ListView inside of a ScrollView :)
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.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With