1

I currently have a listview with utilizes an ArrayAdapter. I start off with no data and dynamically add data to it using user input. For some reason, when I call adapter.notifyDataSetChanged() the list view does not refresh. I have for test purposes added some basic data before it and it does simply, therefore the adapter is working correctly. This is the first time implementing my own adapter so i am not sure if i am doing something wrong or not. Thanks. Code below.

My Adapter:

public class FeedListviewAdapter extends ArrayAdapter<FeedPostingLayout> {
    // add the feed posting layout
    ArrayList<FeedPostingLayout> postingLayout;

    public FeedListviewAdapter(Context context, int textViewResourceId,
            ArrayList<FeedPostingLayout> postingLayout) {
        super(context, textViewResourceId, postingLayout);
        this.postingLayout = postingLayout;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.post_layout, null);
        }

        FeedPostingLayout layout = postingLayout.get(position);
        if (layout != null) {
            TextView post = (TextView) view.findViewById(R.id.postTextView);
            TextView time = (TextView) view
                    .findViewById(R.id.postTimeTextView);

            if (post != null) {
                post.setText(layout.feedPost);
            }

            if (time != null) {
                time.setText(layout.feedTime);
            }
        }

        return view;
    }

}

Where I create a new array, add it to my data, and then refresh adapter:

FeedPostingLayout test = new FeedPostingLayout(post, currentTimePost);
        data.add(test);
        feedAdapter.notifyDataSetChanged();

Thanks.

0

2 Answers 2

1

Try to add directly at ArrayAdapter.

For example:

FeedListviewAdapter adapter=new FeedListviewAdapter(ctx,id,data);
FeedPostingLayout test = new FeedPostingLayout(post, currentTimePost);
adapter.add(test);
adapter.notifyDataSetChanged();
Sign up to request clarification or add additional context in comments.

Comments

0

Just for once try replacing the arraylist, say something like

setPostingLayout(ArrayList<FeedPostingLayout> postingLayout) {
    this.postingLayout = postingLayout;
    notifyDataSetChanged();
} 

Reason being the arraylist have different copies changes made to data will not be reflected to postingLayout.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.