deucalion0
deucalion0

Reputation: 2440

Show SQLite data by clicking list item in a list populated by the Database

I have spent a few days trying to achieve this myself and have experimented with many Stackoverflow answers which are close to what I need to achieve to no avail.

I have an SQLite database, each row holds holds a phrase and a translation for that phrase in a foreign language, I need to display the stored phrases in a list which I have done so far but I am struggling to figure out how to do two things when clicking a list item. The two things are displaying the foreign translation of the clicked list item (phrase) in a toast as well as have text to speech read out the foreign string.

Here is my class which contains the code:

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);
}

}

Can anyone provide an insight into how I can do what I am trying to achieve?

Upvotes: 0

Views: 728

Answers (1)

SilentKiller
SilentKiller

Reputation: 6942

You need to change focusable mode of your TextView to false

update TextView in XML

android:focusable="false"

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {

          data.moveToPosition(position);
          Toast.makeText(getApplicationContext(), c.getString(c.getColumnIndex("notes")), Toast.LENGTH_LONG).show();
          speakOut(c.getString(c.getColumnIndex(BaseColumns._ID)));

      }
    }); 

Upvotes: 1

Related Questions