2015-07-03 77 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

男孩女孩难过的时候我会站立可能的代码将完全可操作:)采取从用户的干扰,我需要充电我的导体:)晚安全部或者一天太多的伤害!玩得开心