简体   繁体   中英

Getting Clicked Item from ListActivity

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?

myList.setOnItemClickListener(onListItemClick);

private AdapterView.OnItemClickListener onListItemClick = new AdapterView.OnItemClickListener() {
@Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
......

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM