2012-03-23 107 views
24

我正在使用其搜索API與Twitter進行交互的Android應用程序。 Everythings運行良好,除了當我想使用ListView顯示結果時,只顯示第一個結果。Android - ListView只顯示第一個結果

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(); 

上面的代碼片段展示瞭如何將結果添加到適配器並將此適配器設置爲listview,我做錯了什麼?

+0

什麼是關於arrayList.size()的LogCat? – 207 2012-03-23 03:37:04

+0

我有n個元素(大約15),它甚至可以正確地打印出它們! – Rorchackh 2012-03-23 03:41:07

+1

這可能不是問題,但你不必調用resultAdapter.notifyDataSetChanged()。嗯,這個清單包括15個項目,但只有第一個顯示..第一個顯示正確嗎?你在TwitterJSONResults類中重寫toString()嗎? – 207 2012-03-23 03:52:29

回答

93

不要把ListView的滾動型的內部:)

+0

好的,但現在我失去了滾動!我的視圖由一個包含ListView的LinearLayout組成。 ListView本身可以滾動,但LinearLayout不能,所以LinearLayout中超出ListView的項目不能被查看 - 取決於用戶的屏幕大小。我想一個人不應該在一個佈局中的ListView後包含項目! – Tom 2012-07-27 22:40:08

+5

如果您仍然在尋找解決方案,請http://nex-otaku-en.blogspot.com/2010/12/android-put-listview-in-scrollview.html提供了一種修復ListView高度的方法所以它會顯示所有項目。 – dymk 2012-09-04 23:31:07

+1

拯救了我的一天! – ramesh 2013-07-25 09:55:45

3

事實證明,我的Java代碼中的所有東西都完美運行。問題是這是使用內的列表視圖一個滾動視圖,這通常是一個壞主意。它導致列表視圖忽略

android:layout="wrap_content" 

因此,內容在那裏,它只是沒有顯示。從我的XML文件中刪除滾動視圖後,所有的工作文件。

非常感謝207的支持:)

33

它不是一個答案。您可以在ScrollView中使用ListView。閱讀:http://kennethflynn.wordpress.com/2012/09/12/putting-android-listviews-in-scrollviews/

+2

該解決方案非常完美。謝謝! – JesusS 2013-06-20 10:58:38

+3

不客氣! – iProgrammer 2013-06-20 12:43:49

+0

謝謝。我有一個滑出菜單,如果沒有滾動視圖,它將無法正確滾動。這個解決方案適用於我無法避免的罕見情況。 – LoungeKatt 2013-08-04 18:54:35

7

感謝@ 207我意識到,在我的情況下,問題是,因爲我是用NestedScrollView,所以對我來說,使用

android:fillViewport="true" 

的解決方案在這裏我的代碼:

<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> 
+0

這幫了我! – pblead26 2018-03-03 21:44:49

2

我有像你這樣的錯誤,當把列表視圖內滾動查看。我的解決方案是以下爲:

1.創建一個自定義列表視圖,其非滾動

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.使用上述自定義類xml文件

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

它適用於我所有的操作系​​統版本。 希望最適合你。