Question

I am developing an android app with a ListView/ArrayAdapter combo with the items being dynamically added when it receives a broadcast from another service.

Here is the initial setup:

postAdapter = new PostAdapter(this.getActivity(), R.layout.item, posts);

ListView list = (ListView) V.findViewById(R.id.listView1);

list.setAdapter(postAdapter);       

There are some elements inside the list initially (stored inside posts).

The initial render of the listView from the adapter works perfectly.

However, when a broadcast is received from another service problem occurs.

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        String title= intent.getStringExtra("title");
        String content= intent.getStringExtra("content");
        String senderDesc= intent.getStringExtra("senderDesc");
        String receiverDesc= intent.getStringExtra("receiverDesc");

        Post newPost = new Post(title, content, senderDesc, receiverDesc);

        posts.add(newPost);

        postAdapter.notifyDataSetChanged();

    }
};  

At first, when the first 2 element is added, the listView still looks fine.

Until the listView is added to about 7 elements, then the next element is no longer rendered as the content of the element in question but the content of a previous element.

For example(pseudo for simplicity):

Below is the initial list: 1 2 3

And when the 4th, and 5th post is added, then the listView looks like: 1 2 3 4 5 as expected,

However, when the 6th and sometimes 7th item is added, then it will look like this 1 2 3 4 5 1 2 ...

Below is my custom PostAdapter function:

public class PostAdapter extends ArrayAdapter { int resource;

public PostAdapter(Context context, int resource, List<Post> posts) {
    super(context, resource, posts);
    this.resource = resource;
}

@Override
public View getView(int position,  View convertView, ViewGroup parent) {

    // Setting up postView
    RelativeLayout postView;

    Post post = getItem(position);

    String titleString = post.getTitle();
    String contentString = post.getContent();
    String senderDescString = post.getSenderDesc();
    String receiverDescString = post.getReceiverDesc(); 

    if(convertView == null) {

        postView = new RelativeLayout(getContext());
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li;

        li = (LayoutInflater)getContext().getSystemService(inflater);
        li.inflate(resource, postView, true);

        TextView contentView = (TextView) postView.findViewById(R.id.postContent);

        contentView.setText(contentString);

    } else {
        postView = (RelativeLayout) convertView;
    }

    // Check if the post has been previously populated;


    return postView;
}

}

I have spend over 10 hours on this and I don't know what is causing the problem.

If expert can point it out to me I really appreciate.

Thank you, Dennis

Was it helpful?

Solution

I think View-reused make this happen, put the following method below getView() method comment, it will works. BTW, you should use ViewHolder pattern in AdapterView if you have a lot of items to display

TextView contentView = (TextView) postView.findViewById(R.id.postContent);

contentView.setText(contentString);

OTHER TIPS

The problem is the else part when convertView is not null, do else { postView = (RelativeLayout) convertView; setText(contentString); }

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top