Search code examples
androidlistviewbackgroundandroid-broadcast

notifydatasetchanged() not working properly


I have a listview and an Baseadapter but adapeter.notiydatasetchanged() is not working properly for me.....i get one adapter item more than two times in my listview :(

My adapter class extends baseadapter.

public class Event_update_adapter extends BaseAdapter {
    private StartActivity activity = null;
    private List<String> pokemonList = null;

    public Event_update_adapter(StartActivity activity, List<String> p) {
        super();
        this.activity = activity;
        this.pokemonList = p;
    }
    public void add(String data) {

        this.pokemonList.add(data);

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

    @Override
    public String getItem(int i) {
        return pokemonList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        final String pokemon = (String) getItem(position);

        if (view == null) {
            LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.event_updater_lv_item, viewGroup, false);
            holder = new ViewHolder();
            holder.name = (TextView) view.findViewById(R.id.event_update_in_list);

            view.setTag(holder);
        } else {
            // get view holder back
            holder = (ViewHolder) view.getTag();
        }

        // bind text with view holder content view for efficient use
        holder.name.setText(pokemon);

        return view;
    }

    private class ViewHolder {
        TextView name=null;
    }
}

Here is implementation of listview and adapter data :

public class StartActivity extends AppCompatActivity
{

  private Event_update_adapter adapter = null;
    private List<String> adapter_data = new Vector<>();
     private ListView mListView = null;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_start);
        this.mContext = this;
 initviews();

}


private void initviews() {
       // adapter_data = new Vector<String>();
        adapter = new Event_update_adapter((StartActivity) mContext,
                adapter_data);

        mListView = (ListView) findViewById(R.id.eventList);
        mListView.setAdapter(adapter);
}

Than using a receiver I am updating adapter data and calling notifydatasetchanged().

Receiver Code:

   BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // Grab the bundle from the intent.
            //Bundle bundle = intent.getExtras();

            //Initializes the ViewPager.
            if (intent.hasExtra(CommonForApp.UPDATE_LISTVIEW)) {

                adapter.add(intent.getStringExtra(CommonForApp.UPDATE_LISTVIEW));
                adapter.notifyDataSetChanged();
            }
}

Note: My receiver is working fine for other intents so their is no problem in receiver.

I am sending broadcast using Application context and from a background thread in service postexceute() function, but one item gets pushed more than two times atleast in listview!

Any help will be appreciated ! Thanks!


Solution

  • You're not giving the data to your adapter. You can add a method to the adapter to update the data:

    public void add(String data) {
    
        this.pokemonList.add(data);
    }
    

    After you've added the item to the list inside the adapter then you can call notifyDataSetChanged and the list will update.