2014-01-17 102 views
0

我知道這個問題已被詢問了數千次,但由於某種原因我無法找到答案。NotifyDataSetChanged()不刷新列表?

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

} 
+2

您沒有更新addItem中的count數值 – njzk2

+1

您要在哪裏通知數據集c在途中被絞死? –

+0

@njzk2說了什麼,或者只是在'getCount()'中使用'returns titles.size()'而不是 – hypd09

回答

1

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

+0

它正在其他地方更新。 –

+0

你可以請將代碼更新到哪裏? – Apoorv

+0

int currentCount = getConnectionCount(); \t \t \t \t setConnectionCount(currentCount + 1); \t \t \t \t getAndParseNotes(lvPropertyNotes); - > getAndParseNotes()調用Thread ... –

相關問題