2013-10-13 67 views
1

我有一個ListView顯示日期。通過使用Spinner,我可以選擇要顯示多少天(如果列表太長,我可以滾動瀏覽)。Android ListView選擇要顯示的項目數導致洗牌項目

enter image description here

當我看到30天,我在列表的底部,如果我用微調玩,選擇看15天,則30天,則15天,我結束了有重複或錯誤的元素。

enter image description here

我讀過有關,很多東西,每一個應用建議:

  • 無效

  • invalidateViews

  • notifyDataSetInvalidated

  • notifyDataSetChanged

  • refreshDrawableState

  • 使用處理器來運行這些調用

,但沒有固定我的問題。

我用下面的代碼:

=== CustomAdapter ===

public class CustomAdapter extends BaseAdapter { 

    private List<ViewHolder> viewHolders; 

    @Inject 
    public CustomAdapter(@Assisted List<ViewHolder> viewHolders) { 
     this.viewHolders = viewHolders; 
    } 

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

    @Override 
    public Object getItem(int i) { 
     return viewHolders.get(i); 
    } 

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

    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 
     return viewHolders.get(i).getView(view); 
    } 
} 

=== ListViewHolder ===

public class ListViewHolder extends ViewHolder { 

    @Override 
    public View getView(View view) { 
     Holder holder; 

     if (view == null) { 
      view = LayoutInflater.from(contextProvider.get()).inflate(R.layout.entry, null); 
      holder = new Holder(); 

      holder.text = (TextView) view.findViewById(R.id.textView); 

      view.setTag(viewStructure); 
     } else { 
      holder = (Holder) view.getTag(); 
     } 

     holder.text.setText(/* date */); 

     return view; 
    } 
} 

=== ListFragment == =

public class ListFragment extends RoboFragment { 

    private CustomAdapter customAdapter = null; 

    private List<ViewHolder> viewHolders = new ArrayList<ViewHolder>(); 

    private ListView listView; 

    private Handler handler = new Handler(); 

    @Inject 
    private ListFragment(AdapterFactory adapterFactory) { 
     customAdapter = adapterFactory.createAdapter(viewHolders); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     final View view = inflater.inflate(R.layout.list, container, false); 

     listView = (ListView) view.findViewById(R.id.listView); 

     listView.setAdapter(customAdapter); 

     return view; 
    } 

    public void setContentRange(List<ViewHolder> viewHolders, int range) { 
     update(viewHolders.subList(0, range)); 
    } 

    private void update(List<ViewHolder> content) { 
     viewHolders.clear(); 
     viewHolders.addAll(content); 

     if (customAdapter != null) { 
      handler.post(new Runnable() { 
       @Override 
       public void run() { 
        customAdapter.notifyDataSetChanged(); 
        //customAdapter.notifyDataSetInvalidated(); 

        if (listView != null) { 
         listView.invalidateViews(); 
        // listView.requestLayout(); 
        // listView.refreshDrawableState(); 
        } 
       } 
      }); 
     } 
    } 
} 

有什麼建議嗎?提前致謝。

對不起,不能發佈圖片,沒有足夠的聲譽...

編輯:

  • 添加鏈接,圖片

  • 的ListViewHolder的代碼實際上是這樣的。我簡化它張貼在這裏,但也許它的問題:

    公共類ListViewHolder擴展ViewHolder {

    @Inject 
    private ListViewHolder(Holder holder) { // Guice will create the holder 
        super(holder); 
    } 
    
    @Override 
    public View getView(View view) { 
        Holder holder; 
    
        if (view == null) { 
         view = LayoutInflater.from(contextProvider.get()).inflate(R.layout.entry, null); 
         holder = getHolder(); // getHolder is defined in ViewHolder, and returns the object created in the constructor 
    
         holder.text = (TextView) view.findViewById(R.id.textView1); 
    
         view.setTag(viewStructure); 
        } else { 
         holder = (DualTextViewStructure) view.getTag(); 
        } 
    
        holder.text.setText(/* Date */); 
    
        return view; 
    } 
    

    }

+0

「對不起,不能發佈圖片,沒有足夠的信譽」 - 上傳其他地方,然後從你的問題鏈接到他們。 – CommonsWare

+0

嘗試添加日誌。每次更改適配器中的項目時打印列表(可能是項目列表出錯)。在Adapter和ViewHolder中添加登錄方法getView()。 – alaster

回答

0

原來,使用吉斯在構造函數中創建的保持者是問題。 如果我使用一個供應商,則列表的行爲與預期

public class ListViewHolder extends ViewHolder { 

    @Override 
    public View getView(View view) { 
     Holder holder; 

     if (view == null) { 
      view = LayoutInflater.from(contextProvider.get()).inflate(R.layout.entry, null); 
      // getHolder looks like this: 
      // @Inject 
      // private Provider<Holder> holderProvider; 
      // public Holder getHolder() { 
      //  holderProvider.get(); 
      // } 
      holder = getHolder(); 

      holder.text = (TextView) view.findViewById(R.id.textView1); 

      view.setTag(viewStructure); 
     } else { 
      holder = (DualTextViewStructure) view.getTag(); 
     } 

     holder.text.setText(/* Date */); 

     return view; 
    } 
}