ListView Baseadapter notifyDataSetChanged()不起作用

时间:2014-05-05 14:24:58

标签: android listview

我在视图寻呼机内有列表视图。当我更新列表并尝试通过notifyDataSetChanged()更新列表视图时,它没有工作,也没有更新列表。第一个列表异步任务调用获取列表,然后调用LoadMoreList获取更多数据。

如果我将视图寻呼机外的列表视图比notifyDataSetChanged工作但在视图寻呼机内部没有。

private class List extends AsyncTask<Void, Void, String> {

    ArrayList<String> loadMoreList;
    @Override
    public String doInBackground(Void... params) {
        loadMoreList = WebFunctions.getEventList();

        return null;
    }

    @Override
    public void onPreExecute() {
    }

    @Override
    public void onPostExecute(String string) {
        if(loadMoreList != null){
            eventList.addAll(loadMoreList);
            adapter = new myPagerAdapter(thisContext, eventList);
            myPager.setAdapter(adapter );

            }
    }
}

异步任务以获取更多数据。

private class LoadMoreList extends AsyncTask<Void, Void, String> {

    ArrayList<String> loadMoreList;
    @Override
    public String doInBackground(Void... params) {
        loadMoreList = WebFunctions.getEventList();

        return null;
    }

    @Override
    public void onPreExecute() {
        }

    @Override
    public void onPostExecute(String string) {
        if(loadMoreList != null){
            eventList.addAll(loadMoreList);
            myAdapter.notifyDataSetChanged();

            } 
    }
}

查看寻呼机适配器

 //Custom pager adapter
private class myPagerAdapter extends PagerAdapter
{
    ArrayList<String> eventlist;
    private final Context context;
    private LayoutInflater inflater;
    public myPagerAdapter (Context context , ArrayList<String> events)
    {
        this.eventlist = events;
        this.context = context;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {

        return eventlist.size();
    }

    @Override
    public Object instantiateItem(View container, int position) {

        final LinearLayout page = (LinearLayout) inflater.inflate(R.layout.event_list, null);

        listView = (ListView) page.findViewById(R.id.listView);
        myAdapter = new myAdapter(eventList);
        listView.setAdapter(myAdapter);
        ((ViewPager) container).addView(page);

        return page;
    }
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);

    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}

这是myAdpter

 //list adapter
public class myAdapter extends BaseAdapter {
    private final ArrayList<ContactMoadel> contact;

    public myAdapter(ArrayList<String> contactlist) {
        this.contact = contactlist;
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        retrun convertView;
    }
}

2 个答案:

答案 0 :(得分:0)

  • 我认为可能存在问题,因为您从doInBackground方法返回null,将返回值更改为loadMoreList

@覆盖

public String doInBackground(Void... params) {
  loadMoreList = WebFunctions.getEventList();

  return loadMoreList;
}

答案 1 :(得分:0)

解决了这个问题。我第一次收到活动清单时应该设置适配器。 在列表异步任务

@Override
public void onPostExecute() {
    if(loadMoreList != null){
        eventList.addAll(loadMoreList);
        myAdapter = new myAdapter(thisContext, eventList);  
}

和LoadMoreList

@Override
public void onPostExecute() {
    if(loadMoreList != null){
        eventList.addAll(loadMoreList);
        myAdapter.notifyDataSetChanged();
}

查看寻呼机适配器应该是这样的

 //Custom pager adapter
private class myPagerAdapter extends PagerAdapter
{
    ArrayList<String> eventlist;
    private final Context context;
    private LayoutInflater inflater;
    public myPagerAdapter (Context context , ArrayList<String> events)
   {
       this.eventlist = events;
       this.context = context;
       this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   }
    @Override
    public int getCount() {

        return eventlist.size();
    }

    @Override
    public Object instantiateItem(View container, int position) {

        final LinearLayout page = (LinearLayout) inflater.inflate(R.layout.event_list,   null);

        listView = (ListView) page.findViewById(R.id.listView);
        listView.setAdapter(myAdapter);
        ((ViewPager) container).addView(page);

        return page;
   }
   @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);

   }

   @Override
   public int getItemPosition(Object object) {
        return POSITION_NONE;
  }
}