0

I am new to android and I'm developing a dictionary with multiple languages joined in one listview basically I'm trying to do a search all from all tables but if I clicked a word that has on table2 it displays an empty word and definition in the fragment.

This is from DbHelperClass

public Word getWordForAll(String word, int dicType) {
    String tableName1 = getAllTableName1(dicType);
    String tableName2 = getAllTableName2(dicType);
    String q = "SELECT * FROM "+ tableName1 +" WHERE word = ? UNION SELECT * FROM " + tableName2 + " WHERE word = ?";
    Cursor result = mDB.rawQuery(q, new String[]{word});

    Word word1 = new Word();
    while (result.moveToNext()) {
        word1.word = result.getString(result.getColumnIndex(COL_WORD));
        word1.definition = result.getString(result.getColumnIndex(COL_DEFINITION));
    }
    return word1;
}


public String getTableName(int dicType) {

    String tableName = "";

    if (dicType == R.id.language1) {
        tableName = lang1;
    } else if (dicType == R.id.language2) {
        tableName = lang2;
    } 
    return tableName;
}

//same for tableName2
public String getAllTableName1(int dicType) {

    String tableName1 = "";

    if (dicType == R.id.action_all) {
        tableName1 = lang1;
    }
    return tableName1;
}

This is from the definition fragment

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setHasOptionsMenu(true);
    
    tvWord = (TextView) view.findViewById(R.id.tvWord);
    tvDefinition = (TextView) view.findViewById(R.id.tvDefinition);
    
    //here mDicType is integer value from onOptionItemSelected in MainActivity
    if (mDicType == R.id.action_all){
        final Word word = mDBHelper.getWordForAll(value, mDicType);
        tvWord.setText(word.word);
        tvDefinition.setText(word.definition);

        Word bookmarkWord = mDBHelper.getWordFromBookmark(value);
        int isMark = bookmarkWord == null ? 0 : 1;
        btnBookmark.setTag(isMark);

        int icon = bookmarkWord == null ? R.drawable.bookmark:R.drawable.bookmarked;
        btnBookmark.setImageResource(icon);

        btnBookmark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = (int) btnBookmark.getTag();
                if (i == 0){
                    btnBookmark.setImageResource(R.drawable.bookmarked);
                    btnBookmark.setTag(1);
                    mDBHelper.addBookmark(word);
                }else if (i == 1){
                    btnBookmark.setImageResource(R.drawable.bookmark);
                    btnBookmark.setTag(0);
                    mDBHelper.removeBookmark(word);
                }
            }
        });
    } else {
        final Word word = mDBHelper.getWord(value,mDicType);
        tvWord.setText(word.word);
        tvDefinition.setText(word.definition);

        Word bookmarkWord = mDBHelper.getWordFromBookmark(value);
        int isMark = bookmarkWord == null ? 0 : 1;
        btnBookmark.setTag(isMark);

        int icon = bookmarkWord == null ? R.drawable.bookmark:R.drawable.bookmarked;
        btnBookmark.setImageResource(icon);

        btnBookmark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = (int) btnBookmark.getTag();
                if (i == 0){
                    btnBookmark.setImageResource(R.drawable.bookmarked);
                    btnBookmark.setTag(1);
                    mDBHelper.addBookmark(word);
                }else if (i == 1){
                    btnBookmark.setImageResource(R.drawable.bookmark);
                    btnBookmark.setTag(0);
                    mDBHelper.removeBookmark(word);
                }
            }
        });
    }

}

This is from my Main Activity

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.language1) {
        Global.saveState(this, "dic_type", String.valueOf(id));
        ArrayList<String> source = dbHelper.getWord(id);
        dictionaryFragment.resetDataSource(source);
        return true;
    } else if (id == R.id.language2) {
        Global.saveState(this, "dic_type", String.valueOf(id));
        ArrayList<String> source = dbHelper.getWord(id);
        dictionaryFragment.resetDataSource(source);
        return true;
    } else if (id == R.id.action_all) {
        Global.saveState(this, "dic_type", String.valueOf(id));
        ArrayList<String> source = dbHelper.getWordAll(id);
        dictionaryFragment.resetDataSource(source);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

And on onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    menuSetting = menu.findItem(R.id.action_settings);

    String id = Global.getState(this, "dic_type");

    if (id != null)
        onOptionsItemSelected(menu.findItem(Integer.valueOf(id)));
    else{
        ArrayList<String> source = dbHelper.getWord(R.id.language1);
        dictionaryFragment.resetDataSource(source);
    }
    return true;
}

0 Answers0