您好我正在使用此代码
Cursor mCursor = this.getContentResolver().query(PlayerContentProviderDB.CONTENT_URI, PLAYERS_PROJECTION, null, null, Players.SCORE + " ASC" + " LIMIT 2");
this.adapter = new SimpleCursorAdapter(this, R.layout.high_score, mCursor, dataColumns, viewIDs, 0);
setListAdapter(adapter);
_
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
// System.out.println(sortOrder);
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
checkColumns(projection);
queryBuilder.setTables(PlayerContract.Players.PLAYERS_TABLE_NAME);
int uriType = sURIMatcher.match(uri);
switch (uriType) {
case ALL_SCORES:
break;
case SCORE_ID:
queryBuilder.appendWhere(PlayerContract.Players.ID + "=" + uri.getLastPathSegment());
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
SQLiteDatabase db = this.dbHelper.getReadableDatabase();
Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
System.out.println(uri.toString() + " " + projection[0] + " " + projection[1] + " " + projection[2] + " " + projection[3] + " " + selection + " " + sortOrder);
// Notify potential listeners
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
尝试排序和限制数据库的返回,但它无法正常工作。通过插入诊断打印语句,我注意到在调用上述代码后立即调用我的内容提供程序的查询方法。我认为这可能与我的错误的根源有关。所以我的问题是为什么我的查询方法被调用两次?
更新
我现在知道我的错误是由于调用两次查询引起的。在第一次调用时,我的字符串顺序是正确的,但在第二次调用时它是null,这使得我的列表未分类且无限制。
我的ListView链接到新活动
setListAdapter(new ArrayAdapter<String>(this, R.layout.puzzle_selector, options));
ListView list = this.getListView();
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (!((TextView) view).getText().toString().equalsIgnoreCase("HighScore")) {
String selectionString[] = ((TextView) view).getText().toString().split(" ");
String puzzleNumber = selectionString[1];
System.out.println("puzzleNumber is " + puzzleNumber);
SharedPreferences prefs = getSharedPreferences(getString(R.string.currentSave), MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("selectedNumber", puzzleNumber);
editor.commit();
Intent passPuzzleNumberIntent = new Intent(PuzzleSelector.this, MainActivity.class);
passPuzzleNumberIntent.putExtra("puzzleNumber", puzzleNumber);
startActivity(passPuzzleNumberIntent);
} else {
Intent lookAtScoresIntent = new Intent(PuzzleSelector.this, HighScores.class);
startActivity(lookAtScoresIntent);
}
}
});
答案 0 :(得分:2)
由于列表视图布局两次,一次没有高度,再次设置高度后(在Android中常见),getView在与适配器关联的列表上被调用两次。为了确保您不会多次在列表视图中布置个体视图,请检查视图是否为null,如果是,则将其放在方法getView中:
public View getView(int position, View convertView, ViewGroup parent) {
super.getView(position, convertView, parent);
if (convertView == null) {
// do custom layout of view with data from cursor
}
return convertView;
}