2015-07-03 82 views
0

我有一個自定義列表適配器的活動。如果填充列表視圖的數組列表爲空,則向用戶顯示文本框,否則文本框變爲不可見,並顯示列表視圖。每當數組列表爲空時,文本框就會出現,但當數組不存在時,我會看到空白屏幕。我不確定這是因爲我的列表視圖是不可見的還是因爲它沒有正確填充。這個工作昨天完全正常,但自那時以來我對這個類做了很多改變,我不確定哪一個導致了這個問題。我一直在嘗試遵循過去幾個小時的代碼來解決這個問題,我注意到如果我在適配器的getView()方法中添加一個log調用,它將不會顯示,如果我將它添加到構造函數中,它會顯示精細。我還確保調用適配器,並使用log在if語句中輸入正確的條件。我希望你們中的一個能夠發現問題,因爲我不能。listview填充不正確/不可見

方法,它調用適配器:

​​

活動的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayout23" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:orientation="vertical"> 
    <!-- Main ListView 
     Always give id value as list(@android:id/list) 
    --> 
    <fragment 
     android:id="@+id/fragment" 
     android:name="com.example.albert.betterapp.menu" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     tools:layout="@layout/fragment_menu" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="40dp" 
     android:layout_gravity="center" 
     android:orientation="horizontal" 
     android:weightSum="100"> 

     <LinearLayout 
      android:id="@+id/openbetslayout" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="49" 
      android:tag="" 
      android:background="@drawable/bettypeselector" 
      android:clickable="true"> 

      <TextView 
       android:id="@+id/openbetstxtview" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_gravity="center" 
       android:clickable="false" 
       android:gravity="center" 
       android:text="Open Bets" 
       android:textColor="#B4B5AE" 
       android:textStyle="bold" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="@drawable/seperator"> 

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/closedbetslayout" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="49" 
      android:tag="" 
      android:background="@drawable/bettypeselector" 
      android:clickable="true"> 

      <TextView 
       android:id="@+id/settledbetstxtview" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_gravity="center" 
       android:clickable="false" 
       android:gravity="center" 
       android:text="Settled Bets" 
       android:textColor="#B4B5AE" 
       android:textStyle="bold" /> 
     </LinearLayout> 
    </LinearLayout> 


    <android.support.v4.widget.SwipeRefreshLayout 
     android:layout_width="fill_parent" 
     android:id = "@+id/displayallbetsrefresh" 
     android:layout_height="wrap_content"> 
     <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="200dp" 
     android:id="@+id/nobetstxtbox" 
     android:text="You currently don't have any open bets saved." 
     android:textStyle="bold" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:visibility="gone" 
     android:textSize="17sp" 
     android:textColor="#067103"/> 
     <ListView 
      android:id="@+id/betslistviews" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/betcolor"></ListView> 
    </android.support.v4.widget.SwipeRefreshLayout> 


</LinearLayout> 

適配器:

private class MyListAdapter1 extends ArrayAdapter<BetDisplayer> { 
    public MyListAdapter1() { 
     super(DisplayAllBets.this, R.layout.activity_singletotalbet, openbetsarray); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     Log.d("TESTING", "123123123"); 
     itemView = convertView; 
     if (itemView == null) { 
      itemView = getLayoutInflater().inflate(R.layout.activity_singletotalbet, parent, false); 
     } 
     Button v = (Button) itemView.findViewById(R.id.detailsbutton); 
     BetDisplayer currentwriter = openbetsarray.get(position); 
     Log.d("TESTING", currentwriter.getSelections()); 
     Log.d("TESTING2", currentwriter.getSelections()); 
     String selections = currentwriter.getSelections(); 
     int numberofselections = 0; 

     for (int i = 0; i < selections.length(); i++) { 
      if (selections.charAt(i) == '/') { 
       numberofselections++; 
      } 
     } 
     if (numberofselections == 1) { 
      TextView descriptor = (TextView) itemView.findViewById(R.id.no); 
      descriptor.setText("Single"); 
     } else if (numberofselections == 2) { 
      TextView descriptor = (TextView) itemView.findViewById(R.id.no); 
      descriptor.setText("Double"); 
     } else if (numberofselections == 3) { 
      TextView descriptor = (TextView) itemView.findViewById(R.id.no); 
      descriptor.setText("Treble"); 
     } else { 
      TextView descriptor = (TextView) itemView.findViewById(R.id.no); 
      descriptor.setText("Accumulator" + "(" + numberofselections + ")"); 
     } 
     TextView status = (TextView) itemView.findViewById(R.id.status); 
     status.setText(currentwriter.getStatus()); 
     Log.d("ERRORDEBUG", currentwriter.toString()); 
     if (status.getText().toString().equals("open")) { 
      TextView winnings = (TextView) itemView.findViewById(R.id.winnings); 
      winnings.setText("-"); 
      /*LinearLayout displaylayout = (LinearLayout) itemView.findViewById(R.id.displaylayout); 
      displaylayout.setBackgroundColor(Color.parseColor("#FFFFFF")); 
      Button b = (Button) itemView.findViewById(R.id.detailsbutton); 
      b.setBackgroundResource(R.drawable.btnback);*/ 
     } 
     else if (status.getText().toString().equals("lost")) { 
      TextView winnings = (TextView) itemView.findViewById(R.id.winnings); 
      winnings.setText("0"); 
      /*LinearLayout displaylayout = (LinearLayout) itemView.findViewById(R.id.displaylayout); 
      Button b = (Button) itemView.findViewById(R.id.detailsbutton); 
      b.setBackgroundResource(R.drawable.btnbacklost); 
      displaylayout.setBackgroundResource(R.drawable.lost);*/ 
     } 
     else { 
      TextView winnings = (TextView) itemView.findViewById(R.id.winnings); 
      winnings.setText(Integer.toString(currentwriter.getReturns())); 
      /* Button b = (Button) itemView.findViewById(R.id.detailsbutton); 
      b.setBackgroundResource(R.drawable.btnback); 
      LinearLayout displaylayout = (LinearLayout) itemView.findViewById(R.id.displaylayout); 
      displaylayout.setBackgroundResource(R.drawable.won);*/ 
     } 
     TextView winningss = (TextView) itemView.findViewById(R.id.winningss); 
     winningss.setText(Integer.toString(currentwriter.getReturns())); 
     TextView stakes = (TextView) itemView.findViewById(R.id.stakes); 
     stakes.setText(Integer.toString(currentwriter.getStake())); 
     TextView tokens = (TextView) itemView.findViewById(R.id.tokens); 
     tokens.setText(currentwriter.getId().substring(0,10)); 
     TextView teams = (TextView) itemView.findViewById(R.id.teams); 
     TextView finalteams = (TextView) itemView.findViewById(R.id.actteams); 
     String userselectionssetter = currentwriter.getUserselections(); 
     String actteamssetter = currentwriter.getActualselections(); 
     teams.setText(userselectionssetter); 
     finalteams.setText(actteamssetter); 
     return itemView; 


    } 
} 
+0

