2012-03-23 53 views
0

(對不起,我不擅長英語) 我已經創建了一個自定義的BaseAdapter來顯示我的數據從sqlite到一個ListView。 現在我可以看到列表,但是當我快速滾動listview時,行很奇怪地查看。我無法理解這一點。還有一點奇怪的是,如果我有5個數據行,getView運行5次,但只有前4行可以顯示。我使用日誌,我看到5次「返回視圖」;清晰。錯誤滾動listview與適配器

這裏是我的適配器代碼:

public class LocalRankAdapter extends BaseAdapter { 
private static final String FIELD1 = "time"; 
private static final String FIELD2 = "name"; 
private static final String TABLE_NAME = "rank"; 
private static final String FIELD_ORDER = "time"; 

private DataBaseHelper dbHelper; 
private Cursor mCursor; 
private Context mContext; 
private int timeColIndex; 
private int nameColIndex; 
private int index; 
private int length; 
public static float recent_time=0; 
public static String recent_playerName=""; 

public LocalRankAdapter(Context context, DataBaseHelper mdbHelper) { 
    mContext=context; 
    dbHelper=mdbHelper; 

    // get data from database 
    mCursor = dbHelper.getData(TABLE_NAME, new String[] { FIELD1, FIELD2 }, FIELD1 + " !=0 ", FIELD_ORDER + " ASC"); 
    mCursor.moveToFirst(); 
    length=mCursor.getCount(); 

    timeColIndex=mCursor.getColumnIndex(FIELD1); 
    nameColIndex=mCursor.getColumnIndex(FIELD2); 

    index=1; 
} 

public static void insertData(DataBaseHelper dbHelper, float currentTime, String name) { 
    ContentValues cv=new ContentValues(); 
    cv.put(FIELD1, currentTime); 
    cv.put(FIELD2, name); 
    dbHelper.insertData(TABLE_NAME, new String[] {FIELD1, FIELD2}, cv); 
} 

@Override 
public int getCount() { 
    return length-1; 
} 

@Override 
public View getView(int id, View convertView, ViewGroup parent) { 
    View v=convertView; 
    if (convertView == null) { 
     v=convertView; 
     LayoutInflater inflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.list_item, parent, false); 
     TextView rankNumber=(TextView) v.findViewById(R.id.txtRank); 
     TextView time=(TextView) v.findViewById(R.id.txtTime); 
     TextView playerName=(TextView) v.findViewById(R.id.txtPlayerName); 

     String timeStr=mCursor.getString(timeColIndex); 
     String playerNameStr=mCursor.getString(nameColIndex); 

     if(Float.parseFloat(timeStr)==(recent_time/1000f) && playerNameStr.equals(recent_playerName)) { 
      v.setBackgroundColor(mContext.getResources().getColor(R.color.item_bg_recent)); 
      rankNumber.setText(String.valueOf(index++)+" (Your)"); 
     } else rankNumber.setText(String.valueOf(index++)); 
     time.setText(timeStr); 
     playerName.setText(playerNameStr); 
     mCursor.moveToNext(); 
     return v;   
    } 
    return v; 
} 

@Override 
public Object getItem(int id) { 
    return id; 
} 

@Override 
public long getItemId(int arg0) { 
    return 0; 
} 

public static void setModeView(int mtime, String mplayerName) { 
    recent_time=mtime; 
    recent_playerName=mplayerName; 
} 

}

回答

0

你改變你的查詢

String getData = "SELECT " + FIELD1 + "," + FIELD2 + " FROM " + TABLE_NAME; 
Cursor cursor; 
    cursor = myDataBase.rawQuery(getData, null); 
    while (cursor.moveToNext()) { 

     //String cursor.getColumnIndex(FIELD1); 
     int id = cursor.getInt(1); 
     String name = cursor.getString(0);    
} 
+0

是的,我認爲我的查詢是正確的,它工作正常。 – Kiradev 2012-03-23 09:53:02

+0

感謝您的閱讀 – Kiradev 2012-03-23 10:23:45

0

我看到一個可能的問題就在這裏:

@Override 
public int getCount() { 
    return length-1; 
} 

你應該寫return length;因爲length=mCursor.getCount()

另一個問題,你在getView中使用光標,只是做moveNext它。 我認爲這不是個好主意,因爲讀取數據不是一個快速的過程。所以它導致了滾動的滯後。嘗試將完整的數據集讀取到結構的內部列表中,並在getView中使用它。

+0

謝謝,我使用長度爲1,因爲我已經告訴過的問題。最後一個視圖不顯示,我做「-1」不查看它(如果不強制關閉)。我認爲你是對的,我應該從getview()中獲取數據。我會嘗試。 – Kiradev 2012-03-23 09:56:19

+0

你解決了這個問題嗎? – asktomsk 2012-04-05 06:49:49