2017-01-23 46 views
0

我在Android中創建了一個ListView的適配器,它的行爲真的很奇怪。該列表視圖傳遞了一個對象列表,它按照一些規則排序(比較數字,哇...)。現在,當列表顯示在視圖中時,會顯示重複的條目,它們不會排序,條目丟失,並且當我滾動列表時甚至條目也會更改!到底是怎麼回事?爲什麼我的列表視圖在android中表現奇怪?

這裏是適配器的代碼,我可以張貼更多的代碼,如果這是必要的:

public class StatViewAdapter extends BaseAdapter { 

    Activity activity; 
    ArrayList<Entry> entries; 

    TextView txtName; 
    TextView txtOK; 
    TextView txtNOK; 
    TextView txtHist; 
    TextView txtPrandom; 
    TextView txtPhist; 
    TextView txtPtotal; 

    //public StatViewAdapter(Activity activity, ArrayList<HashMap<String, String>> list){ 
    public StatViewAdapter(Activity activity, ArrayList<Entry> entries){ 

     super(); 
     this.activity=activity; 
     this.entries = entries; 

     Collections.sort(this.entries, new Comparator<Entry>() { 
      @Override 
      public int compare(Entry o1, Entry o2) { 
       if (o1.getPriority() > o2.getPriority()) { 
        return 1; 
       } 
       if (o1.getPriority() < o2.getPriority()) { 
        return -1; 
       } 
       return 0; 

      } 
     }); 

     for (int i=0;i<this.entries.size();i++) { 
      String name = this.entries.get(i).name(); 
      int p = this.entries.get(i).getPriority(); 
      System.out.println(String.format("%s: %d", name, p)); 
     } 


    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     //return list.size(); 
     return this.entries.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return this.entries.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 



    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     LayoutInflater inflater=this.activity.getLayoutInflater(); 

     if(convertView == null){ 

      convertView=inflater.inflate(R.layout.list_view, null); 

      txtName=(TextView) convertView.findViewById(R.id.listname); 
      txtOK=(TextView) convertView.findViewById(R.id.listok); 
      txtNOK=(TextView) convertView.findViewById(R.id.listnok); 
      txtHist=(TextView) convertView.findViewById(R.id.listhist); 
      txtPrandom =(TextView) convertView.findViewById(R.id.listprandom); 
      txtPhist=(TextView) convertView.findViewById(R.id.listphist); 
      txtPtotal=(TextView) convertView.findViewById(R.id.listptot); 
     } 

     Entry entry = this.entries.get(position); 
     txtName.setText(entry.name()); 
     txtOK.setText(Integer.toString(entry.number_ok)); 
     txtNOK.setText(Integer.toString(entry.number_nok)); 
     txtHist.setText(entry.history); 
     txtPrandom.setText(Integer.toString(entry.randomIndex)); 
     txtPhist.setText(Integer.toString(entry.histIndex)); 
     txtPtotal.setText(Integer.toString(entry.getPriority())); 

     return convertView; 
    } 

} 
+1

很明顯,因爲您在適配器 – Selvin

+0

中直接存儲對項目視圖的引用,什麼?你可以解釋嗎?它對我來說並不明顯... – Alex

+0

分析如果convertView不爲null,但與getView返回的最後一個視圖不一樣會發生什麼 – Selvin

回答

2

public View getView(int position, View convertView, ViewGroup parent)的目的是在給定position到對象的數據綁定到視圖。該視圖可能是一個可回收的視圖(convertView),或者您需要在該方法中創建一個視圖。問題在於,您不應該在Adapter中保留對這些視圖的引用(txtNametxtOk)。

public class StatViewAdapter extends BaseAdapter { 

    Activity activity; 
    ArrayList<Entry> entries; 

    //TextView txtName; 
    //TextView txtOK; 
    // ... 

    public StatViewAdapter(Activity activity, ArrayList<Entry> entries){ 
    // ... 

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

     LayoutInflater inflater=this.activity.getLayoutInflater(); 

     if(convertView == null){ 
      convertView=inflater.inflate(R.layout.list_view, null); 
     } 

     TextView txtName=(TextView) convertView.findViewById(R.id.listname); 
     TextView txtOK=(TextView) convertView.findViewById(R.id.listok); 
     // ... 

     Entry entry = this.entries.get(position); 
     txtName.setText(entry.name()); 
     txtOK.setText(Integer.toString(entry.number_ok)); 
     // .... 

     return convertView; 
    } 

它的工作原理,但它仍然是不要做的好方法,你可能需要使用一個RecyclerView或實現ViewHolder模式。

ViewHolder模式的想法是,您不必在每次綁定視圖時都調用findViewById,因爲它在計算上很昂貴,並且可能會使滾動滯後。

您可能需要使用一個RecyclerView,因爲它是做了ListView的同一作業更新,更靈活地查看它具有ViewHolder模式內置。

+0

不,不起作用。我看到了相同的結果。也許我必須清理這個項目。一會兒...... – Alex

+0

好吧,這似乎工作。但爲什麼這不是一個好方法。什麼是「RecyclerView」?我不想回收視圖,只是爲了在列表視圖中呈現數據....? – Alex

+0

我編輯答案爲什麼它不是一個很好的理由,也如果它幫助請考慮接受答案:) – lelloman

相關問題