簡體   English   中英

NotifyDataSetChanged()是否不刷新列表?

[英]NotifyDataSetChanged() not refreshing list?

我知道這個問題已經被問過數千遍了,但是由於某種原因,我的案子找不到答案。

我所擁有的是一個線程,該線程從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;
    }

}

您還需要在addItem更新count變量,以便在調用notifyDataSetChanged時創建所有行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM