2012-12-14 27 views
0

我有一個自定義適配器,我綁定到一個列表視圖。那部分很好。當我滾動列表視圖時,排序被拋棄,因爲如果我滾動回到開始(即向下滾動後滾動),顯示的TextView不是第一個。在滾動過程中,是否有適配器需要處理?滾動與列表視圖和自定義TextView適配器拋出訂購?

public class TextViewAdapter extends BaseAdapter 
{ 
    private Context context; 

    public TextViewAdapter(Context c) 
    { 
     context = c; 
    } 

    //---returns the number of images--- 
    public int getCount() { 
     return textViews.size(); 
    } 

    //---returns the ID of an item--- 
    public Object getItem(int position) { 
     return position; 
    } 

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

    //---returns an TextView--- 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     TextView tv; 
     Log.d("Position", Integer.toString(position)); 
     if (convertView == null) { 

      tv = (TextView)textViews.get(position); 
     } else { 
      tv = (TextView) convertView; 
     }    
     return tv; 
    } 
} 

回答

1

您以錯誤的方式執行getView方法。如果您使用convertView應重置其文本內容,如

public View getView(int position, View convertView, ViewGroup parent) 
{ 
    TextView tv; 
    Log.d("Position", Integer.toString(position)); 
    if (convertView == null) { 

     tv = (TextView)textViews.get(position); 
    } else { 
     tv = (TextView) convertView; 
     // HERE, set its content 
     tv.setText(textViews.get(position).getText().toString()); 
    }    
    return tv; 
} 

一個ListView將回收爲您的孩子的意見,當它滾動,因此有的孩子去無形,從而成爲可重複使用。這就是爲什麼每個convertView最初都爲空,但當ListView向下滾動時變爲非零的原因。但那些不爲空的convertView仍然保留其原始文本內容,您應手動重置其內容。

順便說一句,你的應用程序啓動時不需要新的很多TextView,只是一個String名單是確定的,就像

/* somewhere else */ 
List<String> strings = ... 

public class TextViewAdapter extends BaseAdapter 
{ 
    /* ... */ 

    //---returns the number of images--- 
    public int getCount() { 
     return strings.size(); 
    } 

    /* ... */ 

    //---returns an TextView--- 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     TextView tv; 
     Log.d("Position", Integer.toString(position)); 
     if (convertView == null) { 
      tv = new TextView(context); 
      tv.setText(strings.get(position)); 
     } else { 
      tv = (TextView) convertView; 
      // HERE, set its content 
      tv.setText(strings.get(position)); 
     }    
     return tv; 
    } 
} 
0

,你會遇到的是一個主要問題您要保存到適配器使用的意見參考:

tv = (TextView)textViews.get(position); 

的問題是,您正在使用適配器回收站競爭,這將創造不可預知的結果 ...總之你不能 做這個。請觀看Android開發者的World of ListView以獲取更多信息。

總之,你應該改變你的方法,以便你的Adapter保留一個TextViews將顯示的字符串列表,而不是TextViews本身。

0

如果convertView爲null,則應重新爲視圖充氣,否則需要重置convertView上的數據。

+0

我見過膨脹的例子,但它似乎是在xml佈局文件中的膨脹。我在運行時動態添加TextView。我通過什麼 inflater.inflate(R.layout.calendarcell,null); LayoutInflater inflater =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); cellView = inflater.inflate(R.layout.calendarcell,null); – stack

+0

是的,膨脹只適用於在xml中定義的小部件,如果您動態添加它,您可以新建一個TextView並將其分配給convertView。 ConvertView只是意味着你可以重新使用它,但是當它爲空時,它可能是你重新創建它的正確時間,無論是從xml的充氣機還是動態新的它。 –

相關問題