notifydatasetchanged()无法正常工作

时间:2016-08-23 06:57:58

标签: android listview background android-broadcast

我有一个listview和一个Baseadapter,但是adapeter.notiydatasetchanged()对我不起作用.....我在listview中得到一个适配器项目超过两次:(

我的适配器类扩展了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;
    }
}

这是listview和适配器数据的实现:

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);
}

比使用接收器我正在更新适配器数据并调用notifydatasetchanged()

接收人代码:

   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();
            }
}

注意:我的接收器对其他意图工作正常,因此接收器没问题。

我正在使用应用程序上下文和服务postexceute()函数中的后台线程发送广播,但是在listview中至少有一个项目被推送超过两次!

任何帮助将不胜感激!谢谢!

3 个答案:

答案 0 :(得分:2)

您没有将数据提供给适配器。您可以向适配器添加方法以更新数据:

public void add(String data) {

    this.pokemonList.add(data);
}

将项目添加到适配器内的列表后,您可以调用notifyDataSetChanged,列表将更新。

答案 1 :(得分:1)

请在您的适配器类中添加方法并添加notify方法。

public void addItem(String data) {
    this.pokemonItemList.add(data);
notifyDataSetChanged();
}

并添加广播数据。

adapter.addItem(StringValue);

答案 2 :(得分:0)

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

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

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

@Override
public Object 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;
}

}

    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);
}

然后

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.pokemonList.add(intent.getStringExtra(CommonForApp.UPDATE_LISTVIEW));
                adapter.notifyDataSetChanged();
            }
}