2014-10-18 84 views
0

我有這個序列的佈局:imageview,ratingbar和tabhost(包含用戶和信息的審查)。對於用戶的tabhost評論,我使用自定義的listview,並且我成功地顯示了用戶的所有評論。但我的問題,你可以看到下面的圖片(列表視圖身高只有1項包裹的內容,如果我把它的滾動,它可以加載所有數據)ListView只顯示1項

http://pbrd.co/1wctFFz

而且我想使這個佈局(它顯示多個項目,作爲父母的身高)

http://pbrd.co/1wctBFZ

這裏我的佈局XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <!-- HEADER --> 
    <LinearLayout 
     android:id="@+id/header" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_centerHorizontal="true" 
     android:background="#dcdcdc" 
     android:paddingTop="5dp" 
     android:paddingBottom="5dp" 
     android:layout_alignParentTop="true"> 
     <ImageView 
      android:id="@+id/ivplacedetail" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:src="@drawable/search"/> 
     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Name" /> 
     <RatingBar 
      android:id="@+id/ratingBar1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" />  

    </LinearLayout> 
    <!-- FOOTER --> 
    <LinearLayout 
     android:id="@+id/footer" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentBottom="true" 
     android:background="#a9d633"> 
     <Button 
      android:id="@+id/btnhome" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_marginLeft="2dp" 
      android:layout_marginRight="2dp" 
      android:layout_marginTop="2dp" 
      android:layout_marginBottom="2dp" 
      android:text="Home" 
      android:background="@layout/round_border"/>   
     <Button 
      android:id="@+id/btnbookmark" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_marginLeft="2dp" 
      android:layout_marginRight="2dp" 
      android:layout_marginTop="2dp" 
      android:layout_marginBottom="2dp" 
      android:text="Bookmark" 
      android:background="@layout/round_border"/> 
     <Button 
      android:id="@+id/btnnearby" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_marginLeft="2dp" 
      android:layout_marginRight="2dp" 
      android:layout_marginTop="2dp" 
      android:layout_marginBottom="2dp" 
      android:text="Nearby" 
      android:background="@layout/round_border"/>    
    </LinearLayout>  
    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@+id/footer" 
     android:layout_below="@+id/header">  
     <TabHost 
      android:id="@android:id/tabhost" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 

       <TabWidget 
        android:id="@android:id/tabs" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" /> 

       <FrameLayout 
        android:id="@android:id/tabcontent" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"/> 

      </LinearLayout> 
     </TabHost>       
    </ScrollView> 
</RelativeLayout> 

我LIS tview自定義佈局在這裏

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

    <ImageView 
     android:id="@+id/imguser" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="1dp" 
     android:src="@drawable/atmicon" 
     android:scaleType="fitCenter"> 
    </ImageView> 

    <TextView 
     android:id="@+id/lbluser" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginTop="3dp" 
     android:layout_marginBottom="1dp" 
     android:layout_toRightOf="@+id/imguser" 
     android:textColor="#4068ec" 
     android:text="Nama User"> 
    </TextView> 
    <TextView 
     android:id="@+id/lblwaktu" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/imguser" 
     android:layout_below="@+id/lbluser" 
     android:layout_marginLeft="5dp" 
     android:layout_marginTop="1dp" 
     android:layout_marginBottom="1dp" 
     android:text="Waktu Review"> 
    </TextView> 
    <TextView 
     android:id="@+id/lblreview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/imguser" 
     android:layout_below="@+id/lblwaktu" 
     android:layout_marginLeft="5dp" 
     android:layout_marginTop="1dp" 
     android:layout_marginBottom="1dp" 
     android:text="isi Review">   
    </TextView> 
</RelativeLayout> 

我的ListView佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/lvreview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

</LinearLayout> 

有人可以幫我解決我的佈局?

+0

當您將其登錄到適配器中時,您會得到10或1個什麼值?問題可能是該適配器在循環完成之前填充 – Elltz 2014-10-18 18:04:39

+0

已顯示所有數據(查看),但問題現在它的高度僅僅是1個數據(查看),鏈接顯示 – Kaizternn 2014-10-18 19:11:20

回答

0

首先聲明你的適配器在主類

private ListViewReviewAdapter reviewAdapter; 

其次,保證第一初始化適配器,然後將數據添加到它,即使arruserreview是空

reviewAdapter = new ListViewReviewAdapter(getBaseContext(), arruserreview); 
lvreview.setAdapter(reviewAdapter); 

在你的for循環請執行以下操作:

for (int i=1;i<=10;i++) { 
    UserReviewClass userreview=new UserReviewClass(String.valueOf(i),"Budi"+String.valueOf(i),"10/19/2014","Tempatnya lumayan. Pelayanan yang diberikan baik"); 
    arruserreview.add(userreview); 
    reviewAdapter.notifyDataSetChanged(); 
} 

所以你會有這樣的事情:

ArrayList<UserReviewClass> arruserreview = new ArrayList<UserReviewClass>(); 
ListView lvreview ; 
private ListViewReviewAdapter reviewAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_place_review); 

    lvreview  = (ListView) findViewById(R.id.lvreview); 
    reviewAdapter = new ListViewReviewAdapter(getBaseContext(), arruserreview); 
    lvreview.setAdapter(reviewAdapter); 

    for (int i=1;i<=10;i++) { 
     UserReviewClass userreview=new UserReviewClass(String.valueOf(i),"Budi"+String.valueOf(i),"10/19/2014","Tempatnya lumayan. Pelayanan yang diberikan baik"); 
     arruserreview.add(userreview); 
     reviewAdapter.notifyDataSetChanged(); 
    } 


} 
+0

在數組已知之前初始化和設置陣列適配器不是必需的,在執行for-loop時,也不需要notifyDataSetChanged()。 – mjp66 2014-10-18 18:13:51

0

正如丹尼爾M提到的那樣,聲明你的適配器(這只是各種原因的好習慣)。

private ListViewReviewAdapter reviewAdapter; 

一旦arruserreview陣列中的for循環,數組傳遞到您的適配器,並設置它構造:

reviewAdapter = new ListViewReviewAdapter(this, arruserreview); 
lvreview.setAdapter(reviewAdapter); 

在你的陣列適配器,改變你的構造函數:

public ListViewReviewAdapter(Context context, ArrayList<UserReviewClass> results) { 
    DataProcessorResult = results; 
    mycontext = context; 
    myInflater = LayoutInflater.from(context); 
} 

由於您在reviewAdapter和構造函數中將上下文作爲參數傳遞,因此可以從適配器類中刪除以下行:

final Context mycontext=parent.getContext();