你記錄測試結果表明,「getView」不會被調用(至少在你的MyListAdapter1實例)。需要檢查的事項:陣列適配器是否有任何要顯示的數據? (檢查構造函數中openbetsarray的大小)。陣列適配器報告它是否正確計數? (檢查getCount返回openbetsarray的大小)。 MyListAdapter1或MyListAdapter2是否被創建? –

+0

@SoundConception我在適配器的構造函數中放置了一個日誌調用,該函數返回openbetsarray.size(),並且它正確地返回2,表示適配器已創建並且具有要使用的數據。我不明白你對getCount方法的看法,以及我如何使用它。此外,有沒有可能的列表視圖不可見? – Alk

+0

正如我所說不需要覆蓋getCount()他的適配器從ArrayAdapter繼承 ceph3us

回答

1

你SwipeRefreshLayout有2個視圖,如果我沒記錯的話,這會導致顯示問題。你應該有這樣的事情,而不是:

<SwipeRefreshLayout> 
    <FrameLayout> 
     <ScrollView> 
      <TextView/> 
     </ScrollView> 
     <ListView/> 
    </FrameLayout> 
</SwipeRefreshLayout> 

這是有拉刷新工作將顯示在列表或文本時的唯一途徑,然後你還必須重寫canChildScrollUp()。祝你好運。

否則把文本SwipeRefreshLayout之外,只是把ListView控件作爲其唯一的孩子,但後來就沒有刷卡時顯示的文本刷新可能。

+0

是的這個作品,我可以有兩個swiperefreshlayouts呢? – Alk

+0

我想你可以,但你會有很多重複的代碼。 – BladeCoder

+0

它們可以重疊還是不可能 – Alk

0

getView()從AbsList在這裏造成一個最問題主題

研究我的例子,我在這裏寫下:(去GitHub的代碼它與說明)

ListAdapter & AbsView + ViewHolder Pattern

如果你有任何問題,或如果你不明白的地方問

你的情況(視圖是不是一個一個地顯示項目,而是重用它們的一部分,所以你需要檢查它的視圖是否想要變爲可見或嘗試在getView()方法中進行更改)

從getView ()以外的任何方法(份組成視圖是不必要的,redundad)它們應當形成佔位符這裏 PROBABLY YOU ARE NOT INFLATING YOUR VIEW PROPERLY

+0

我用viewholders在我的其他活動,但這種適配器或一個非常類似於此工作過,所以我不認爲有在我的地方,在這裏實現viewholder,其相當一個很小的錯誤點,代碼之類的話的一行 – Alk

+0

嘗試改變與佈局吹氣 – ceph3us

+0

行。因此,你必須「的地方非常小的失誤」發現自己的可能性 – ceph3us

0

更explonation的正在使用的適配器「getCount將」的ArrayAdapter的方法應該返回數組的大小。 ArrayAdapter使用它來知道它處理了多少個項目。如果這是返回0,那麼它沒有項目顯示,所以'getView'不會被調用來提供任何視圖。

你已經顯示的'MyListAdapter1'構造函數沒有使用任何參數,所以我假設你通過其他方法獲得'openbetsarray'。 'ArrayAdapter'需要知道'openbetsarray'是在確定需要顯示多少視圖時需要查看的數據集。覆蓋在你的ArrayAdapter的「getCount將」法的東西,如:

@Override 
    public int getCount(){ 
      // if openbetsarray is not null return it's length, else return 0 
      return openbetsarray!=null ? openbetsarray.size: 0; 
    } 
+0

它返回2,就像我使用的日誌 – Alk

+0

不需要覆蓋getCount()他的適配器從ArrayAdapter繼承 ceph3us

+0

任何其他的想法,它似乎getView()方法不會被調用,因爲沒有日誌放在它裏面顯示 – Alk

0

男孩女孩難過的時候我會站立可能的代碼將完全可操作:)採取從用戶的干擾,我需要充電我的導體:)晚安全部或者一天太多的傷害!玩得開心