Reputation: 1181
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.
Upvotes: 1
Views: 2682
Reputation: 145
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();
Upvotes: 1
Reputation: 1236
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.
Upvotes: 0