Reputation: 325
I am trying to get clicked item's string representation in Android ListActivity.My intention is get clicked item's string then convert it to speech by embedded tts service.The intent is fired by a long click on each item.Here is what i got so far.
this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) { int x=position; try{ switch (position) { case 0:startActivity(new Intent(MyActivity.this,Class.forName("Activity1"))); case 1:startActivity(new Intent(MyActivity.this,Class.forName("Acvivity2"))); case 2:startActivity(new Intent(MyActivity.this,Class.forName("Activity3"))); } return true; }catch (ClassNotFoundException e){ e.printStackTrace(); } return false; } }); setListAdapter(new ArrayAdapter<String>(MyActivity.this, R.layout.simple_list_item_1,program_names));
Above is long click method which is working.For a single click i thougth like this but it is not working:
public void onListItemClick(ListView lv,View v,int pos,long id){
super.onListItemClick(lv,v,pos,id);
String selected=(String) (lv.getItemAtPosition(pos));
tts.speak(selected,TextToSpeech.QUEUE_ADD,null);
}
What can be the problem?
Upvotes: 0
Views: 135
Reputation: 16354
myList.setOnItemClickListener(onListItemClick);
private AdapterView.OnItemClickListener onListItemClick = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
......
}
Upvotes: 1