我开发了一个支持离线数据库的字典应用程序。一切正常,但是应用程序花了很多时间来检索单词的定义。我知道数据结构存在问题。我只知道实现的ArrayList,但我不知道如何在我的应用程序中实现任何其他数据结构(HashMap,TRIE或任何其他)。我已经尝试了相同的Oracle文档,但我的所有努力都徒劳无功。我只是想要一些关于如何的帮助在我的应用程序中实现HashMap或任何其他数据结构。
提前致谢...
DMainActivity.java
public class DMainActivity extends AppCompatActivity {
private EditText filterText;
private ArrayAdapter<String> listAdapter;
private TextView textView2;
private TextView textView3;
private TextView textView4;
private TextView textView5;
private TextView textView6;
private TextView textView7;
private TextView textView8;
private TextView textView9;
private TextView textView10;
private TextView textView11;
private TextView textView12;
private TextView textView13;
private TextView textView14;
private TextView textView15;
private TextView textView16;
private TextView textView17;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dmain);
filterText = (EditText)findViewById(R.id.editText);
final ListView itemList = (ListView)findViewById(R.id.list_view);
itemList.setVisibility(View.GONE);
textView2 = (TextView)findViewById(R.id.textView2);
textView3 = (TextView)findViewById(R.id.textView3);
textView4 = (TextView)findViewById(R.id.textView4);
textView5 = (TextView)findViewById(R.id.textView5);
textView6 = (TextView)findViewById(R.id.textView6);
textView7 = (TextView)findViewById(R.id.textView7);
textView8 = (TextView)findViewById(R.id.textView8);
textView9 = (TextView)findViewById(R.id.textView9);
textView10 = (TextView)findViewById(R.id.textView10);
textView11 = (TextView)findViewById(R.id.textView11);
textView12 = (TextView)findViewById(R.id.textView12);
textView13 = (TextView)findViewById(R.id.textView13);
textView14 = (TextView)findViewById(R.id.textView14);
textView15 = (TextView)findViewById(R.id.textView15);
textView16 = (TextView)findViewById(R.id.textView16);
textView17 = (TextView)findViewById(R.id.textView17);
final DbBackend dbBackend = new DbBackend(DMainActivity.this);
String[] terms = dbBackend.dictionaryWords();
listAdapter = new ArrayAdapter<String>(this, R.layout.custom_textview,R.id.tv, terms);
// 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
String word = listAdapter.getItem(position);
String[] words = dbBackend.dictionaryWords();
for(int i=0; i < words.length; i++)
if(words[i].contains(word))
position = i;
Toast.makeText(DMainActivity.this, word + " " +position , Toast.LENGTH_SHORT).show();
//Toast.makeText(getApplicationContext(), "Position " + position, Toast.LENGTH_LONG).show();
Intent intent = new Intent(DMainActivity.this, DictionaryActivity.class);
intent.putExtra("DICTIONARY_ID", position);
startActivity(intent);
}
});
filterText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//MainActivity.this.listAdapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
if (s != null && s.toString().trim().length() > 0) {
itemList.setVisibility(View.VISIBLE);
//textView1.setVisibility(View.GONE)
textView2.setVisibility(View.GONE);
textView3.setVisibility(View.GONE);
textView4.setVisibility(View.GONE);
textView5.setVisibility(View.GONE);
textView6.setVisibility(View.GONE);
textView7.setVisibility(View.GONE);
textView8.setVisibility(View.GONE);
textView9.setVisibility(View.GONE);
textView10.setVisibility(View.GONE);
textView11.setVisibility(View.GONE);
textView12.setVisibility(View.GONE);
textView13.setVisibility(View.GONE);
textView14.setVisibility(View.GONE);
textView15.setVisibility(View.GONE);
textView16.setVisibility(View.GONE);
textView17.setVisibility(View.GONE);
} else {
itemList.setVisibility(View.GONE);
// textView1.setVisibility(View.VISIBLE);
textView2.setVisibility(View.VISIBLE);
textView3.setVisibility(View.VISIBLE);
textView4.setVisibility(View.VISIBLE);
textView5.setVisibility(View.VISIBLE);
textView6.setVisibility(View.VISIBLE);
textView7.setVisibility(View.VISIBLE);
textView8.setVisibility(View.VISIBLE);
textView9.setVisibility(View.VISIBLE);
textView10.setVisibility(View.VISIBLE);
textView11.setVisibility(View.VISIBLE);
textView12.setVisibility(View.VISIBLE);
textView13.setVisibility(View.VISIBLE);
textView14.setVisibility(View.VISIBLE);
textView15.setVisibility(View.VISIBLE);
textView16.setVisibility(View.VISIBLE);
textView17.setVisibility(View.VISIBLE);
}
listAdapter.getFilter().filter(s);
}
});
}
}
DbBackend.java
public class DbBackend extends DbObject{
public DbBackend(Context context) {
super(context);
}
public String[] dictionaryWords(){
String query = "Select * from words";
Cursor cursor = this.getDbConnection().rawQuery(query, null);
**ArrayList<String> wordTerms = new ArrayList<String>();
// HashMap<String,ArrayList<String>> hashMap = new HashMap<String, 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 QuizObject getQuizById(int quizId){
QuizObject quizObject = null;
String query = "select * from words 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("defn"));
quizObject = new QuizObject(word, meaning);
}while(cursor.moveToNext());
}
cursor.close();
return quizObject;
}
}
DictionaryActivity.java
public class DictionaryActivity extends ActionBarActivity{
private TextView wordMeaning;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dict_activity);
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());
}
}
答案 0 :(得分:0)
您有兴趣找到数据库中id
个字词(您称之为position
)。因此,您应该创建一个Map
,将单词映射到id
,Map
看起来像这样:
Map<String, Integer> wordsToIdMap = new HashMap<>();
为了填补地图:
do {
String word = cursor.getString(cursor.getColumnIndexOrThrow("word"));
// Unsure about this line, change it as needed:
int id = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
wordsToIdMap.put(word, id);
} while(cursor.moveToNext());
现在你可以通过这样的超快速呼叫来获得id
:
int id = wordsToIdMap.get(word);