(抱歉,我不擅长英语) 我创建了一个自定义BaseAdapter来将我的数据从sqlite显示到ListView。 现在我可以看到列表但是当我快速滚动列表视图时,行奇怪地查看。我无法理解这一点。还有一个奇怪的地方是,如果我有5个数据行,getView运行5次,但只能显示前4行。我使用日志,我看到5次“返回视图”;清楚。
以下是我的适配器代码:
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 :(得分: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);
}
答案 1 :(得分:0)
我在这里看到一个可能的问题:
@Override
public int getCount() {
return length-1;
}
您应该写return length;
,因为length=mCursor.getCount()
另一个问题是你在getView中使用游标并且只对它执行moveNext。
我认为这不是一个好主意,因为获取数据并不是一个快速的过程。因此它会导致滚动滞后。尝试将完整数据集读取到内部结构列表,并在getView
中使用它。