0

I have an activity with a custom list adapter. If the arraylist populating the listview is empty I display a textbox to the user, otherwise the textbox becomes invisible and i display the listview. The textbox appears every time the arraylist is empty, however when it is not I get a blank screen. I'm not sure if this is because my listview is invisible or because it isn't populated properly. This worked perfectly fine yesterday but I've made a lot of changes to the class since then and I'm not sure which one caused this problem. I've been trying to follow the code for the past couple hours to solve this, I noticed that if I add a log call in the getView() method of the adapter, it isn't displayed, if I add it in the constructor it shows fine. I've also made sure the adapter gets called and enters the proper condition in the if statement by using log. I'm hoping one of you will be able to spot the problem as I can't.

Method which calls adapter :

 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 of activity :

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

Adapter :

 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;


    }
}
Alk
  • 4,240
  • 7
  • 30
  • 82
  • Your logging tests indicate that 'getView' is not being called (at least in your MyListAdapter1 instance). Things to check: Does the array adapter have any data to display? (check the size of openbetsarray in the constructor). Is the array adapter reporting it's count correctly? (check getCount is returning the size of openbetsarray). Is MyListAdapter1 or MyListAdapter2 being created? – Sound Conception Jul 03 '15 at 00:39
  • @SoundConception I placed a log call in the constructor of the adapter, which returns openbetsarray.size() and it correctly returned 2 meaning that the adapter gets created and that it has data to use. I didn't understand your point about the getCount method and how I am meant to use it. Also, is there any chance the listview isn't visible? – Alk Jul 03 '15 at 00:46
  • as i said don't need Override getCount() his adapter inherits from ArrayAdapter – ceph3us Jul 03 '15 at 01:14
  • @Tomasz Only if mankee used one of the constructors like ArrayAdapter(Context, int, Object[]) or ArrayAdapter(Context, int, List), which supplies the data array then there is no need to override getCount. Otherewise the ArrayAdapter has no concept of what data you are using. – Sound Conception Jul 03 '15 at 01:53
  • I got very lots of complicated adapters in my app and I did not encounter any problems by adapters base cursor pages rare card view etc u can write your own adapter as they serve as Dao & pojo data holders. Most of those methods ate like child's playground u play them and they making most of us like zombies :=) – ceph3us Jul 03 '15 at 03:16

4 Answers4

1

Your SwipeRefreshLayout has 2 child Views and if I remember correctly, this causes display problems. You should have something like that instead:

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

It's the only way to have pull to refresh work when the list OR the text is shown, and then you also have to override canChildScrollUp(). Good luck.

Otherwise put the text outside of the SwipeRefreshLayout and just put the ListView as only child of it, but then there will be no swipe to refresh possible when the text is shown.

BladeCoder
  • 11,697
  • 2
  • 51
  • 46
0

getView() from AbsList is here one of the most causing problems topic

study my example as i wrote here: (go to gitHub Code it's with description)

ListAdapter & AbsView + ViewHolder Pattern

if you will have any further question or if you will not understand something ask

in youre case (view is displaing items not one by one but reusing part of them so u need to check up if its view you wanna make gone or visible and try to change it in getView() method )

pull out from getView() any methods outside (parts that make up the view are unnecessary and redundad) They should be formed PlaceHolder

more explonation here PROBABLY YOU ARE NOT INFLATING YOUR VIEW PROPERLY

Community
  • 1
  • 1
ceph3us
  • 6,591
  • 2
  • 34
  • 39
  • I use viewholders in my other activities, but this adapter or one very similar to this one worked before so I don't think there's a point in me implementing a viewholder here, its rather a very small mistake somewhere, a one line of code kind of thing – Alk Jul 03 '15 at 00:47
  • try change the line with layout inflater – ceph3us Jul 03 '15 at 00:48
  • Thus, you have the possibility of finding yourself your "very small mistake somewhere" – ceph3us Jul 03 '15 at 01:04
0

The 'getCount' method of an ArrayAdapter should return the size of the array being used by the adapter. This is used by the ArrayAdapter to know how many items it's dealing with. If this is returning 0 then it has no items to display and so 'getView' will not get called to supply any views.

The 'MyListAdapter1' constructor you have shown, does not take any parameters, so I assume you are obtaining 'openbetsarray' by some other means. The 'ArrayAdapter' needs to know that 'openbetsarray' is the data set it need to look at when determining how many views it needs to display. Override the 'getCount' method in your ArrayAdapter with something like:

@Override
    public int getCount(){
          // if openbetsarray is not null return it's length, else return 0
          return openbetsarray!=null ? openbetsarray.size: 0;
    }
Sound Conception
  • 5,006
  • 4
  • 25
  • 46
  • it returns 2, just like the log I used – Alk Jul 03 '15 at 01:10
  • don't need Override getCount() his adapter inherits from ArrayAdapter – ceph3us Jul 03 '15 at 01:13
  • Any other ideas, it appears the getView() method isn't getting called as no logs placed inside of it are displayed – Alk Jul 03 '15 at 01:15
  • Adapter.add(items); Adaper.notifyDatasetChanged(); if this will not get yr result its view or activity layout inflation. perioid. – ceph3us Jul 03 '15 at 01:29
  • @Tomasz "don't need Override getCount() his adapter inherits from ArrayAdapter". That simply defines the 'Type' of array the adapter expects. It does not assign Which array to consider when determining the count. Your later comment "Adapter.add(items);" or 'addAll' would address this by adding items to the ArrayAdapter's mObjects which you can see in the [ArrayAdapter source code](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.0_r1/android/widget/ArrayAdapter.java#ArrayAdapter). – Sound Conception Jul 03 '15 at 01:48
  • @SoundConception any other ideas? Is there a chance the visibility is gone as that's the only available option according to this http://stackoverflow.com/a/16338380/4979865 – Alk Jul 03 '15 at 01:51
  • Try adding a breakpoint in your PopulateLister and run debug in your IDE so you can step through and see exactly what happens as it runs line by line. – Sound Conception Jul 03 '15 at 02:02
  • @SoundConception i proposed this as a debug method to check if he got any view data. I still belive its cs – ceph3us Jul 03 '15 at 03:00
0

Boys sorry girls when I will stand-up probably the code will be fully operationally :) to take more damage from user interference I need recharge my conductor :) good night all or day too! Have fun

ceph3us
  • 6,591
  • 2
  • 34
  • 39