我的問題在於當我手動滾動文字從列表中選擇它時,它正確顯示了定義。但是,當我鍵入並搜索定義時,它總是顯示我的列表的第一項。我目前正在通過教程學習android,並遇到了這個例子,但無法修復這個bug。這裏是我的代碼:Android:在列表中使用陣列適配器進行搜索
public class DbBackend extends DbObject{
public DbBackend(Context context) {
super(context);
}
public String[] dictionaryWords(){
String query = "Select * from dictionary";
Cursor cursor = this.getDbConnection().rawQuery(query, null);
ArrayList<String> wordTerms = new ArrayList<String>();
if(cursor.moveToFirst()){
do{
String word = cursor.getString(cursor.getColumnIndexOrThrow("word"));
wordTerms.add(word);
}while(cursor.moveToNext());
}
cursor.close();
String[] dictionaryWords = new String[wordTerms.size()];
dictionaryWords = wordTerms.toArray(dictionaryWords);
return dictionaryWords;
}
//
// public int getIndexByValue(String value){
// S
// }
public QuizObject getQuizById(int quizId){
QuizObject quizObject = null;
String query = "select * from dictionary where _id = " + quizId;
Cursor cursor = this.getDbConnection().rawQuery(query, null);
if(cursor.moveToFirst()){
do{
String word = cursor.getString(cursor.getColumnIndexOrThrow("word"));
String meaning = cursor.getString(cursor.getColumnIndexOrThrow("meaning"));
quizObject = new QuizObject(word, meaning);
}while(cursor.moveToNext());
}
cursor.close();
return quizObject;
}
}
這裏的字典活動
public class DictionaryActivity extends ActionBarActivity {
private TextView wordMeaning;
private TextToSpeech convertToSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dictionary);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
int dictionaryId = bundle.getInt("DICTIONARY_ID");
int id = dictionaryId + 1;
TextView word = (TextView)findViewById(R.id.word);
wordMeaning = (TextView)findViewById(R.id.dictionary);
Button textToSpeech = (Button)findViewById(R.id.button);
DbBackend dbBackend = new DbBackend(DictionaryActivity.this);
QuizObject allQuizQuestions = dbBackend.getQuizById(id);
word.setText(allQuizQuestions.getWord());
wordMeaning.setText(allQuizQuestions.getDefinition());
}
另一類
public class MainActivity extends ActionBarActivity {
private EditText filterText;
private ArrayAdapter<String> listAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
filterText = (EditText)findViewById(R.id.editText);
ListView itemList = (ListView)findViewById(R.id.listView);
DbBackend dbBackend = new DbBackend(MainActivity.this);
String[] terms = dbBackend.dictionaryWords();
listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, terms);
itemList.setAdapter(listAdapter);
itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// make Toast when click
Toast.makeText(getApplicationContext(), "Position " + position, Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this, DictionaryActivity.class);
intent.putExtra("DICTIONARY_ID", id);
startActivity(intent);
}
});
有很多的代碼在這裏,其中大部分似乎與你的問題無關。請嘗試縮小代碼的哪部分似乎沒有按照您的意圖進行。如果你無法從所發佈的內容中縮小範圍,這表明你沒有花費足夠的時間來解決它。開始的好地方是點擊項目的地方。 – Karakuri
我很抱歉複製粘貼一切。我現在縮小了代碼的範圍。我試着查看代碼,發現列表的位置沒有移動。無論我搜索什麼,我的MainAcitivity類總是將位置值0發送到另一個顯示活動,並顯示列表中的第一個項目。也許,職位價值尚未更新。我應該通過身份證而不是職位? – user7062312
位置是指它在適配器中的位置,而不是它在數據庫中的位置。當列表被過濾時,適配器將原始列表保存起來,並使用僅包含與過濾條件匹配的項目的列表進行交換。適配器正確報告位置爲零。由於職位無法識別數據,因此您需要使用其他方法來識別該位置的數據。 – Karakuri