(對不起,我不擅長英語) 我已經創建了一個自定義的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;
}
}
是的,我認爲我的查詢是正確的,它工作正常。 – Kiradev 2012-03-23 09:53:02
感謝您的閱讀 – Kiradev 2012-03-23 10:23:45