从ListActivity获取Clicked项目

时间:2013-01-26 16:57:28

标签: android listactivity

我试图在Android ListActivity中获得点击项目的字符串表示。我的意图是获得点击项目的字符串然后通过嵌入式tts服务将其转换为语音。通过长按每个项目来激发意图。这就是我得到的到目前为止。

  

this.getListView()setLongClickable(真);

     

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

上面是长按点击方法,正常工作。点击一下就可以了,但是它不起作用:

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

可能是什么问题?

1 个答案:

答案 0 :(得分:1)

myList.setOnItemClickListener(onListItemClick);

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

}