Vlad Alexeev
Vlad Alexeev

Reputation: 2224

notifyDataSetChanged() not working after add

This happens very often , but I really don't know what to do now.

Here's the code from fragment :

public class MainFragment extends Fragment {

private ListView listView;
private static TicketsAdapter ticketsAdapter;
private static ArrayList<Ticket> tickets = new ArrayList<Ticket>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_main, container, false);

    listView = (ListView) view.findViewById(R.id.list_tickets);        
    ticketsAdapter = new TicketsAdapter(tickets);
    listView.setAdapter(ticketsAdapter);

    return view;
}
public static void addTicket(Ticket ticket){
    tickets.add(ticket);
    ticketsAdapter.notifyDataSetChanged();
    Log.d(A.TAG,"tickets =" + tickets.size());
}

}

now, the Log shows that number of tickets increment, but ListView is not being updated

here's the adapter :

public class TicketsAdapter extends BaseAdapter {

    private ArrayList<Ticket> tickets = new ArrayList<Ticket>();
    private LayoutInflater lInflater;
    private ViewHolder viewHolder;

    public TicketsAdapter(ArrayList<Ticket> tickets){
        this.tickets.clear();
        this.tickets.addAll(tickets);
        lInflater = (LayoutInflater) MainActivity.getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = lInflater.inflate(R.layout.list_item, parent, false);
            viewHolder = new ViewHolder();

            //finding views etc

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }       
        Ticket ticket = (Ticket) getItem(position);

         //Showing data on views

        return convertView;
    }

    @Override
    public int getCount() {
        return tickets.size();
    }

    @Override
    public Object getItem(int position) {
        return tickets.get(position);
    }

    @Override
    public long getItemId(int position) {
        return Long.parseLong(tickets.get(position).getId());
    };

    static class ViewHolder{
        //bunch of views
    }
}

Upvotes: 0

Views: 139

Answers (1)

Danilo Prado
Danilo Prado

Reputation: 2279

You are creating a new list inside your adapter and just adding the items from the fragment list to it. The list inside your adapter is not referencing the list in your fragment (cause it is a new list), so your adapter can't know about changes in the outside list.

You can do something like:

    private ArrayList<Ticket> tickets;
    private LayoutInflater lInflater;
    private ViewHolder viewHolder;

    public TicketsAdapter(ArrayList<Ticket> tickets) {
        this.tickets = tickets;
        lInflater = (LayoutInflater) MainActivity.getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

If you do it this way, your adapter will be referencing the same list that comes from your fragment (cause you are not sending a new list, but a reference to the list in your fragment class) and changes made in your fragment will be reflected in your adapter once you call notifyDataSetChanged().

Hope this helps. =)

Upvotes: 1

Related Questions