通过单击数据库填充的列表中的列表项来显示SQLite数据

时间:2013-08-07 12:08:11

标签: android listview

我花了几天时间尝试自己实现这一目标并尝试了许多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);
}

}

任何人都可以深入了解我如何做我想要实现的目标吗?

1 个答案:

答案 0 :(得分:1)

您需要将focusable的{​​{1}}模式更改为TextView

以XML格式更新TextView

false