0

So I am rather confused right now. I am using an XML layout so I can show an empty view like so:

<?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="fill_parent" >

    <RelativeLayout
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="@string/samples_empty"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </RelativeLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

    </ListView>

</RelativeLayout>

As you have probably seen a billion times here on so.

So I do setContentView(R.layout.foo) and it works the first time, but if I return to this Activity (As in onPause has been called and then onResume) I get this:

alt text

I call notifyDataSetChanged(); on the adapter and that works fine, what I don't get is why its being drawn twice?

Its not like I am creating a new ListView and then adding it to the view, I'm sure I would know about it if I was.

The getView method of the adapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    RecordView av;
    if(convertView == null){
        av = new RecordView(mCtx, this, mRecords[position], position);
    }else{
        av = (RecordView) convertView;
        av.setRecord(mRecords[position]);
    }
    return av;
}

This would be what it would look like normally...

alt text

NOTE

This doesn't seem to be happening every single time, and doesn't happen on a single event happening, but when it does, its when I return from another screen.

UPDATE

I noticed that when I had another activity on top (something that was transparent, like facebook chat heads, then I could see that the problem had occurred then, It doesn't seem to happen on onResume, but more likely on onPause which I actually don't do anything in.

FabianCook
  • 20,269
  • 16
  • 67
  • 115
  • Added. Anything else which would be informational? – FabianCook Jun 26 '13 at 10:38
  • Oh bam bam. I think I know. I might be inflating the view twice D: – FabianCook Jun 26 '13 at 10:39
  • Oh nope I call `this.removeAllViews();` – FabianCook Jun 26 '13 at 10:39
  • Where you call it and why? pls put this code here – shem Jun 26 '13 at 10:41
  • In `onResume` as the data would have changed on the other screen. – FabianCook Jun 26 '13 at 10:43
  • Nope, I am just restarting the activity when the user returns until I can find a solution. – FabianCook Jun 28 '13 at 00:27
  • You can test whether using the convertView is really the cause of the problem (I think it is) by changing `getView` so that it *always* returns a new RecordView. I think you'll see the problem disappear. But even if it does, you should revert `getView` to its current definition, and then figure out what's going on in `RecordView.setRecord`. (And since RecordView is apparently a custom class, you should post the code for it here.) – Dan Breslau Jun 28 '13 at 00:49
  • RecordView is a rather large class. I will have a look into it when I get home later tomorrow. – FabianCook Jun 28 '13 at 01:00
  • a little performance tip never set the `width/height` of `ListView` to `wrap_content` – Muhammad Babar Jun 28 '13 at 06:30

2 Answers2

1

try changing android:layout_height="wrap_content" of listview to android:layout_height="fill_parent"

drarkayl
  • 1,017
  • 7
  • 17
  • could you post a screenshot of the app before you return to it. Like when it works as expected – drarkayl Jun 26 '13 at 10:47
  • this is not a matter of height here. The layouts are probably inflated twice from your adapter. Check to see that you import your layouts once properly and enable recycling. – Akah Jan 25 '16 at 12:02
1

You have this problem because you are dynamically create the row view each time while the convertview still has the old view existing and it is being reused. To get around this problem, you should give an id to every view (that is, every child view in your RecordView)when you dynamically create them, for example a child textview in your RecordView class should be instantiated like this

TextView tv = new TextView(this.getContext());
tv.setId(1);
    ...

Then, in your getView:

if(convertView == null){
    av = new RecordView(mCtx, this, mRecords[position], position);
}else{
    av = (RecordView) convertView;
    av.findViewById(1).setText(mRecords[position]);
}

assuming your mRecords holds an array of String. If you have variant layout for different rows, you can provide a type to each view. See this for further details.

Community
  • 1
  • 1
Neoh
  • 15,906
  • 14
  • 66
  • 78
  • I am doing something rather similar to this if you check my post, I have a method that updates all the views. – FabianCook Jun 28 '13 at 00:27
  • Not at home sorry, thats where my code is, however it consists of just setting text fields and changing the colour of that bar depending on different things. – FabianCook Jun 28 '13 at 02:10
  • The only time the layout is inflated is when the view is created – FabianCook Jun 28 '13 at 02:12
  • I would suggest you to use convertView directly instead of casting to RecordView. By passing the view as parameter to an independent function like `setRecord(convertView)` you should be able to achieve the same purpose. See if that makes a difference. – Neoh Jun 28 '13 at 02:18
  • Okie tokies, I shall try it out. – FabianCook Jun 28 '13 at 02:25