2013-08-17 184 views
0

我想要填充從遊標和裝載機一個SQLiteDatabase一個ListView,但所有的項目都經過單個完整CursorAdapter的bindView障礙

我CursorAdapter類

@Override 
public void bindView(View view, Context context, Cursor c) { 
    ItemHolder holder = null; 
    try{ 
     holder = (ItemHolder) view.getTag(); 
    } catch (Exception e){ 
     e.printStackTrace(); 
    } 

    if(holder != null){ 
     System.out.println("b "+holder.position); 
    } 

} 

@Override 
public View newView(Context context, Cursor c, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View nView = inflater.inflate(layoutID, parent, false); 

    ItemHolder holder = new ItemHolder(); 
    System.out.println("a "+c.getPosition()); 
    holder.position = c.getPosition(); 
    nView.setTag(holder); 

    return nView; 
} 

logcat的輸出顯示在總混亂滾動返回(一個是在bindView在NewView的()的位置和b())

a 0 
b 0 
a 1 
b 1 
a 2 
b 2 
b 0 

代替

a 0 
b 0 
a 1 
b 1 
a 2 
b 2 
a 3 
b 3 

光標在適配器回報的檢查正確性

c.moveToFirst(); 
    for(int i = 0; i < c.getCount(); i++){ 
     System.out.println(i+" "+c.getString(c.getColumnIndex(COL_NAME))); 
     c.moveToNext(); 
    } 

我用的是最新的anroid.support.v4庫

回答

1

你需要一個ORDER BY條款添加到您的SQL語句,即對查詢的結果進行排序。查看the documentationthis example瞭解更多詳情。

樣品:

SELECT * FROM <table> ORDER BY <column>; 

附::如果您使用database.query(...)您可以排序列在此處添加參數(見orderBy):

query(String table, String[] columns, String selection, 
     String[] selectionArgs, String groupBy, String having, String orderBy) 

退房的Android documentation