2013-01-05 16 views
1

我有一個ListView,它使用SimpleCursorAdapter從sqlite中讀取數據,並且該表包含大約1000行,但是我已按照日期篩選了我的Activity中的列表,因此篩選後的光標在該特殊日期中包含2行。爲此我想添加自定義的行號(不能使用_id)對,我已經因子評分我的list.one soloution,是ViewBinder,這裏是我的代碼:ListView中的行號

adapter.setViewBinder(new ViewBinder() { 
    public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) { 
     if (aColumnIndex == 0) { 
      aCursor.moveToFirst(); 
      if(aCursor.moveToFirst()) { 
       TextView textView = (TextView) aView; 
       textView.setText("" + WeeklyListRowNumber); 
       WeeklyListRowNumber = WeeklyListRowNumber + 1; 
      } 
      return true; 
     } 
     return false; 
    } 
}); 

我有11列我List和WeeklyListRowNumber在上面初始化了1,我的問題是我的rownumbers變成了7,8,但它一定是1,2。有人告訴我我該如何解決這個問題?

+1

你說的*自定義行數意味着*?爲什麼要添加自定義行號? – Luksprog

+0

我在列表中有一列作爲行號,但是我不能使用_id,因爲我的列表將過濾一個日期,它將從另一個Activity.so我需要一個行號,從1開始,根據我的結束listitem計數 –

+0

如果你有另一種解決方案,我會很高興聽到 –

回答

1
public View getView(int position, View convertView, ViewGroup parent) { 
     View retval = null; 

      retval = LayoutInflater.from(parent.getContext()).inflate(
        R.layout.content, null); 

      title = (TextView) retval.findViewById(R.id.contactName); 
      number = (TextView) retval.findViewById(R.id.contactNumber); 
      title.setText(text to display in list); 
      number.setText(""+position);//add row number to list //fixed the variable 
     } 
+0

謝謝deepak的答案和你的時間,我解決了它與查看器。 –

1

由於您使用適配器的列表視圖,所以你可能會在getview中獲取位置變量。 使用該位置(int)作爲自定義列表行號 它將從零(0)開始。

設置它根據按規定...

+0

即時通訊不擴展ListActivity(它的活動),所以你可以提供一些代碼或示例來告訴我如何使用adapter.getView(position,convertView,parent)來改變行號? –

+0

list =(ListView)findViewById(R.id.list); list.setAdapter(mAdapter); – deepak825

1

finaly我已經soved我的問題ViewBinder:

adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { 

     public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) { 
      if (aColumnIndex == 0) { 
        TextView textView = (TextView) aView; 
        int CursorPos = aCursor.getPosition() + 1; 
        textView.setText(Integer.toString(CursorPos)); 

       return true; 
      } 

      return false; 
     }});