我花了几天时间尝试自己实现这一目标,并尝试了许多 Stackoverflow 答案,这些答案接近我需要实现的目标,但无济于事。
我有一个 SQLite 数据库,每一行都包含一个短语和该短语的外语翻译,我需要在一个列表中显示存储的短语,到目前为止我已经完成了但我正在努力弄清楚如何做两个单击列表项时发生的事情。这两件事是在敬酒中显示单击的列表项(短语)的外文翻译,以及让文本到语音读出外文字符串。
这是我的课程,其中包含代码:
public class ShowPhrases extends Activity implements OnInitListener{
private SQLiteDatabase database;
private CursorAdapter dataSource;
private Cursor mNotesCursor;
private NotesDbAdapter mDbHelper;
private TextToSpeech tts;
private static final String fields[] = { "phrase", BaseColumns._ID };//The phrase will be shown in the list
String Transalation = "folang";//folang holds the translation
@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.hs);
NotesDbAdapter.DatabaseHelper helper = new NotesDbAdapter.DatabaseHelper(this);
database = helper.getWritableDatabase();
Cursor data = database.query("notes", fields, null, null, null, null, null);
dataSource = new SimpleCursorAdapter(this, R.layout.highscores, data, fields, new int[] { R.id.first });
final ListView lv = (ListView) findViewById(R.id.list_view);
lv.setHeaderDividersEnabled(true);
lv.addHeaderView(getLayoutInflater().inflate(R.layout.highscores, null));
lv.setAdapter(dataSource);
lv.setClickable(true);
tts = new TextToSpeech(this, this);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
//LIST ITEM SHOWS ENGLISH WORD BUT WHEN CLICKING I NEED TO SHOW FOREIGN WORD AS WELL AS HAVE THE PHONE SPEAK IT
// speakOut(((TextView) findViewById(R.id.tvTranslatedText))
// .getText().toString());
}
});
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.ITALIAN);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut(String text) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
谁能提供有关我如何做我想要实现的目标的见解?