2013-08-17 145 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