listview没有正确填充/不可见

时间:2015-07-03 00:08:40

标签: java android android-layout listview android-listview

我有自定义列表适配器的活动。如果填充列表视图的arraylist为空,则向用户显示文本框,否则文本框将变为不可见,并显示列表视图。每次arraylist为空时都会出现文本框,但是当它不是时,我会出现一个空白屏幕。我不确定这是因为我的列表视图是不可见的还是因为它没有正确填充。昨天这个工作非常好,但是从那以后我对这个课程做了很多改动,我不确定是哪一个引起了这个问题。我一直在尝试按照过去几个小时的代码来解决这个问题,我注意到如果我在适配器的log方法中添加getView()调用,则不显示,如果我在它显示的构造函数中添加它。我还确保调用适配器并使用log在if语句中输入正确的条件。我希望你们中的一个能够发现问题,因为我不能。

调用适配器的方法:

 private void PopulateLister(ArrayList<BetDisplayer> listwriterr) {
    Log.d("POPULATING", listwriterr.toString());
    if (listwriterr.isEmpty()) {
        TextView emptybet = (TextView) findViewById(R.id.nobetstxtbox);
       // emptybet.setText("None of your bets have been settled yet.");
        emptybet.setVisibility(View.VISIBLE);
        ListView listView = (ListView) findViewById(R.id.betslistviews);
        listView.setVisibility(View.GONE);
    }
    else if (listwriterr.size() > 0){
        TextView emptybet = (TextView) findViewById(R.id.nobetstxtbox);
        emptybet.setVisibility(View.GONE);
        ListView listView = (ListView) findViewById(R.id.betslistviews);
        listView.setVisibility(View.VISIBLE);
        if (listwriterr.get(0).getStatus().equals("open")){
            ArrayAdapter<BetDisplayer> adapter = new MyListAdapter1();
            listView.setAdapter(adapter);
        }
        else {
            ArrayAdapter<BetDisplayer> adapter = new MyListAdapter2();
            listView.setAdapter(adapter);
        }



    }
}

活动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;


    }
}

4 个答案:

答案 0 :(得分:1)

您的SwipeRefreshLayout有2个子视图,如果我没记错,这会导致显示问题。你应该有这样的东西:

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

当显示列表或文本时,这是拉动刷新工作的唯一方法,然后您还必须覆盖canChildScrollUp()。祝你好运。

否则将文本放在SwipeRefreshLayout之外,只是将ListView作为它的唯一子项,但是当显示文本时,将无法刷新以进行刷新。

答案 1 :(得分:0)

来自AbsList的

getView()是导致问题的主题之一

像我在这里写的那样研究我的例子:(转到gitHub代码吧&#39; s&gt;

ListAdapter & AbsView + ViewHolder Pattern

如果您有任何进一步的问题,或者您不理解某事,请

在你的情况下(视图不是一个一个地替换项目,而是重复使用它们中的一部分,所以你需要检查它的视图你想要消失或可见并尝试在getView()方法中更改它)

从getView()中拉出任何外部的方法(构成视图的部分是不必要的和冗余的)它们应该形成PlaceHolder

这里有更多的说明 PROBABLY YOU ARE NOT INFLATING YOUR VIEW PROPERLY

答案 2 :(得分:0)

ArrayAdapter的'getCount'方法应该返回适​​配器使用的数组的大小。 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;
    }

答案 3 :(得分:0)

男孩抱歉女孩,当我站起来可能代码将完全可操作:)从用户干扰中受到更多伤害我需要充电我的指挥:)晚上好或晚上好!玩得开心