NotifyDataSetChanged()不刷新列表?

时间:2014-01-17 09:57:40

标签: java android android-listview baseadapter

我知道这个问题已被问过几千次,但由于某些原因我无法找到答案。

我所拥有的是一个从Web服务获取数据并使用该信息填充列表的线程。然后我想按一个按钮并调用同一个线程来获取更多数据并将其添加到列表中。

但是当我拨打notifyDataSetChanged()时,由于某种原因,列表不会刷新。数据在适配器中虽然......

以下是代码:

    @Override
        protected void onPostExecute(PropertyNotesParser result) {

            this.progressDialog.dismiss();

            ArrayList<PropertyNoteHistory> propertyNoteList = result.getAllTheNotes();
            addNoteListItems(propertyNoteList);

            Collections.sort(getNoteList());

            ArrayList<String> titles = new ArrayList<String>();
            ArrayList<String> subtitles = new ArrayList<String>();
            DateHandler handleDate = new DateHandler();
            DataResolve convert = new DataResolve();

            for(Iterator<PropertyNoteHistory> i = getNoteList().iterator(); i.hasNext(); ) {
                PropertyNoteHistory item = i.next();

                PropertyNoteHistory.Note note = item.getNotes();
                PropertyNoteHistory.Jobs jobs = item.getJobs();

                // Default value is office in case the xml does not have the tag "job" associated with "note".
                String service = "Office";

                if(jobs != null){
                    service = convert.getServiceName(jobs.getServiceID());
                } 

                titles.add(note.getTitle() + " (" + service + ")");
                subtitles.add(handleDate.formatDate(note.getDate(), "dd MMM yyyy") + " Ref: " + note.getJobID());
            }

            if(getConnectionCount() == 0){
                adapter = new SimpleListAdapter(getActivity(), titles, subtitles);
                lv.setAdapter(adapter);
            }

            else {
                adapter.addItem(titles, subtitles);

            }

和我的适配器:

    public class SimpleListAdapter extends BaseAdapter {
    private int count = 0;
    private static LayoutInflater inflater = null;
    private ArrayList<String> titles = new ArrayList<String>();
    private ArrayList<String> subtitles = new ArrayList<String>();
    private ArrayList<Integer> imageResource = new ArrayList<Integer>();
    private boolean hasImage = false;

    public SimpleListAdapter(Activity activity, ArrayList<String> titles,
            ArrayList<String> subtitles, ArrayList<Integer> imageResource) {
        count = titles.size();
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.titles = titles;
        this.subtitles = subtitles;
        this.imageResource = imageResource;
        this.hasImage = true;
    }

    /**Constructor that creates an adapter with only a title and subtitle.
     * @param activity The context.
     * @param titles ArrayList with the titles of each list option.
     * @param subtitles ArrayList with the subtitles of each list option. */
    public SimpleListAdapter(Activity activity, ArrayList<String> titles,
            ArrayList<String> subtitles) {
        count = titles.size();
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.titles = titles;
        this.subtitles = subtitles;
        this.hasImage = false;
    }

    public void addItem(ArrayList<String> title, ArrayList<String> subtitle){
        this.titles.addAll(title);
        this.subtitles.addAll(subtitle);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return count;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if(v == null)
            v = inflater.inflate(R.layout.layout_simple_list, null);
        final ImageView icon = (ImageView) v.findViewById(R.id.icon);
        final TextView title = (TextView) v.findViewById(R.id.title);
        final TextView subtitle = (TextView) v.findViewById(R.id.subtitle);

        title.setText(titles.get(position));
        subtitle.setText(subtitles.get(position));
        if (hasImage) {
            icon.setImageResource(imageResource.get(position));
        }

        else {
            icon.setVisibility(ImageView.GONE);
        }

        return v;
    }

}

1 个答案:

答案 0 :(得分:1)

您需要在count中更新addItem变量,以便在调用notifyDataSetChanged时创建所有行