2017-02-26 40 views
0

我開發了一個離線數據庫支持的字典應用程序。一切工作都很好,但應用程序花費時間檢索單詞的定義。我知道數據結構存在問題。我只知道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

你有興趣找到你的數據庫中的單詞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); 
+0

Tnxx的解釋先生...這是真正有益的。只是最後一個查詢。我在哪裏把最後一行即int id = wordsToIdMap.get(word); – Mandy8055

+0

我會把那個你稱爲dbBackend的字典單詞。在dbbackend中創建一個接受單詞的方法,然後從wordsToIdMap中獲取它。 – john16384

+0

再次感謝先生,我非常感激...... – Mandy8055